Prashant | Sun, 14 Jun, 2020 | 115
Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:
Following are the examples of some very common data types used in C:
Different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.
|Data Types||Memory (Bytes)||Range||Format Specifier|
|short int||2||-32,768 to 32,767||%hd|
|unisgned short int||2||0 to 65,535||%hu|
|unsigned int||4||0 to 4,294,967,295||%u|
|int||4||-2,147,483,648 to 2,147,483,647||%d|
|long int||8||-2,147,483,648 to 2,147,483,647||%ld|
|unsigned long int||8||0 to 4,294,967,295||%lu|
|long long int||8||-(2^63) to (2^63)-1||%lld|
|unsigned long int||8||0 to 18,446,744,073,709,551,615||%llu|
|signed char||1||-128 to 127||%c|
|unsigned char||1||0 to 255||%c|
char represents character, it can be used to declare a character type variable, constant in C language. It takes only one byte (8 bits) in the computer memory and it stores a single character. The value range of char data type is -128 to +127.
'char' variable declaration
char gender; /* to store gender*/
'char' constant declaration
const char IS_MARRIED= 'N'; /*to store, marital status as "No"*/
int represents integer, it can be used to declare an integer type variable, constant in C language. It takes either 2 bytes (16 bits) or 4 bytes (32 bits) according to compiler architecture in the computer memory. It stores only integer numbers (numbers without precision). The value range of 2 bytes integer is -32,768 to +32,767 and 4 bytes integer is -21474836478 to +21474836477.
'int' variable declaration
int number; /*to store any integer number*/
'int' constant declaration
const int MAX_LENGTH=100; /*to store maximum length as 100*/
float represents single precision float type value, it can be used to declare a float type variable, constant in C language. It takes 4 bytes (32 bits) in the computer memory. The value range of float data type is 1.2E-38 to 3.4E+38 with 6 decimal places.
'float' variable declaration
float weight; /*to store weight in kilogram and gram*/
'float' constant declaration
const float PI=3.14f; /*to store value of PI as 3.14*/
These are the basic/primitive data types in C language, there are other data types too, they are the enhanced version of these data types, by using some qualifiers we can change their storage capacity and value range, some of the most useful qualifiers are:
Consider the given declaration
Here, value is the signed char type variable and it can store value between -128 to +127, but if we have only positive values we can define the value as unsigned char.
unsigned defines that variable will store only positive values and the value range of unsigned type variable is: 0 to ((MAX_RANGE*2)+1).
unsigned char - 0 to 255
unsigned int - 0 to 65,535 (for 2 bytes integer)
unsigned int - 0 to 4294967295 (for 4 bytes integer)
short can also be written as short int, it takes 2 bytes (16 bits) in the computer memory and its value range is -32,768 to +32767 (which is similar to 2 bytes int data type).
short can also be used as unsigned short, in that case the value range will be 0 to 65,535.
long can be used with int and double data types, here is the sizes that variable will take in the computer memory.
According to 32 bits compiler architecture
long int - 8 bytes
long double - 16 bytes