SlideShare a Scribd company logo
A First Book of C++
Chapter 2
Data Types, Declarations, and Displays
 In this chapter, you will learn about:
 Data Types
 Arithmetic Operators
 Variables and Declarations
 Common Programming Errors
 Bits, Bytes, and Binary Number Representations
A First Book of C++ 4th Edition 2
Objectives
 The objective of all programs is to process data
 Data is classified into specific types
 Numerical
 Alphabetical
 Audio
 Video
 C++ allows only certain operations to be performed
on certain types of data
 Prevents inappropriate programming operations
A First Book of C++ 4th Edition 3
Data Types
 Data type
 Set of values and operations that can be applied to a
particular data
 Example of a data type: Integer
 The values: set of all Integer numbers (whole number,
no decimal points)
 The operations: familiar mathematical and comparison
operators
A First Book of C++ 4th Edition 4
Data Types (cont'd.)
 Class data type
 Programmer-created data type
 Set of acceptable values and operations defined by a programmer
using C++ code
 Built-in data type
 Provided as an integral part of C++
 Also known as a primitive type
 Requires no external code
 Consists of basic numerical types
 Majority of operations are symbols (e.g. +, -, *, …)
A First Book of C++ 4th Edition 5
Data Types (cont'd.)
A First Book of C++ 4th Edition 6
Numerical Data Types
 Literal
 Value explicitly identifies itself (what you see is what
you get)
 The numbers 2, 3.6, and –8.2 are literals
 Values are literally displayed
 The text “Hello World!” is a literal
 Text itself is displayed
 Literals also known as literal values and constants
A First Book of C++ 4th Edition 7
Data Types (cont'd.)
 C++ provides nine built-in integer data types :
 bool, char, short int, int, long int unsigned char, unsigned short int,
unsigned int, unsigned long int
 Three most important
 int
 char
 bool
 Reason for remaining types is historical
 Originally provided for special situations
 Difference among types based on storage requirements
A First Book of C++ 4th Edition 8
Integer Data Types
 int data type
 Set of values supported are whole numbers
 Whole numbers mathematically known as integers
 Explicit signs allowed
 Commas, decimal points, and special signs not
allowed
 Examples of int:
 Valid: 0 5 -10 +25 1000 253 -26351 +36
 Invalid: $255.62 2,523 3. 6,243,982 1,492.89
A First Book of C++ 4th Edition 9
Integer Data Types (cont’d.)
 Different compilers have different internal limits on
the largest and smallest values that can be stored in
each data type
 Most common allocation for int is 4 bytes
(range: -2,147,483,648 to 2,147,483,647)
A First Book of C++ 4th Edition 10
Integer Data Types (cont’d.)
smallest possible integer stored largest possible integer stored
 char data type
 Used to store single characters
 Letters of the alphabet (upper- and lowercase)
 Digits 0 through 9
 Special symbols such as + $ . , - !
 Single character value: letter, digit, or special
character enclosed in single quotes
 Examples ‘A’ ‘$’ ‘b’ ‘7’ ‘y’ ‘!’ ‘M’ ‘q’
A First Book of C++ 4th Edition 11
Integer Data Types (cont’d.)
 Character values stored in ASCII or Unicode codes
 ASCII: American Standard Code for Information
Interchange
 Provides English language-based character set plus
codes for printer and display control
 Each character code contained in 1 byte (1 byte = 8 bits)
 256 distinct codes
A First Book of C++ 4th Edition 12
Integer Data Types (cont’d.)
ASCII Code
A First Book of C++ 4th Edition 13
ASCII Code
A First Book of C++ 4th Edition 14
 Unicode: provides other language character sets
 Each character contained in 2 bytes
 Can represent 65,536 characters
 First 256 Unicode codes have same numerical value as
the 256 ASCII codes
A First Book of C++ 4th Edition 15
Integer Data Types (cont’d.)
Unicode
A First Book of C++ 4th Edition 16
Unicode
A First Book of C++ 4th Edition 17
 The escape character
 Backslash (  )
 Special meaning in C++
 Placed in front of a group of characters, it tells the
compiler to escape from normal interpretation of these
characters
 Escape sequence: combination of a backslash and
specific characters
 Example: newline escape sequence, n
A First Book of C++ 4th Edition 18
Integer Data Types (cont’d.)
A First Book of C++ 4th Edition 19
 bool data type
 Represents boolean (logical) data
 Restricted to true or false values
 Often used when a program must examine a specific
condition
 If condition is true, the program takes one action; if
false, it takes another action
 Boolean data type uses an integer storage code
A First Book of C++ 4th Edition 20
Integer Data Types (cont’d.)
Example Boolean
A First Book of C++ 4th Edition 21
 C++ makes it possible to see how values are stored
 sizeof()
 Provides the number of bytes required to store a value
for any data type
 Built-in operator that does not use an arithmetic symbol
A First Book of C++ 4th Edition 22
Determining Storage Size
 Signed data type: stores negative, positive, and zero
values
 Unsigned data type: stores positive and zero values
 Provides a range of positive values double that of
unsigned counterparts
 Some applications only use unsigned data types
 Example: date applications in form yearmonthday
A First Book of C++ 4th Edition 23
Determining Storage Size (cont’d.)
A First Book of C++ 4th Edition 24
Determining Storage Size (cont'd.)
A First Book of C++ 4th Edition 25
Determining Storage Size (cont'd.)
Type Storage Absolute Range
Float 4 bytes 1.40129846432481707 x 10-45 to
3.40282346638528860 x 10-45
double and long
double
8 bytes 4.94065645841246544 x 10-45 to
1.79769313486231570 x 10-45
 The number zero or any positive or negative number
that contains a decimal point
 Also called real number
 Examples: +10.625 5. -6.2 3521.92 0.0
 5. and 0.0 are floating-point, but same values without a
decimal (5, 0) would be integers
 C++ supports three floating-point types
 float, double, long double
 Different storage requirements for each
A First Book of C++ 4th Edition 26
Floating-Point Types
 Most compilers use twice the amount of storage for
doubles as for floats
 Allows a double to have approximately twice the
precision of a float
 A float value is sometimes referred to as a single-
precision number
 A double value is sometimes referred to as a double-
precision number
A First Book of C++ 4th Edition 27
Floating-Point Types (cont'd.)
 Floating-point numbers can be written in exponential
notation
 Similar to scientific notation
 Used to express very large and very small values in
compact form
A First Book of C++ 4th Edition 28
Exponential Notation
 Binary operators: require two operands
 Can be a literal or an identifier
 Binary arithmetic expression:
 literalValue operator literalValue
A First Book of C++ 4th Edition 29
Arithmetic Operations
 cout allows for display of result of a numerical expression
 Display is on standard output device
 Example:
cout << “The sum of 6 and 15 is ” << (6 + 15)
 Statement sends string and value to cout
 String: “The total of 6 and 15 is ”
 Value: value of the expression 6 + 15
 Display produced: The total of 6 and 15 is 21
A First Book of C++ 4th Edition 30
Arithmetic Operators
 Manipulator
 Item used to change how an output stream of
characters is displayed
 endl manipulator causes a newline character ('n') to
be inserted in the display first
 Then forces all current insertions to be displayed
immediately
A First Book of C++ 4th Edition 31
Arithmetic Operators (cont'd.)
 Mixed-mode expression
 Arithmetic expression containing integer and non-integer operands
 Rule for evaluating arithmetic expressions
 Both operands are integers: result is integer
Example : 1 + 2 = 3
 One operand is floating-point: result is floating-point
Example : 1.0 + 2 = 3.0
 Overloaded operator: a symbol that represents more than one
operation
 Execution depends on types of operands
Example : ‘c’ + ‘h’ = ‘ch’ (example in Python programming language)
2 + 3 = 5
A First Book of C++ 4th Edition 32
Expression Types
 Division of two integers yields an integer
 Integers cannot contain a fractional part (i.e., decimal
points) ; results may seem strange
 Example: integer 15 divided by integer 2 produces the
integer result 7 (i.e., 15/2 = 7 and not 7.5)
 Modulus operator (%): captures the remainder
 Also known as the remainder operator
 Example: 9 % 4 is 1 (remainder of 9/4 is 1)
A First Book of C++ 4th Edition 33
Integer Division & Modulus
A First Book of C++ 4th Edition 34
Integer Division
Output:
Program:
 A unary operation that negates (reverses the sign of)
the operand
 Uses same sign as binary subtraction (-)
 Example : -9
A First Book of C++ 4th Edition 35
Negation
 Rules for expressions with multiple operators
 Two binary operators cannot be placed side by side
 Example : x = a + - b
 Parentheses may be used to form groupings
 Example : x = (a+b) * (c-d)
 Expressions within parentheses are evaluated first
 Example: x = a/b + (c * d)  (c * d) evaluated first
A First Book of C++ 4th Edition 36
Operator Precedence and
Associativity
 Rules for expressions with multiple operators
 Sets of parentheses may be enclosed by other parentheses
 Example : x = ( (a+b) * 3 + ( (c/d) – e) )
 Parentheses cannot be used to indicate multiplication
 Example : x = a (b) x = a * b
A First Book of C++ 4th Edition 37
Operator Precedence and
Associativity (cont'd.)
A First Book of C++ 4th Edition 38
Operator Precedence and
Associativity (cont'd.)
 Symbolic names used in place of memory addresses
 Symbolic names are called variables
 These variables refer to memory locations
 The value stored in the variable can be changed
 Simplifies programming effort
 Assignment statement: assigns/gives a value to a variable
 Format: variable name = value assigned/given;
Example: num1 = 45;
A First Book of C++ 4th Edition 39
Variables and Declarations
 Names a variable and specifies its data type
 General form:
dataType variableName;
 Example:
int sum;
- declares sum as variable which stores an integer value
 Declaration statements can be placed anywhere in function
 Typically grouped together and placed immediately after the
function’s opening brace
 Variable must be declared before it can be used
A First Book of C++ 4th Edition 40
Declaration Statements
//PROGRAM 2.3
#include <iostream>
using namespace std;
int main()
{
double grade1; // declare variable grade1 as a double
double grade2; // declare variable grade2 as a double
double total; // declare variable total as a double
double average; // declare variable average as a double
grade1 = 85.5; //assign a value of 85.5 to grade1
grade2 = 97.0; //assign a value of 97.0 to grade2
total = grade1 + grade2; //add grade1 and grade2, store in total
average = total/2.0; //divide total with 2.0, store in average
cout << "The average grade is " << average << endl;
system("Pause");
return 0;
}
A First Book of C++ 4th Edition 41
Declaration Statements
Declaration Statements (cont'd.)
A First Book of C++ 4th Edition 42
 Variables with the same data type can be grouped together and
declared in one statement
 Format: dataType variableList;
 Example:
double grade1, grade2, total, average;
 Initialization: using a declaration statement to store a value in a
variable
 Good programming practice is to declare each initialized variable
on a line by itself
 Example:
double grade2 = 93.5; /*initialize grade2
with a default value */
A First Book of C++ 4th Edition 43
Multiple Declarations
 Each data type has its own storage
requirements
 Computer must know variable’s data
type to allocate storage
 Definition statements: declaration
statements used for the purpose of
allocating storage
A First Book of C++ 4th Edition 44
Memory Allocation
A First Book of C++ 4th Edition 45
Memory Allocation (cont'd.)
 Forgetting to declare all variables used in a program
 Attempting to store one data type in a variable
declared for a different type
 Using a variable in an expression before the variable is
assigned a value
 Dividing integer values incorrectly
A First Book of C++ 4th Edition 46
Common Programming Errors
 Mixing data types in the same expression without
clearly understanding the effect produced
 It is best not to mix data types in an expression unless a
specific result is desired
 Forgetting to separate individual data streams passed
to cout with an insertion (“put to”) symbol
A First Book of C++ 4th Edition 47
Common Programming Errors
(cont'd.)
 Four basic types of data recognized by C++
 Integer, floating-point, character, boolean
 cout object can be used to display all data types
 Every variable in a C++ program must be declared as
the type of variable it can store
A First Book of C++ 4th Edition 48
Summary
 A simple C++ program containing declaration statements has the
format:
#include <iostream>
using namespace std;
int main()
{
declaration statements;
other statements;
return 0;
}
A First Book of C++ 4th Edition 49
Summary (cont'd.)
 Declaration statements: inform the compiler of
function’s valid variable names
 Definition statements: declaration statements that
also cause computer to set aside memory locations
for a variable
 sizeof() operator: determines the amount of
storage reserved for a variable
A First Book of C++ 4th Edition 50
Summary (cont'd.)
 This section explains how numbers are stored in a
computer’s memory and different means of
representing them
A First Book of C++ 4th Edition 51
Chapter Supplement: Bits, Bytes, and
Binary Number Representations
 Bit (Binary Digit)
 A switch that can be open or closed
 Byte
 Group of 8 bits
 Character code (example: ASCII and Unicode)
 Collection of patterns used to represent letters, single digits,
and other characters
 Number codes (binaries representing decimal values)
 Patterns used to store numbers
 Words and addresses (larger units of bytes)
A First Book of C++ 4th Edition 52
Bits and Bytes
A First Book of C++ 4th Edition 53
Binary, Hexadecimal, and Octal
Numbers
0 + 0 + 21 + 0
0 + 0 + 0 + 20
0 + 0 + 21 + 20
0 + 22 + 0 + 0
0 + 22 + 0 + 20
23 + 22 + 21 + 20
A First Book of C++ 4th Edition 54
Binary, Hexadecimal, and Octal
Numbers (cont’d.)

More Related Content

What's hot

Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
01 indirect indexed_addressing_and_arrays_sp15
01 indirect indexed_addressing_and_arrays_sp1501 indirect indexed_addressing_and_arrays_sp15
01 indirect indexed_addressing_and_arrays_sp15John Todora
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Qazi Shahzad Ali
 
C data type format specifier
C data type format specifierC data type format specifier
C data type format specifier
Sandip Sitäulä
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
Rounak Samdadia
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
eteaching
 
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1IIUM
 
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1
IIUM
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
DataminingTools Inc
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in CSahithi Naraparaju
 
Data type
Data typeData type
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
raksharao
 
Vhdl introduction
Vhdl introductionVhdl introduction
Vhdl introduction
VandanaPagar1
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Alexander Podkhalyuzin
 
Octal encoding
Octal encodingOctal encoding
Octal encoding
rajshreemuthiah
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
Chitrank Dixit
 
PYTHON REVISION TOUR - 1
PYTHON REVISION TOUR - 1PYTHON REVISION TOUR - 1
PYTHON REVISION TOUR - 1
AyushGupta976
 

What's hot (19)

Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
01 indirect indexed_addressing_and_arrays_sp15
01 indirect indexed_addressing_and_arrays_sp1501 indirect indexed_addressing_and_arrays_sp15
01 indirect indexed_addressing_and_arrays_sp15
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
C data type format specifier
C data type format specifierC data type format specifier
C data type format specifier
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1
 
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Data type
Data typeData type
Data type
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
 
Vhdl introduction
Vhdl introductionVhdl introduction
Vhdl introduction
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
 
Octal encoding
Octal encodingOctal encoding
Octal encoding
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 
PYTHON REVISION TOUR - 1
PYTHON REVISION TOUR - 1PYTHON REVISION TOUR - 1
PYTHON REVISION TOUR - 1
 

Similar to Csc1100 lecture02 ch02-datatype_declaration

02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
Manzoor ALam
 
C material
C materialC material
C material
tarique472
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
C++
C++C++
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
CHAPTER-2.ppt
CHAPTER-2.pptCHAPTER-2.ppt
CHAPTER-2.ppt
Tekle12
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Prof. Dr. K. Adisesha
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
Soran University
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
Terry Yoast
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
ssusera0bb35
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
AqeelAbbas94
 
C programming part2
C programming part2C programming part2
C programming part2
Keroles karam khalil
 
C programming part2
C programming part2C programming part2
C programming part2
Keroles karam khalil
 
C programming part2
C programming part2C programming part2
C programming part2
Keroles karam khalil
 
C++11 and 64-bit Issues
C++11 and 64-bit IssuesC++11 and 64-bit Issues
C++11 and 64-bit Issues
Andrey Karpov
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.ppt
masadjie
 
C++.ppt
C++.pptC++.ppt
C++.ppt
shweta210
 
9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt
LokeshK66
 

Similar to Csc1100 lecture02 ch02-datatype_declaration (20)

02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
C material
C materialC material
C material
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
 
C++
C++C++
C++
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
CHAPTER-2.ppt
CHAPTER-2.pptCHAPTER-2.ppt
CHAPTER-2.ppt
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
 
C programming part2
C programming part2C programming part2
C programming part2
 
C programming part2
C programming part2C programming part2
C programming part2
 
C programming part2
C programming part2C programming part2
C programming part2
 
C++11 and 64-bit Issues
C++11 and 64-bit IssuesC++11 and 64-bit Issues
C++11 and 64-bit Issues
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.ppt
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt
 

More from IIUM

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
IIUM
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
IIUM
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
IIUM
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
IIUM
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
IIUM
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
IIUM
 
Group p1
Group p1Group p1
Group p1
IIUM
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
IIUM
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
IIUM
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
IIUM
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
IIUM
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
IIUM
 
Heaps
HeapsHeaps
Heaps
IIUM
 
Report format
Report formatReport format
Report format
IIUM
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
IIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
IIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
IIUM
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
IIUM
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
IIUM
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
IIUM
 

More from IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Heaps
HeapsHeaps
Heaps
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 

Csc1100 lecture02 ch02-datatype_declaration

  • 1. A First Book of C++ Chapter 2 Data Types, Declarations, and Displays
  • 2.  In this chapter, you will learn about:  Data Types  Arithmetic Operators  Variables and Declarations  Common Programming Errors  Bits, Bytes, and Binary Number Representations A First Book of C++ 4th Edition 2 Objectives
  • 3.  The objective of all programs is to process data  Data is classified into specific types  Numerical  Alphabetical  Audio  Video  C++ allows only certain operations to be performed on certain types of data  Prevents inappropriate programming operations A First Book of C++ 4th Edition 3 Data Types
  • 4.  Data type  Set of values and operations that can be applied to a particular data  Example of a data type: Integer  The values: set of all Integer numbers (whole number, no decimal points)  The operations: familiar mathematical and comparison operators A First Book of C++ 4th Edition 4 Data Types (cont'd.)
  • 5.  Class data type  Programmer-created data type  Set of acceptable values and operations defined by a programmer using C++ code  Built-in data type  Provided as an integral part of C++  Also known as a primitive type  Requires no external code  Consists of basic numerical types  Majority of operations are symbols (e.g. +, -, *, …) A First Book of C++ 4th Edition 5 Data Types (cont'd.)
  • 6. A First Book of C++ 4th Edition 6 Numerical Data Types
  • 7.  Literal  Value explicitly identifies itself (what you see is what you get)  The numbers 2, 3.6, and –8.2 are literals  Values are literally displayed  The text “Hello World!” is a literal  Text itself is displayed  Literals also known as literal values and constants A First Book of C++ 4th Edition 7 Data Types (cont'd.)
  • 8.  C++ provides nine built-in integer data types :  bool, char, short int, int, long int unsigned char, unsigned short int, unsigned int, unsigned long int  Three most important  int  char  bool  Reason for remaining types is historical  Originally provided for special situations  Difference among types based on storage requirements A First Book of C++ 4th Edition 8 Integer Data Types
  • 9.  int data type  Set of values supported are whole numbers  Whole numbers mathematically known as integers  Explicit signs allowed  Commas, decimal points, and special signs not allowed  Examples of int:  Valid: 0 5 -10 +25 1000 253 -26351 +36  Invalid: $255.62 2,523 3. 6,243,982 1,492.89 A First Book of C++ 4th Edition 9 Integer Data Types (cont’d.)
  • 10.  Different compilers have different internal limits on the largest and smallest values that can be stored in each data type  Most common allocation for int is 4 bytes (range: -2,147,483,648 to 2,147,483,647) A First Book of C++ 4th Edition 10 Integer Data Types (cont’d.) smallest possible integer stored largest possible integer stored
  • 11.  char data type  Used to store single characters  Letters of the alphabet (upper- and lowercase)  Digits 0 through 9  Special symbols such as + $ . , - !  Single character value: letter, digit, or special character enclosed in single quotes  Examples ‘A’ ‘$’ ‘b’ ‘7’ ‘y’ ‘!’ ‘M’ ‘q’ A First Book of C++ 4th Edition 11 Integer Data Types (cont’d.)
  • 12.  Character values stored in ASCII or Unicode codes  ASCII: American Standard Code for Information Interchange  Provides English language-based character set plus codes for printer and display control  Each character code contained in 1 byte (1 byte = 8 bits)  256 distinct codes A First Book of C++ 4th Edition 12 Integer Data Types (cont’d.)
  • 13. ASCII Code A First Book of C++ 4th Edition 13
  • 14. ASCII Code A First Book of C++ 4th Edition 14
  • 15.  Unicode: provides other language character sets  Each character contained in 2 bytes  Can represent 65,536 characters  First 256 Unicode codes have same numerical value as the 256 ASCII codes A First Book of C++ 4th Edition 15 Integer Data Types (cont’d.)
  • 16. Unicode A First Book of C++ 4th Edition 16
  • 17. Unicode A First Book of C++ 4th Edition 17
  • 18.  The escape character  Backslash ( )  Special meaning in C++  Placed in front of a group of characters, it tells the compiler to escape from normal interpretation of these characters  Escape sequence: combination of a backslash and specific characters  Example: newline escape sequence, n A First Book of C++ 4th Edition 18 Integer Data Types (cont’d.)
  • 19. A First Book of C++ 4th Edition 19
  • 20.  bool data type  Represents boolean (logical) data  Restricted to true or false values  Often used when a program must examine a specific condition  If condition is true, the program takes one action; if false, it takes another action  Boolean data type uses an integer storage code A First Book of C++ 4th Edition 20 Integer Data Types (cont’d.)
  • 21. Example Boolean A First Book of C++ 4th Edition 21
  • 22.  C++ makes it possible to see how values are stored  sizeof()  Provides the number of bytes required to store a value for any data type  Built-in operator that does not use an arithmetic symbol A First Book of C++ 4th Edition 22 Determining Storage Size
  • 23.  Signed data type: stores negative, positive, and zero values  Unsigned data type: stores positive and zero values  Provides a range of positive values double that of unsigned counterparts  Some applications only use unsigned data types  Example: date applications in form yearmonthday A First Book of C++ 4th Edition 23 Determining Storage Size (cont’d.)
  • 24. A First Book of C++ 4th Edition 24 Determining Storage Size (cont'd.)
  • 25. A First Book of C++ 4th Edition 25 Determining Storage Size (cont'd.) Type Storage Absolute Range Float 4 bytes 1.40129846432481707 x 10-45 to 3.40282346638528860 x 10-45 double and long double 8 bytes 4.94065645841246544 x 10-45 to 1.79769313486231570 x 10-45
  • 26.  The number zero or any positive or negative number that contains a decimal point  Also called real number  Examples: +10.625 5. -6.2 3521.92 0.0  5. and 0.0 are floating-point, but same values without a decimal (5, 0) would be integers  C++ supports three floating-point types  float, double, long double  Different storage requirements for each A First Book of C++ 4th Edition 26 Floating-Point Types
  • 27.  Most compilers use twice the amount of storage for doubles as for floats  Allows a double to have approximately twice the precision of a float  A float value is sometimes referred to as a single- precision number  A double value is sometimes referred to as a double- precision number A First Book of C++ 4th Edition 27 Floating-Point Types (cont'd.)
  • 28.  Floating-point numbers can be written in exponential notation  Similar to scientific notation  Used to express very large and very small values in compact form A First Book of C++ 4th Edition 28 Exponential Notation
  • 29.  Binary operators: require two operands  Can be a literal or an identifier  Binary arithmetic expression:  literalValue operator literalValue A First Book of C++ 4th Edition 29 Arithmetic Operations
  • 30.  cout allows for display of result of a numerical expression  Display is on standard output device  Example: cout << “The sum of 6 and 15 is ” << (6 + 15)  Statement sends string and value to cout  String: “The total of 6 and 15 is ”  Value: value of the expression 6 + 15  Display produced: The total of 6 and 15 is 21 A First Book of C++ 4th Edition 30 Arithmetic Operators
  • 31.  Manipulator  Item used to change how an output stream of characters is displayed  endl manipulator causes a newline character ('n') to be inserted in the display first  Then forces all current insertions to be displayed immediately A First Book of C++ 4th Edition 31 Arithmetic Operators (cont'd.)
  • 32.  Mixed-mode expression  Arithmetic expression containing integer and non-integer operands  Rule for evaluating arithmetic expressions  Both operands are integers: result is integer Example : 1 + 2 = 3  One operand is floating-point: result is floating-point Example : 1.0 + 2 = 3.0  Overloaded operator: a symbol that represents more than one operation  Execution depends on types of operands Example : ‘c’ + ‘h’ = ‘ch’ (example in Python programming language) 2 + 3 = 5 A First Book of C++ 4th Edition 32 Expression Types
  • 33.  Division of two integers yields an integer  Integers cannot contain a fractional part (i.e., decimal points) ; results may seem strange  Example: integer 15 divided by integer 2 produces the integer result 7 (i.e., 15/2 = 7 and not 7.5)  Modulus operator (%): captures the remainder  Also known as the remainder operator  Example: 9 % 4 is 1 (remainder of 9/4 is 1) A First Book of C++ 4th Edition 33 Integer Division & Modulus
  • 34. A First Book of C++ 4th Edition 34 Integer Division Output: Program:
  • 35.  A unary operation that negates (reverses the sign of) the operand  Uses same sign as binary subtraction (-)  Example : -9 A First Book of C++ 4th Edition 35 Negation
  • 36.  Rules for expressions with multiple operators  Two binary operators cannot be placed side by side  Example : x = a + - b  Parentheses may be used to form groupings  Example : x = (a+b) * (c-d)  Expressions within parentheses are evaluated first  Example: x = a/b + (c * d)  (c * d) evaluated first A First Book of C++ 4th Edition 36 Operator Precedence and Associativity
  • 37.  Rules for expressions with multiple operators  Sets of parentheses may be enclosed by other parentheses  Example : x = ( (a+b) * 3 + ( (c/d) – e) )  Parentheses cannot be used to indicate multiplication  Example : x = a (b) x = a * b A First Book of C++ 4th Edition 37 Operator Precedence and Associativity (cont'd.)
  • 38. A First Book of C++ 4th Edition 38 Operator Precedence and Associativity (cont'd.)
  • 39.  Symbolic names used in place of memory addresses  Symbolic names are called variables  These variables refer to memory locations  The value stored in the variable can be changed  Simplifies programming effort  Assignment statement: assigns/gives a value to a variable  Format: variable name = value assigned/given; Example: num1 = 45; A First Book of C++ 4th Edition 39 Variables and Declarations
  • 40.  Names a variable and specifies its data type  General form: dataType variableName;  Example: int sum; - declares sum as variable which stores an integer value  Declaration statements can be placed anywhere in function  Typically grouped together and placed immediately after the function’s opening brace  Variable must be declared before it can be used A First Book of C++ 4th Edition 40 Declaration Statements
  • 41. //PROGRAM 2.3 #include <iostream> using namespace std; int main() { double grade1; // declare variable grade1 as a double double grade2; // declare variable grade2 as a double double total; // declare variable total as a double double average; // declare variable average as a double grade1 = 85.5; //assign a value of 85.5 to grade1 grade2 = 97.0; //assign a value of 97.0 to grade2 total = grade1 + grade2; //add grade1 and grade2, store in total average = total/2.0; //divide total with 2.0, store in average cout << "The average grade is " << average << endl; system("Pause"); return 0; } A First Book of C++ 4th Edition 41 Declaration Statements
  • 42. Declaration Statements (cont'd.) A First Book of C++ 4th Edition 42
  • 43.  Variables with the same data type can be grouped together and declared in one statement  Format: dataType variableList;  Example: double grade1, grade2, total, average;  Initialization: using a declaration statement to store a value in a variable  Good programming practice is to declare each initialized variable on a line by itself  Example: double grade2 = 93.5; /*initialize grade2 with a default value */ A First Book of C++ 4th Edition 43 Multiple Declarations
  • 44.  Each data type has its own storage requirements  Computer must know variable’s data type to allocate storage  Definition statements: declaration statements used for the purpose of allocating storage A First Book of C++ 4th Edition 44 Memory Allocation
  • 45. A First Book of C++ 4th Edition 45 Memory Allocation (cont'd.)
  • 46.  Forgetting to declare all variables used in a program  Attempting to store one data type in a variable declared for a different type  Using a variable in an expression before the variable is assigned a value  Dividing integer values incorrectly A First Book of C++ 4th Edition 46 Common Programming Errors
  • 47.  Mixing data types in the same expression without clearly understanding the effect produced  It is best not to mix data types in an expression unless a specific result is desired  Forgetting to separate individual data streams passed to cout with an insertion (“put to”) symbol A First Book of C++ 4th Edition 47 Common Programming Errors (cont'd.)
  • 48.  Four basic types of data recognized by C++  Integer, floating-point, character, boolean  cout object can be used to display all data types  Every variable in a C++ program must be declared as the type of variable it can store A First Book of C++ 4th Edition 48 Summary
  • 49.  A simple C++ program containing declaration statements has the format: #include <iostream> using namespace std; int main() { declaration statements; other statements; return 0; } A First Book of C++ 4th Edition 49 Summary (cont'd.)
  • 50.  Declaration statements: inform the compiler of function’s valid variable names  Definition statements: declaration statements that also cause computer to set aside memory locations for a variable  sizeof() operator: determines the amount of storage reserved for a variable A First Book of C++ 4th Edition 50 Summary (cont'd.)
  • 51.  This section explains how numbers are stored in a computer’s memory and different means of representing them A First Book of C++ 4th Edition 51 Chapter Supplement: Bits, Bytes, and Binary Number Representations
  • 52.  Bit (Binary Digit)  A switch that can be open or closed  Byte  Group of 8 bits  Character code (example: ASCII and Unicode)  Collection of patterns used to represent letters, single digits, and other characters  Number codes (binaries representing decimal values)  Patterns used to store numbers  Words and addresses (larger units of bytes) A First Book of C++ 4th Edition 52 Bits and Bytes
  • 53. A First Book of C++ 4th Edition 53 Binary, Hexadecimal, and Octal Numbers 0 + 0 + 21 + 0 0 + 0 + 0 + 20 0 + 0 + 21 + 20 0 + 22 + 0 + 0 0 + 22 + 0 + 20 23 + 22 + 21 + 20
  • 54. A First Book of C++ 4th Edition 54 Binary, Hexadecimal, and Octal Numbers (cont’d.)