How data is stored in memory in C Language

PREVIOUS                                                                                                                          NEXT


C  Programming Concepts


How data is stored in memory in C Language
The data in the memory stored in the memory in Big Endian or Little Endian.
Let in a program we assign a value to a character variable like this
main()
{
    char a;
    a=127;
    printf("a=%d",a);
}

when the program runs it will show the result on the screen  a=127
Look carefully

main()
{
    char a;
    a=128;
    printf("a=%d",a);
}

when the program runs it will show the result on the screen  a= -128
This happens for every data types. When range of a data type exceeds its range its value automatically falls into the other range.
Now why and how this is happening ........

we know that 1 byte=8bit ( processor dependent) 

Every data in the computer memory is stored in binary format i.e in 1 and 0. when we are declaring a character data type variable the computer first allocate 1 byte i.e 8 bit of memory for the variable. If it is declared as signed char or char then 1 bit of memory out of 8 bit is reserved for sign i.e for '+' or for '-' and the rest are used for storing for data. If it is declared as unsigned char then no bit is reversed for sign bit, all are used for storing data. 0 is for + sign and 1 is for - sign.

Before we go ahead we will see the binary conversion of a decimal digit.

Decimal            Binary Conversion
   128        =         1000 0000

Let us explain this from the below example

main()
{
      char a;
      a=128;
}
When we declare a variable the compiler first reserve one byte of memory for this variable. Then it stores the value i.e 128 in the memory in binary format i.e 1000 0000. Like this

1
0
0
0
0
0
0
0


        sign bit

main()
{
      char a;
      a=128;
      printf(" a = %d",a);
}

The out put in the screen is a = -128.
when the program retrieve the data from the memory first it looks for the sign bit and it finds 1 is present in the sign bit. So it is a negative number. The computer stores and retrieves the negative number in two's complement.

What is one's complement and two's complement?
 one's complement of 128
 The binary conversion of 128 is  1000 0000
one's complement of 128 is         0111 1111
just alter 1 as 0 and 0 as 1
two's complement of 128 is         1000 0000
just add 1 in one's complement of 128, it is a binary addition


Now when the computer finds 1 in sign bit and so it is a negative number it retrieves the data from the memory as two's complement of  7 bit of the variable a except the sign bit.
The 7 bit of 128 stored in the memory is 000 0000
one's complement of 7 bit is                    111 1111
two's complement of 7 bit is                  1000 0000 i.e 128
and since it is a negative number so the output is a = -128

Let us explain another example

main()
{
      char a;
      a= -129;
}

When we declare a variable the compiler first reserves one byte of memory for this variable. Since it is a negative number the value is stored in the memory in two's complete of 129.
The binary conversion of 129 is   1000 0001
one's complement of 129 is         0111 1110
just alter 1 as 0 and 0 as 1
two's complement of 129 is         0111 1111
just add 1 in one's complement of 129, it is a binary addition.

This two's complement of 129 is stored in the memory 

0
1
1
1
1
1
1
1


    sign bit


main()
{
      char a;
      a=-129;
      printf(" a = %d",a);

}

The out put in the screen is a = 127
when the program retrieve the data from the memory first it looks for the sign bit and it finds 0 is present in the sign bit. So it is a positive number. So no two's complement is required. It retrieves the data from the memory and print over the screen.
The decimal equivalent of 111 1111 =127
In integer type variable the data stored in same principle. But the storing procedure of floating point is different.

PREVIOUS                                                                                                                NEXT

No comments:

Post a Comment