Week 3 - FUNDAMENTAL DATA TYPES
Department of Computer Science -
Federal University Dutse
GENERAL CLASSIFICATION
NUMERIC
CHARACTERS
Numeric can further be classified into two broad
categories based on whether they can have only
integer values or also fractional part in addition to
the integer part.
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.
On the other hand, floating point numbers are
used approximately measure something.
This distinct between integers and floating
point numbers is significant from the
perspective of representing such numbers
inside computers.
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.
Standard C++ has 14 different fundamental
types: 11 integral types and 3 floating point
types.
The Integral Type
Boolean type: bool
Enumeration type: enum
The three character types
Char
Unsigned
Wchar_t
The six explicit integer types
Short
Int
Long
Unsigned short
Unsigned int
Unsigned long
The Floating – Point Types
float
double
long double
The most frequently used fundamental types
are bool, char, int, and double.
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.
Example
#include<iostream>
int main(){
bool signal= false;
cout<< “signal=” <<signal<<endl;
signal= true;
cout<< “signal=” <<signal<<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 being defined
and enumerator – list stands for a list of
names for integer constants.
Example:
enum semester{first, second, summer};
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
(‘).
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.
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
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.
End of Week 3