In this post, we are going to learn about what is C Data Types, how the values are stored in the memory, and how much memory is allocated for different values and different C Data Types in detail.
What is a Variable?
A variable name is a name given to the memory location, in which the values are stored. The value stored in the memory location may vary, hence given the name variable.
For example, the variable is declared as int i=10,
where int is the datatype, and i is the variable name given to the memory location which has the value 10.
C Data Types
The C Data Types are used to declare the variables and functions of the program, which determines how much memory space is allocated for each variable.
Now let us see the different datatypes in C
Primary Datatypes – integer, char, float
Derived Datatypes – Arrays, Pointers, Structures, Union
Enumeration Datatypes – enum
Void Datatypes – void
Datatypes with their range and size
The following table shows the value of different datatypes with their range and size
Data Types | Memory size in Bytes | Range |
---|---|---|
char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
short | 2 | -32,768 to 32,767 |
unsigned short | 2 | 0 to 65,535 |
int | 2 or 4 | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 | 0 to 65,535 or 4,294,967,295 |
long | 8 | -9223372036854775808 to 922332036854775807 |
unsigned long | 8 | 0 to 18446744073709551615 |
float | 4 | 1.2E-38 to 3.4E+38 6 decimal places |
long | 8 | 2.3E-308 to 1.7E+308 15 decimal places |
Double | 10 | 3.4E-4932 to 1.1E+4932 19 decimal places |
Where 1byte=8bits
The value of an unsigned integer is calculated using the formula
0 to (2^n)-1 where n- number of bits
1 1 1 1 1 1 1 1 -> (255)10
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
similarly for 2bytes
1111 1111 1111 1111 -> (65535)10
for 4 bytes
1111 1111 1111 1111 1111 1111 1111 1111 ->(4294967295)10
The value of a signed integer is calculated using the formula
(-2^(n-1)-1) to (2^(n-1) -1) where n – number of bits.
The integer number can be either a positive or negative number, it is represented by the Most Significant Bit(MSB). The value of MSB is 0, for a positive number, 1 for a negative number