1. What is data type in C
Data type specifies what type of data a variable can hold.
There are four category of data type in C language.
They are as follows:
Types | Data Types |
---|---|
Basic (Primitive) data types | int, char, float, double |
Enumeration data type | Enum |
Derived data type | pointer, array, structure, union |
Void data type | void |
2. Primitive Data Types In C Language
There are two types of qualifiers are used with primitive data types
- Size Qualifier – short, long
- Sign Qualifier- signed, unsigned
Size qualifier specifies the size and range of data type short qualifier is used to store less amount of data compared to long qualifier.
Sign qualifier specifies whether a data type can store only positive value or positive and negative both.
By default a data type is signed qualifier.
Size of primitive data type depends on computer system and word size.
So our data type may vary with others.
If you want to check data size of int, char, float double you can use sizeof operator.
Example: How to find size of data types in c language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> int main() { int a = 5; printf("Printing size of variable and data types\n"); printf("size of int %d bytes \n", sizeof(int)); printf("size of short %d bytes \n", sizeof(short)); printf("size of long %d bytes \n", sizeof(long)); printf("size of char %d bytes \n", sizeof(char)); printf("size of float %d bytes \n", sizeof(float)); printf("size of double %d bytes \n", sizeof(double)); printf("size of int variable a %d bytes \n", sizeof(a)); } |
1 2 3 4 5 6 7 8 | Printing size of variable and data types size of int 4 bytes size of short 2 bytes size of long 8 bytes size of char 1 bytes size of float 4 bytes size of double 8 bytes size of int variable a 4 bytes |
Following are details of data types with format specifier in c, range of data types in c.
1 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 | Format Specifier |
---|---|---|---|
int | 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 | %d or %i |
unsigned int | 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 | %u |
short | 2 bytes | -32,768 to 32,767 | %hd |
unsigned short | 2 bytes | 0 to 65,535 | %hu |
long | 4 bytes | -2,147,483,648 to 2,147,483,647 | %ld |
unsigned long | 4 bytes | 0 to 4,294,967,295 | %lu |
Example: Addition of integer number.
1 2 3 4 5 6 7 8 | #include < stdio.h > 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 printf("Sum of x+y = %i", z); } |
Output
1 | Sum of x+y =35 |
Note: Here %i is a format specifier which is used to print integer value.
2. Character Data Type
Character data ( example ‘a’ , ‘b’ , ‘f’, ….. etc. ) is stored in “char” data type.
Type | Storage size | Value range | Format Specifier |
---|---|---|---|
unsigned char | 1 byte | 0 to 255 | %c |
signed char | 1 byte | -128 to 127 | %c |
Example: Declare and initialize character variable.
1 2 3 4 5 6 7 | #include<stdio.h> 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 printf("value of variable c1 & c2 = %c %c", c1,c2); } |
Output
1 | value of variable c1 & c2 = A B |
3. Float and Double Data Types
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 | Format Specifier | Precision |
---|---|---|---|---|
float | 4 byte | 1.2E-38 to 3.4E+38 | %f ,%e or %E for scientific notation | 6 decimal places |
double | 8 byte | 2.3E-308 to 1.7E+308 | %lf | 15 decimal places |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 | %Lf | 19 decimal places |
Example: Declare and initialize float & double variable.
1 2 3 4 5 6 7 8 9 10 11 | #include<stdio.h> 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 printf("Sum of x+y = %f", z); double d = 11.234; // initialize double variable d to 11.234 printf("\n d= %f", d); // \n is used for new line return 0; } |
Output:
1 2 | Sum of x+y = 35.230000 d= 11.234000 |