int (1 sign bit + 31 data bits) keyword in C

In C programming language a most common keyword ‘int’ is used to define any positive or negative integer. But there is a difference between an integer and the numbers which can be represented with the help of the keyword ‘int’. Not every integer can be represented with the keyword ‘int’. According to MinGW the size of one ‘int’ is 4 bytes which is equal to 32 bits (1 byte=8 bits). It is still a myth somewhere that ‘int’ can represent an integer or ‘int’ is used to represent integers. Integer is a very vast category of numbers where as one ‘int’ has limited and exact amount of memory (size of ‘int’ is 4 bytes or 32 bits) to store what is being represented by it. An ‘int’ type variable in C language is able to store only numbers till 2147483647. Beyond this number ‘int’ fails to store precisely and even not correctly. ‘int’ is a 32 bit data type. Whenever a number is being assigned to an ‘int’ type variable, it is first converted to its binary representation (that is in 0’s and 1’s) then it is kept in memory at specific location. An ‘int’ is actually 1 sign bit + 31 data bits, that is 31 bits are available for storing the number being assigned to a ‘int’ type variable and 1 bit is reserved for maintaining the sign of the number which is either + or – . The sign is also represented by binary digits, 0 for positive sign and 1 for negative sign.

Let us understand this by an example.
Example – Consider,

int num=2147483647; 

At this point first 2147483647 will be converted into its binary form which is equal to:

1111111111111111111111111111111 is a 31 digit binary number which will be assigned to variable num’s right most 31 bits and the 32nd bit will have a zero(0) as the number being assigned to variable num is a positive number. If we try to store any number greater than 2147483647 into an ‘int’ type variable then we will lose information.

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter