CSC201
Week 3 - FUNDAMENTAL DATA TYPES
Contents
Numeric Data Type
o Integral types
o Floating – point types
Lab Excises
CSC2311
COMPUTER PROGRAMMING I
(C++ PROGRAMMING LANGUAGE)
11
CSC201
FUNDAMENTAL DATA TYPES
NUMERIC DATA TYPES
From the point of view of computer programmers, there are two main types of numbers. The
classification of numbers in to these two categories is based on whether they can have only
integer values or also fractional part in addition to the integer part. For example, the number 123
has only integer portion. However, the number 12.35 has an integer portion (i.e 12) and also
fractional portion (i.e 35) after decimal point.
Numbers that can not have any fractional part are called an integer. Number that can have
fractional part are called floating – point numbers.
Integers are used for counting discrete values. For example, we say that, there are 5 balls in a bag
or 10 biscuits in a pack. But we never say that, there are 2.8 balls or 5.9 biscuits.
On the other hand, floating point numbers are used approximately measure something. For
example, we say that, the height of a person is 5.7 feet or a weight of some metal is 5.20
kilograms.
This distinct between integers and floating point numbers is significant from the perspective of
representing such numbers inside computers. When we know that integer can not have fractional
part. Why set aside extra (unused) space for the fractional part. Instead, we can utilize only the
number of the bits that suffice the storage of integers. Also, the mathematics of floating point
numbers is more complicated then that of integers. So, it is advisable to treat these numbers as
distinct and have them stored and processed as such.
Two kinds of numeric types are common to all programming languages: integral types and
floating – point types.
The term “floating – point” refers to the scientific notation that is used for rational numbers. For
example, 123.4567 can also be represented as 1.234567 x 10^2, and 0.000123 as 1.23 x 10^-4.
The alternative are obtained by letting the decimal point to “float” among the digits and using the
exponent on 10 to count how many places it has floated to the left or right.
11
CSC201
Standard C++ has 14 different fundamental types: 11 integral types and 3 floating point types.
The integral type includes:
Boolean type: bool
Enumeration type: enum
The three character types
1. Char
2. Unsigned
3. Wchar_t
The six explicit integer types
1. Short
2. Int
3. Long
4. Unsigned short
5. Unsigned int
6. Unsigned long
The floating – point types includes
Float
Double
Long double
The most frequently used fundamental types are bool, char, int, and double.
11
CSC201
The Boolean Type
The Boolean type is an integral type whose variables can have only one of the two values: false
or true. The values are stored as the integer 0 and 1 respectively. The Boolean type in standard
C++ is named bool.
Example:
#include<iostream>
int main(){
Bool flag = false;
cout<< “flag =” <<flag<<endl;
Flag = true;
cout<< “flag=” <<flag<<endl;
Enumeration Types
In addition to the predefined types such int and char, C++ allows you to define your own special
data types. This can be done in several ways. The most powerful of which use classes, we
consider here much simpler kind of user – define type an enumeration type is an integral type
that is defined by the user with the syntax:
Enum typename {enumeration – list}
Here enum, is a C++ keyword, typename stands for an identifier that names the type of being
defined, and enumerator – list stands for a list of names for integer constants. For example, the
following defines the enumeration type semester, specifying the three possible value that a
variable of that type can have.
Enum semester{winter, monsoon, summer};
We then declare variables of these types:
Semester s1, s2;
11
CSC201
And we can use those variables and those type values as we would with predefined types:
S1 = monsoon;
S2 = winter;
If (s1 == s2) cout<<”same semester.”<<endl;
The actual values defined in the enumerator – lists are called enumerators. In fact, they are
ordinary integer constants. For example, the enumerator winter, monsoon, and summer that are
defined for the same semester type above would have been defined like this:
Const int = 0;
Const int = 1;
Const int = 2;
The values 0,1,… are assigned automatically when the type is defined. Thes default values can
be overridden in the enumerator – list:
Enum coin{paisa, five_paisa, ten_paisa};
If integer values are assigned to only some of the enumerators, then the ones that follow are
given consecutive values. For example,
enum month {jan = 1, feb, mar, april, may, jun, jul, aug, sept, oct, nov, dec};
Will assigned the values 1 through 12 to the twelve months.
Since enumerators are simply integer constants, it is legal to have several different enumerators
with the same values. For example:
enum answer{No = 0, False = 0, Yes = 1, True = 1, O.k =1};
This would allow the code,
Answer answer;
cin>>answer;
11
CSC201
If (answer ==yes) cout<<”you said it was Ok”<<endl;
To work as expected. If the values of the variable answer is Yes or Ok (both of which equal 1),
then the condition will be true and the output will occur. Note that this selection statement could
also be written.
If (answer) cout<<”you have said it was Ok.”<<endl;
The Character Type
The character type is an integral type whose variables represent characters like the letter ‘A’ or
the digit ‘8’. Character literals are delimited by the apostrophe (‘).
11
CSC201
Example:
#include<iostream>
int main(){
char c = ‘A’;
cout<<”c =”<<c<< “, int(c) =”<<int(c)<<endl;
c = ‘t’;
cout<< “c =”<<c<< “, int(c) =”<<int(c)<<endl;
c = ‘!’;
cout<< “c=”<< “, int(c) =”<<int(c)<<endl;
Since character values are used for input and output, they appear in their character form instead
of their integral form: the character ‘A’ is printed as the letter “A”, not as the 65 which is its
internal representation. The type cast operator int(c) is used here to reveal the corresponding
integral value. These are the character’s ASCII codes.
Integer Types
There are six integer types in standard C++; these types actually have several names. For
example, short is also named short int, and int is also unsigned int.
You can determine the numerical ranges of the integer types on your system by running the
program in the following example:
#include<iostream>
#include<climits>
int main()
{//prints some of the constants stored in the <climits> headerfile:
11
CSC201
cout<< “minimum short =”<<SHRT_MIN<<endl;
cout<< “maximum short =”<<SHRT_MAX<<endl;
cout<< “minimum unsigned short = 0”<<endl;
cout<< “maximum unsigned short =”<<USHRT_MAX<<endl;
cout<< “minimum int=”<<INT_MIN<<endl;
cout<< “maximum int=”<<INT_Max<<endl;
cout<< “minimum unsigned int=0”<<endl;
cout<< “maximum unsigned int=”<<UINT_MAX<<endl;
cout<< “minimum long =”<<LONG_MIN<<endl;
cout<< “maximum long =”<<LONG_MAX<<endl;
cout<< “minimum unsigned long =0”<<endl;
cout<< “maximum unsigned long =”<<ULONG_MAX<<endl;
The headerfile <climits> defines the constants SHRT_MIN, SHRT_MAX, USHRT_MIN, etc.
these are the limits on the range of values of the indicated type can have.
11
CSC201
Floating – Point Types
C++ supports three real numbers types: float, double, and long double on the most systems,
double uses twice as many bytes as float, typically, float uses 4 bytes, double uses 8 bytes, and
long double uses 8, 10, 12, or 16 bytes.
Types that use for real numbers are called “floating – point” types because of the way they are
stored internally in the computer.
123.45 = 1111011.01110011 x 2^7
Then the point is “floated” so that all the bits are on its right in this example, the float – point
form is obtained by floating the point 7 bits to the left, providing the mantissa 2^7 times smaller.
So the original number is
123.45 = 0.1111011011100112 x 2^7
This number would be represented internally by storing the mantissa 111101101110011 and the
exponent 7 separately. For a 32 – bits float type, the mantissa is stored in 23 – segment and the
exponent in an 8 – bits segment, leaving 1 bit for the sign of the number. For a 64 – bits double
type, the mantissa is stored in a 52 – bits segment and the exponent in an 11 – segment.
Example:
#include<iostream>
int main()
{ //tests the floating – point operators +, -, *, and /:
double x = 55.0;
double y = 20.0;
cout<< “x=”<<x<< “and y =”<<y<<endl;
cout<< “x+y=”<<(x+y)<<endl;
cout<< “x-y=”<<(x-y)<<endl;
11
CSC201
cout<< “x*y=”<<(x*y)<<endl;
cout<< “x/y=”<<(x/y)<<endl;
11
CSC201
Lab Exercises
1. Write a program that store the detail of your bank account. Declare Name of the bank,
Account Name, Account Type, Account Number and Current Balance as your variables,
assign values and display them on the screen.
2. Modify the program in 1.) Above to receive keyboard input.
11