1
Programming with C++
Lecture 3: Fundamental Data types
Sikander Naseem
2
Numerical Data Types
Integer data types is used Integral type
for discreet numbers with • Boolean Type (bool)
no decimal or fractional • Enummeration Type (enum)
part. • Char Type (char, unsigned
char, wchar_t)
Floating point data types
• Integer type (short, int,
can store floating point long, unsigned short,
numbers having decimal unsigned int, unsigned
part. long)
So why not using Floating Floating-point type
point types for almost all • float
data types? • Double
• long double
3
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)
A simple numerical calculation program.
7
Increment and Decrement Operators
Integral object’s values can be incremented or
decremented with operators ++ and – respectively
“Pre” version e.g. ++i, and “post” version e.g. i++
int main()
{int m,n;
m=44;
n=++m; //Using "pre" version increment
cout<<"m ="<<m<<" and n="<<n<<endl;
m=44;
n=m++; //Using "post" version increment
cout<<"m ="<<m<<" and n="<<n<<endl;
}
8
Composite assignment operator
standard assignment operator =
In addition C++ has the following composite
assignment operators +=,-=,*=,/= and %=
Each applies the indicated operator to the varialble
9
Composite assignment operator
int main()
{int m=22;
cout<<"m ="<<m<<endl;
m += 8; //adds 8 to m
cout<<"after m += 8, m= "<<m<<endl;
m /= 3; //divide m with 3
cout <<"after m /= 3, m= "<<m<<endl;
m %= 7; //converts m to reminder after diving by 7
cout <<"after m %= 7, m= "<<m<<endl;
}
10
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
Type conversion and truncation
In C++ “lower types” such as int are converted
(promoted) automatically to “higher type” such as
float
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
Type conversion and truncation
But conversion from “higher types” such as float
to “lower type” such as int is not automatic.
This will remove the decimal part (truncation) and
the process is called type casting
int main()
{
float f=120.67;
int m = int(f); // m get the discreet part of f
cout<<"f ="<<f<<endl;
cout <<"m ="<<m<<endl;
}
14
Numerical overflow
The space allocated to a data type is limited
Exceeding the space limit will cause numerical
overflow error
Example
15
Round off error
The rounding off occurs in the decimal point part of a
numerical value after calculation
For instance number 1/3 maybe stored as 0.333333
which is no equal to 1/3.
This round off error accumulates in the next
calculations
E.g. solving quadratic equations. The roots will not
have the exact value.
It is best not use floating point types for equality
comparison in decision making.
16
The Scientific format
Scientific format (E-format) can be used to input
very small floating point number or very large
numbers.
The display can output the values in both format
depending on the output value size.
Example
17
Scope, internal blocks and nesting
{ } represents a block and the beging and end of its
scope
There can internal blocks inside another block.
This process is called nesting
int main()
{ //main function block scope starts here
{//internal function block scope starts here
}//internal function block scope ends here
}//main function block scope ends here
18
Scope, internal blocks and nesting
The scope for instance for variables is the range in which they can
identified by compilers and use.
The scope of the variable starts from the point of declaration to the
end of the {}.
int main()
{ //main function block scope starts here
int x =22;
{//internal function block scope starts here
int x=12;
cout<<"Value of x in internal block ="<<x<<endl;
}//internal function block scope ends here
cout<<"Value of x in main() ="<<x<<endl;
}//main function block scope ends here
19
The End
Thanks for coming
Reference book:
Programming with C++, SCHAUM’s outlines, John Hubbard,
3rd/2nd Edition, McGraw Hill, New Delhi