Friday, July 13, 2012

Memory Representation of int data type in C

int may be signed or unsigned both have different memory representation.

1. Memory representation of:
unsigned int a=7;

It is 16-bit data type and all 16 bit is data bit.
Binary equivalent of 7 is: 111
for 16 bit we will add 13 zero in the left side i.e. 00000000 00000111
Since Turbo C is based on 8085 microprocessor which follow little-endian.
Here A is 00000111 and B is 00000000

Memory representation: 
Note: same memory representation will be of:
unsigned short int a=7;
2. Memory representation of:
 
int a=7 or signed int a=7;
 
It is 16 bit data type.
15 bit: data bit
1 bit: signed bit
Binary equivalent of 7 is 111
for 16 bit we will add 13 zero in the left side i.e. 00000000 00000111
Here
A is 00000111
B is 00000000


Memory representation:


Note: same memory representation will be of:



3. Memory representation of:



int a= -7 or signed int a= -7;
It is 16 bit data type.Binary equivalent of 7 is 111 for 16 bit we will add 13 zero in the left side 

i.e. 00000000 00000111 since a is negative number so it will first convert in 
the 2’s complement format before stored in the memory. 1’s Complement of a: 11111111 11111000 + 1 ______________________ 2’s Complement of a: 11111111 11111001 Memory representation

Note: same memory representation will be of:
short int a=-7 or signed short int a=-7;

Note:
Above memory representation is for TURBO C compiler. Both linux gcc and tubro c follow little endianess architecture. But size of data type in linux gcc and turbo c is not same. So memory representation may vary in gcc.

No comments:

Post a Comment