Thursday, July 10, 2014

Data Types in C

There are 4 basic data types in C namely character, integer, floating point number and double precision floating point number(See Table 1). Character(char) and Integer(int) data types either signed or unsigned. The default for char and int is signed i.e. values are distributed between positive and negative values (See Table 2).

Table 1:
Data Type
C Keyword
Storage size
Character
char
1 bye
Integer
int
2 or 4 bytes
Floating Point
float
4 bytes
Double precession floating point.
double
8 bytes

Table 2:
Type
Storage size
Value range
char
1 byte
 -128 to 127
unsigned char
1 byte
 0 to 255
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

The standard C specification (ANSI C) does not actually specify a fixed range or memory for 
these data types. All it says is something like below: 

      short int <= int <= long int and float <= double <= long double


This means that a short int should assign less than or the same amount of storage as an int and the int should be less or the same bytes as a long int. And the above ranges are actually machine and implementation dependent.

If you want to know the actual size allocated based on your machine, then you can use the sizeof operator. See the following examples:

int main()
{
    printf("the size of char is %d bytes\n", sizeof(char));
    printf("the size of short is %d bytes\n", sizeof(short));
    printf("the size of int is %d bytes\n", sizeof(int));
    printf("the size of long is %d bytes\n", sizeof(long));
    printf("the size of float is %d bytes\n", sizeof(float));
    printf("the size of double is %d bytes\n", sizeof(double));
    printf("the size of long double is %d bytes", sizeof(long double));
    return 0;
}

Now, what is the difference between signed and unsigned number? Most Significant Bit(MSB) of a number will determine whether a number is signed or unsigned.

Suppose, a 8-bit Unsigned Number represented as follows:

MSB






LSB
b7
b6
b5
b4
b3
b2
b1
b0
27
26
25
24
23
22
21
20
128
64
32
16
8
4
2
1

Where, each bit from b0 to b7 contains either 0 or 1. So there are 256 different numbers from 0 to 255 and the value of a numbers calculated as follows:

     number = 128×b+ 64×b+ 32×b+ 16×b+ 8×b+ 4×b+ 2×b+ 1×b0

For example, if a byte contain 10010001b then the value of a number is 
(128×1+0+0+16×1+0+0+0+1×1) = 145d. See the following table 3 for more examples:

Table 4: 8-bit Unsigned Numbers


Binary
Equivalent Decimal
0000 0000
0+0+0+0+0+0+0+0 = 0
1010 0011
128+0+32+0+0+0+2+1 = 163
0101 0100
0+64+0+16+0+4+0+0 = 84
1010 1101
128+0+32+0+8+4+0+1 = 173
1111 1111
128+64+32+16+8+4+2+1 = 255

In case of a signed number, MSB bit is a sign bit. So for a 8-bit signed number, there are 256 different numbers from -128 to 127.

MSB






LSB
b7
b6
b5
b4
b3
b2
b1
b0
27
26
25
24
23
22
21
20
-128
64
32
16
8
4
2
1

And the value of a 8-bit signed number calculated as follows:

     number = -128×b+ 64×b+ 32×b+ 16×b+ 8×b+ 4×b+ 2×b+ 1×b0

For Example, if a byte contain 10010001b then the value of a signed number is 
(-128×1+0+0+16×1+0+0+0+1×1) = (-128+16+1) = -111d. See the following table for more examples:

Table 4: 8-bit Signed Numbers

Binary
Equivalent Decimal
1000 0000
-128+0+0+0+0+0+0+0 = -128
1010 0011
-128+0+32+0+0+0+2+1 = -93
0101 0100
0+64+0+16+0+4+0+0 = 84
1010 1101
-128+0+32+0+8+4+0+1 = -83
0111 1111
0+64+32+16+8+4+2+1 = 127

Similarly, there are 65536 different 16-bit numbers; 0 to 65535 unsigned numbers or -32768 to 32767 signed numbers.

Unsigned 16-bit numbers calculated and represented as follows:

     number = 32768×b15 + 16384×b14 + 8192×b13 + 4096×b12 + 2048×b11 + 1024×b10
                     + 512×b9 + 256×b128×b+ 64×b+ 32×b+ 16×b+ 8×b+ 4×b
                     + 2×b+ 1×b0

MSB














LSB
b15
b14
b13
b12
b11
b10
b9
b8
b7
b6
b5
b4
b3
b2
b1
b0
215
214
213
212
211
210
29
28
27
26
25
24
23
22
21
20
32768
16384
8192
4096
2048
1024
512
256
128
64
32
16
8
4
2
1

Examples: 16-bit Unsigned Numbers

Binary
Equivalent Decimal
0000000000000000
0+0+0+0+0+0+0+0 = 0
1100101010100011
32768+16384+0+0+2048+0+512+0+128+0+32+0+0+0+2+1 = 51875
0000010101001100
0+0+0+0+0+1024+0+256+0+64+0+0+8+4+0+0 = 1365
1010110010010100
32768+0+8192+0+2048+1024+0+0+128+0+0+16+0+4+0+0 = 44180
1111111111111111
32768+16384+8192+4096+2048+1024+512+256+128+64+32+16+8+4+2+1 = 65535

Signed 16-bit numbers calculated and represented as follows:

     number = -32768×b15 + 16384×b14 + 8192×b13 + 4096×b12 + 2048×b11 + 1024×b10
                     + 512×b9 + 256×b128×b+ 64×b+ 32×b+ 16×b+ 8×b+ 4×b
                     + 2×b+ 1×b0

MSB














LSB
b15
b14
b13
b12
b11
b10
b9
b8
b7
b6
b5
b4
b3
b2
b1
b0
215
214
213
212
211
210
29
28
27
26
25
24
23
22
21
20
-32768
16384
8192
4096
2048
1024
512
256
128
64
32
16
8
4
2
1

Examples: 16-bit Signed Numbers

Binary
Equivalent Decimal
1000000000000000
-32768+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0 = -32768
1100101010100011
-32768+16384+0+0+2048+0+512+0+128+0+32+0+0+0+2+1 = -13661
0000010101001100
0+0+0+0+0+1024+0+256+0+64+0+0+8+4+0+0 = 1356
1010110010010100
-32768+0+8192+0+2048+1024+0+0+128+0+0+16+0+4+0+0 = -21356   
0111111111111111
0+16384+8192+4096+2048+1024+512+256+128+64+32+16+8+4+2+1 = 32767