C++ Supports a variety of Data types and programmer can select the appropriate type as per the need of application. Data Type is generally tells or specifies the type of data that we are going to store in the variable or Type of value a function will return when called at RunTime.
C++ Supports following Data Types :-
1. User Defined
2. Primitive / Built-in
3. Derived
1. Primitive / Built-In DataTypes
1.1 Integer Type
Integer is basic datatype . For Example int. We can assign any value which has no fraction part to int type. But int has a limitation . This takes 2 Bytes space i.e 16 bit.
That means in int type variable the range of value is 2^15 to (2^15 -1 )i.e. -32768 to 32767.
We can assign a value between the above range to int type variable If we assign a value out of above range that gives a wrong answer
Integer are of following Types in C++ :-
a) int
b) Unsigned int
c) Short int
d) Unsigned Int
e) signed int
f) signed short int
g) long int
h) signed long int
i) Unsigned long int
Type
|
Byte
|
Range
|
Int
|
2
|
-32768 to 32767
|
Unsigned int
|
2
|
0 to 65535
|
Signed int
|
2
|
-32768 to 32767
|
Unsigned int
|
2
|
-32768 to 32767
|
Unsigned short int
|
2
|
0 to 65535
|
Signed short int
|
2
|
-32768 to 32767
|
Long int
|
4
|
-2147483648 to 2147483647
|
Signed long int
|
4
|
-2147483648 to 2147483647
|
Unsigned long int
|
4
|
0 to 429496677295
|
In above type the difference is in space and range of value which can assign them .
If there is a Unsigned word that mean in that we can not store negative value
1.2 Floating Type
Floating Type data are the numbers which consists fracional part also. In Floating Type. The data can be represented in mantissa and exponenet form.Example :- Following are valid floating type data
29.7
4556.45
901 E 10 i.e. ( 902 * 10^10 )
78 e -5 i.e. ( 78 * 10^ -5 )
Following type data are valid floating type in C++ --
a) Float
b) double
c) long double
Type
|
Byte
|
Range
|
float
|
4
|
3.4 E-38 to 3.4E + 38
|
double
|
8
|
1-7 E-308 to 1.7 E +308
|
Long double
|
10
|
3.4 E-4932 to 1.1 E + 4932
|
Variable can be declared as floating type by :-
float x , y ; // x and y are floating type variables
double c ; // c is double type
long double a , b ; // a and b are long double type
1.3 Character Type
A Character variable is declared using the type specifier char . Char range is -128 to + 127. Character variables are declared by following way.Char ch ;
Here, ch is a variable which is character-type
we can assign single character to ch. This can be done via their ASCII value or through character constant
Example :-
ch = 'A'
or
ch = 65 ( 65 is ASCII value of A )
2. Derived Data Type
The various derived datatype are :-* Arrays
* Pointers
* Functions
( i ) Arrays :- An Array is collection of homogenous data elements i.e. collection of related data items with similar data type and having a common name. They are used to represent a group of related data items.
An Array is sequence of objects of Objects all of which have the same type. The Objects are called elements of array and numbered consecutively 0,1,2,3,4----n . These numbers are called index or value or subscripts of Array. An arrays would be written with subscripts Ao ,A1 ,A2 ,A3 ......The elements of Array are A[0], A[1], A[2], A[3].
E.g :- int RollNo [40] ;
char Name [20] ;
( ii ) Pointer :- Pointer is a variable that contains the address of another variable in memory. The address is the location of variable. If First variable contains the address of second, Then first points to second.
When a variable is declared , Three fundamental attributes are associated with it. These are name, type and its address in memory. e.g. int X;
It associates with name X, type int and address of some location in memory where X is to be stored. Suppose the address is Ox41FFFFA Then X like this -
Ox41FFFFA
X |------------------------|
|------------------------|
int
If X = 22
Ox41FFFFA
X |------------------------|
| 22 |
|------------------------|
int
A Refrence is An Allias , a Synonym for another variable . It is declared by using the reference operator and appended to the refrences type . e.g.
int &r = x ; // r is refrence for x
The reference operator & return the memory address of variable to which it is applied. We can also store address in another variable . The type of variable that stores an address is called Pointer
3. User Defined Data Type :- The various user defined data types are :-
* Class
* Structure
* Union
( i ) Class :- A class is a group of subjects with same attributes and common behaviour . Objects consist of same type and constitute a class e.g. Scooter, Motor cycle, Car etc. are all members of same class vehicle. Objects are variables of type class. Once a class is definecd , you can create number of obects
( ii ) Structure :- A structure is a method of grouping data of different data types. A structure may contain integer elements , character elements and floating point elements . These elements are called members. The unique feature of structure is that , even though the items grouped together are of different data types but still they are stored in contigous memory location.
Structure is declared as :-
structure employee {
char Name [50] ;
int Age ;
char sex [5] ;
float salary ;
};
Here the name of structure is employee. It contains four members i.e. Name ( char type ), Age ( int type ), Sex ( int type ) , Salary ( float type ).
( iii ) Union :- Union is similar to structure . A structure contains members of different data types and each member has its own memory location , where as the members with in a union all share the same storagearea within computer memory. Whereas in case of a structure each member is assigned its unique storage area The union are used to conserve memory. Unions are applied for applications, involving multiple members, where values need not be assigned to all of the members at one time. When a Union is declared compiler automatically allocates memory location to hold their largest data type of members In Union .
union employee {
int Age ;
char Code [9] ;
};
Here the name of union is employee. It contains Two members i.e. Age ( int type ), Code ( char type ).
0 Comment to "Data Types in C++"
Post a Comment