This part of the 'Practical C++' series is meant to teach you about the various data types existing in C++.
- The formal definition of a data type - the description of a set of values and the basic set of operations that can be applied to it. In C++, there are two kinds of data types :-
- Fundamental data types - Derived data types
- In this part we will look at only the fundamental data types, which fall under these categories :-
- Integer - Character - Floating point - Boolean - Double Precision
The three most used data types are integer, float and character.
The integer data type is used to represent integer whole numbers from -32,768 to +32,767. A variable of integer type can be declared as :-
int num;
Let us look at a program that adds two numbers and displays their result.
#include<iostream.h>
#include<conio.h></em>
<em>void main()
{
clrscr();
int num1 = 10;
int num2 = 5;
cout<<"Sum = "<<num1+num2<<endl;
getch();
}
The output of this program will be :-
Sum = 15
Take particular notice of the statements between 'clrscr()' and 'getch()'.
Floating point numbers are used to represent numbers which have a decimal point. For example : 3.1, 4.0099 etc.
A floating point variable can be declared as :-
float num;
where 'num' is the name of the variable.
Example : float a = 4.099;
Character data type is used to represent characters, as the name suggests. It is defined as :-
char a = 'X';
char b = '45';
The value of 'b' will be 45, but it won't be treated as an integer, which means you cannot do mathematical operations like addition on it.
References :-
Using C++ by Rob McGregor
C++ : The Complete Reference by Herbert Schildt
Written by Rae (12 January 2005)
Rae is a member of CAU Knowledge-Bank Tutorial Writers
This article was originally published by CyberArmy.net in the CyberArmy Library.
|