Introduction to fundamental Data types:
Fundamental (atomic) data types are predefined data types available with C++. There are five fundamental data types in C++: char, int, float, double and void. Actually, these are the keywords for defining the data types.
Integers are whole numbers without any fraction. Integers can be positive or negative. Integer data type accepts and returns only integer numbers. If a variable is declared as an int, C++ compiler allows storing only integer values into it. If you try to store a fractional value in an int type variable it will accept only the integer portion of the fractional value..
//Program to receive two integer numbers and display their sum
using namespace std; int main()
int num1, num2, sum;
//variables num1, num2, and sum are declared as integers
cout << "\n Enter Number 1: ";
cin >> num1;
cout << "\n Enter Number 2: "; cin >> num2;
sum = num1 + num2;
cout << "\n The sum of " << num1 << " and " << num2 << " is " << sum;
In the above program, there are three variables declared viz. num1, num2 and sum. All these three variables are declared as integer types. So, three different integer values can be stored in these variables.
The variables num1 and num2 are used to store the values that are obtained from the user during the execution of the program, whereas the variable sum is used to store the processed (resultant) value.
During the execution of the above program, line numbers 7 and 9 prompt the user to Enter number 1 and number 2.
Input stream cin in line 8 and 10 will receive the values given by the user and store the same in variable num1 and num2.
Line 11; both the values of num1 and num2 will be added and the result will be assigned to the variable sum.
Character data type accepts and returns all valid ASCII characters. Character data type is often said to be an integer type, since all the characters are represented in memory by their associated ASCII Codes. If a variable is declared as char, C++ allows storing either a character or an integer value.
using namespace std;
cout << "\n Enter a character: ";
cin >> ch;
ch = ch + 1;
cout << "\n The Next character: " << ch;
Enter a character: A
The Next character: B
In the above program, ch is declared as a char type variable to hold a character. When the user enters a character, it will be stored in the ch variable as an integer value (ie. Equivalent ASCII code).
ch = ch + 1;
In this statement, the value of ch is incremented by 1 and the new value is stored back in the same variable ch. (Remember that, arithmetic operations are carried out only on the numbers not with alphabets).
Another program illustrates how int and char data types are working together.
using namespace std;
int main ()
cout << "\n Enter an ASCII code (0 to 255): ";
cin >> n;
ch = n;
cout << "\n Equivalent Character: " << ch;
Enter an ASCII code (0 to 255): 100
Equivalent Character: d
In the above program, variable n is declared as an int type and another variable ch as a char type. During execution, the program prompts the user to enter an ASCII value. If the user enters an ASCII value as an integer, it will be stored in the variable n. In the statement ch = n; the value of n is assigned into ch. Remember that, ch is a char type variable.
For example, if a user enters 100 as input; initially, 100 is stored in the variable n. In the next statement, the value of n i.e., 100 is assigned to ch. Since, ch is a char type; it shows the corresponding ASCII character as output. (Equivalent ASCII Character for 100 is d).
If a variable is declared as float, all values will be stored as floating point values.
(1) They can represent values between the integers.
(2) They can represent a much greater range of values.
At the same time, floating point operations takes more time to execute compared to the integer type ie., floating point operations are slower than integer operations. This is a disadvantage of floating point operation.
using namespace std;
float r, area;
cout << "\n Enter Radius: ";
cin >> r;
area = 3.14 * r * r;
cout << "\n The Area of the circle is " << area;
Enter Radius: 6.5
The Area of the circle is 132.665
In the above example, two variables r and area are declared as float type in a single statement. Variable r is used to hold radius which is a data provided by the user and area for holding the area of the circle which is obtained through the formula. For example, if the user inputs the radius value as 6.5, then the statement area = 3.14 * r * r; multiplies the value of r twice with 3.14 (value of pi) and the resultant value 132.665 will be kept in the variable area.
In this case, r and area both are same type of variables. So, these two variables are declared in a single statement. Declaring the same variables in a single statement reduces the processing time rather than multiple declarations.
This is for double precision floating point numbers. (precision means significant numbers after decimal point). The double is also used for handling floating point numbers. But, this type occupies double the space than float type. This means, more fractions can be accommodated in double than in float type. The double is larger and slower than type float. double is used in a similar way as that of float data type.
The literal meaning for void is ‘empty space’. Here, in C++, the void data type specifies an empty set of values. It is used as a return type for functions that do not return any value.