There are four types of data type in C language. They are as follows:
| Types | Data Types |
|---|---|
| Basic data types | int, char, float, double, boolean |
| User Defined data type | structure, union, enum |
| Derived data type | Pointer, array, function |
Basic Data Types In C Language
Integer Data Type
Integer data ( example 1,2,3,4,5,6, ….. etc. ) is stored in int ,short and long data type.
| Type | Storage size | Value range |
|---|---|---|
| int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
| unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
| short | 2 bytes | -32,768 to 32,767 |
| unsigned short | 2 bytes | 0 to 65,535 |
| long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| unsigned long | 4 bytes | 0 to 4,294,967,295 |
Example: Addition of integer numbers
#includeusing namespace std; int main() { int x = 10; // initialize integer variable x to 10 int y=25; // initialize integer variable y to 25 int z = x + y; // add the values of variable x &y and store in variable z cout<<"Sum of x+y = " << z; }
OUTPUT
Character Data TypeCharacter data ( example ‘a’ , ‘b’ , ‘f’, ….. etc. ) is stored in “char” data type.
| Type | Storage size | Value range |
|---|---|---|
| char | 1 byte | -128 to 127 or 0 to 255 |
| unsigned char | 1 byte | 0 to 255 |
| signed char | 1 byte | -128 to 127 |
Example: Declare and initialize integer variable.
#includeusing namespace std; int main() { char c1 = 'A'; // initialize character variable c1 to A char c2 = 66; // ASCII value of 66 is B, in c2 character B will store cout << "value of variable c1 &c2 =" << c1 <<" "<< c2; return 0; }
OUTPUT
value of variable c1 &c2 =A B
Float Data Type
Floating point value (example 11.23, 333.3330 etc. ) can be stored in data type float and double. Small floating value can be stored in float and large floating stored in double.
| Type | Storage size | Value range | Precision |
| float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places |
| double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places |
Example: Declare and initialize float & double variable.
#includeusing namespace std; int main() { float x = 10.00; // initialize float variable x to 10.00 float y = 25.23; // initialize float variable y to 25.23 float z = x + y; // add the values of variable x &y and store in variable z cout << "Sum of x+y = " << z; double d = 11.234; // initialize double variable d to 11.234 cout << "\n d= " << d; // \n is used to move in a new line }
OUTPUT
Sum of x+y = 35.23 d= 11.234