3.4 – Integer Types in C language Part-2

by subbu on August 29, 2013

int type in C language:

  • It is the fundamental integer type
  • It can be written as short, int or short int
  • It takes 2 bytes or 16 bits of memory.
  • Higher most bit is used to identify the sign, that is 0 for +ve and 1 for -ve.
  • The range of int type is from -215 to +215-1 (-32768 to +32767)
  • %d or %i is used as a formatting character in printf() and scanf() statements
  • %o and %x or %X are used as formatting characters to represent integer in Octal and Hexadecimal systems
  • It is generally used to store small to medium range integer values

Storing upper and lower limit values in int type memory allocations:

#include<stdio.h>
int main()
{
int x,y;
x=32767;//higher limit
y=-32768;//lower limit
printf("x=%d",x);
printf("\ny=%d",y);
return 0;
}

Output:
x=32767
y=-32768

Storing upper limit value:

We know that, while assigning a positive value to any integer type the first bit is always assigned with 0 and remaining bits are used to store the actual value in binary format.

As int type takes 2 bytes (16 bits) the first bit is assigned with 0 and the maximum value that it can store in remaining 15 bits is 111 1111 1111 1111 (32767)

int maximum limit in C language

Storing lower limit value:

While assigning a negative value to any integer type, the 2’s compliment of  binary equal is stored in the memory. The minimum negative that an int type can hold is -32768. The binary equal to 32768 is 1000 0000 0000 0000, the 1’s compliment of which is 0111 111 1111 1111 and the 2’s compliment of which is 1000 0000 0000 0000.

While printing, the printf() treats it as a negative by looking 1 at the first bit, reverts the 2’s and 1’s compliments and Finally prints the decimal equal of resultant binary number with -ve sign.

int minimum limit in C language

int overflow in C language:

Assigning a value, that is more than its positive limit result overflows by wrapping the value towards the negative side.

Now we will see what happens when we assign a value which is more than its positive limit with an example

#include<stdio.h>
int main()
{
int x=32770;
printf("%d",x);
return 0;
}

Output:
-32766

Example explained:

Generally while assigning a positive value the first most bit has to store with 0 and remaining 15 bits have to store the binary equal of value.
Here binary equal of 32770 is a 16 bit value instead 15 bit, hence the sign bit is also  used to store the value.
While printing, the printf() treats it as a negative value by looking 1 at the first bit, so printf() reverts the 2’s and 1’s compliment of binary and prints equal decimal value with -ve sign

int overflow in C language

int underflow in C language:

Assigning a value, that is less than its negative limit result underflows by wrapping the value to the positive side

Now we will see what happens when we assign a value which is less than its negative limit with an example

#include<stdio.h>
int main()
{
int x=-32772;
printf("%d",x);
return 0;
}

Output:
32764

Example explained:

Generally while assigning a negative value the 2’s compliment of binary equal is stored.
Here binary equal of 32772 is 1000 0000 0000 0100, its 1’s  compliment is 0111 1111 1111 1011 and 2’s compliment of which 0111 1111 1111 1100 will be stored in the memory
While printing, the printf() treats it as a positive value by looking 0 at the first bit, so that printf() prints decimal equal of remaining 15 bits as a positive value 32764

int underflow in C language

Summary of overflow and underflow:

Any data type has a range according to the bytes of memory that it takes. Assigning a value that is more than it’s higher limit result wrapping towards negative side called overflow. Assigning a value that is less than it’s lower limit result wrapping towards positive side called underflow.

overflow and underflow cycle

Previous post:

Next post: