The main feature of the C++ is that you need to define everything before using it. C++ Variables are the names assigned to the memory locations where different type of data is stored. Data types define the type of the data to be stored in the memory locations. The variable are defined in this way
Type Variable;
or
Type Variable=initialization;
There are seven major types of data which are known as primitive C++ data types.
- Integer
- Float
- char
- double
- bool
- void
- wchar_t
These data bypes are defined by keywords which are given below
Type | Keyword |
---|---|
Integer | int |
Character | char |
Floating point | float |
Double Floating point | double |
Boolean | bool |
Valueless | void |
Wide character | wchar_t |
Integer Data Type:
Integer data types define the variables which can process integers. In fact defining the integer types of variables
define memory locations where only integer types of data can be stored. These are defined by the word “int”.
e.g. int a; int a, x, abc, int FirstNumber, int _seondNm=44;
These data types can be extended by using different modifiers e.g. singed, unsigned, long, short,
Float Data Type:
Floating type of data is defined by the word “float”. All the fractional numbers eighter signed or signed are defiend by the floating data type. This is single precision data type.
The examples are float x, float y, float z=-5.44;
Double Data Type:
This is double precision data type. it is defined by the keyword double e.g. double xy,
Character Data Type:
This type of data defines the characters by using keyword char. The example is char x, myname=’imran kanjoo’;
Boolean Data Type:
The Boolean type of data is defined by the keyword “bool” which hold the Boolean values i.e. true or false.
Void Type:
These are actually valueless data types. These are defined by the word void.
wchar_t:
This type of data variables are double character wide.
The memory space reserved by these data types are given in the following table
Type | Typical Bit Width | Typical Range |
---|---|---|
char | 1byte | -128 to 127 or 0 to 255 |
unsigned char | 1byte | 0 to 255 |
signed char | 1byte | -128 to 127 |
int | 4bytes | -2147483648 to 2147483647 |
unsigned int | 4bytes | 0 to 4294967295 |
signed int | 4bytes | -2147483648 to 2147483647 |
short int | 2bytes | -32768 to 32767 |
unsigned short int | 2bytes | 0 to 65,535 |
signed short int | 2bytes | -32768 to 32767 |
long int | 8bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
signed long int | 8bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long int | 8bytes | 0 to 18,446,744,073,709,551,615 |
float | 4bytes | +/- 3.4e +/- 38 (~7 digits) |
double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
long double | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
wchar_t | 2 or 4 bytes | 1 wide character |
Size of the Variable
The size of the variables depend upon the compiler and the computer or system using and therefore can very. The built in function sizeof can be used to check the sizes of the various data types of the systems in use.
The following example shows how to use the function sizeof.
#include <iostream> using namespace std; int main() { cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of char : " << sizeof(char) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; return 0; }
In the above example the operator << is used to display the output. The endl has been used to end the line.
The output of the above code will be similar to the given below:
typedef Declarations:
There are many situations when we need to give new name to the existing data types. It is just for our clarity and the new names works in the same manner as pre-defined. The syntax to define new name for the existing data type is
typedef type newname;
For example to give new name to the float we can write
typedef float meters;
Enumerated Types:
Enumerated type defines an optional type name and a set of identifiers zero or more. This set can be used as values of the define type. If it has zero identifier then it has no values. To create the enumeration types we use the following general syntax.
enum enum-name { list of optional-names } var-list;
The following example clarifies how to define enums
enum fruit { apple, orange, banana, melon } f; f = orange;
by default the value of first name in the list is 0 and the value of further names increment by one from the preceding values i.e. the value of apple is 0 and the value of banana is 2. But we can change these values and assign new values as well. For example we need to assign 6 to the banana then we can write as
enum fruit { apple, orange, banana=6, melon } f;
In this case the value of banana will be 6 and the value of melon will be 7.