Programming With C++ - Lecture3
Programming With C++ - Lecture3
Integral type
Boolean type
• Can have one of the two variables “true” or “false”
• The values are stored as integers 0 and 1 (binary)
• Declared by bool
int main()
{// Prints the values of a boolean variable
bool b=false;
cout <<"the value of b ="<<b<<endl;
b=true;
cout <<"the value of b ="<<b;
}
4
Integral type
Character type
• Character type “char” is and integral type which represents the
letters like “A”, digits like “4”. Should be enclose with ( ‘ ).
• The character are stored as integers of their repective ASCII code,
though they appears as letters “A” and digits “4”.
int main()
{// Prints the ASCII code for the characters
char c1='A', c2='a', c3='\t', c4=':';
cout <<"c1 =" <<c1<<" with ASCII code = "<<int(c1)<<endl;
cout <<"c2 =" <<c2<<" with ASCII code = "<<int(c2)<<endl;
cout <<"c3 =" <<c3<<" with ASCII code = "<<int(c3)<<endl;
cout <<"c4 =" <<c4<<" with ASCII code = "<<int(c4)<<endl;
}
5
Integral type
Integeral type
short, Type Name Bytes Range of Values
int, bool 1 false or true
long, char 1 –128 to 127 by
unsigned short, default
unsigned int, short 2 –32,768 to 32,767
unsigned long unsigned short 2 0 to 65,535
int 4 –2,147,483,648 to
2,147,483,647
unsigned int 4 0 to 4,294,967,295
long 4 –2,147,483,648 to
2,147,483,647
unsigned long 4 0 to 4,294,967,295
6
Arithmetic operators
To perform numerical calculation C++ uses
arithmetic operators such as +,-,*,/ and % (modulus
or remainder operator)
Floating-point type
Floating-point type
• float
• double
• long double
Use to store floating-point numerical values.
Type Name Bytes Range of Values
float 4 3.4E +/- 38 (7 digits)
double 8 1.7E +/- 308 (15 digits)
long double same as double Same as double
11
Floating-point type
All of the above programs for athematic operators,
increment and decrement operators, and
composite assignment operators can be use for
floating-point types also.
Floating-point types can be use for storing discreet
numerical values, but….
Using the right and sufficient type of data is
storage space and access time efficient.
12
int main()
{int m = 10;
float f=3.1443;
f += m; // m is converted to 22.0
cout<<"f ="<<f<<endl;
f +=2; // 2 is converted to 2.0
cout <<"f ="<<f<<endl;
}
13
Numerical overflow
The space allocated to a data type is limited
Exceeding the space limit will cause numerical
overflow error
Example
15
The End
Reference book:
Programming with C++, SCHAUM’s outlines, John Hubbard,
3rd/2nd Edition, McGraw Hill, New Delhi