Integer variable in C4 min read

As we already learned about different data types in C language. In this article, we will learn about how the integer variable in c will store data in the memory. And some do’s and don’t of it with suitable examples.

The size distribution of an Integer variable

Size of a variable will varies on different platforms,

Basic data typesData types with type qualifiersSize (bytes)Range
charsigned char1-128 to 127
unsigned char10 to 255
intint or signed int2- 32768 to 32767
unsigned int20 to 65535
short int or signed short int1-128 to 127
unsigned short int10 to 255
long int or signed long int4-2147483648 to 2147483647
unsigned long int40 to 4294967295
floatfloat43.4E-38 to 3.4E+38
doubledouble81.7E-308 to 1.7E+308
long double103.4E-4932 to 1.1E+4932

But in my system size of an Integer is 4bytes. So, let’s break it and see how this variable is stored and its range “4 bytes means what?” “How much data can be stored?”

Integer variable in c size distribution
Size sepration of unsigned and signed int

Example 1

Let’s check the variable how it reacts if by mistake we try to store out of range value.

To check it we start with 1000000 (1 mega OR 1 x 10^6) value and multiply it by 100 two times. What we want as the final output is

1000000
100000000
10000000000

Code:

#include <stdio.h>

int main(void)
{
     unsigned int E = 1000000;
     
     printf("E = %d \n", E);

     E = E*100;
     printf("E = %d \n", E);

     E = E*100;
     printf("E = %d \n", E);

     return 0;
}

Output:

E = 1000000 
E = 100000000 
E = 1410065408
Example 1 output

Explanation:

As we seen unsigned int is having max capacity to store 4Gbytes range of number.

Technically as we want output like it should be 100000000 (1*10^8) x 100 = 10000000000 (1*10^10)

But last output we got is E=1410065408 is undefined (Garbage value) because value went out of range.

Value should be under 4*10^9 (4 Giga Bytes) for unsigned int variable.

Any Solution?

Yes, Let’s use Long int


Size distribution of Long Integer

Size of a long integer variable is 8Bytes. It is very easy to breakout the size because we already calculated for 4Bytes.

long integer size distribution 3
unsigned long int size

Example 2

Let’s use “unsigned long integer” instead of “unsigned integer”.

Code:

#include <stdio.h>

int main(void)
{
     unsigned long int E = 1000000;
     
     printf("E = %d \n", E);

     E = E*100;
     printf("E = %d \n", E);

     E = E*100;
     printf("E = %d \n", E);

     return 0;
}

Output:

E = 1000000 
E = 100000000 
E = 1410065408
example 2 output

Explanation:

Here we used long int and forgot to change formate specifier from %d to %lu.

example 2 warnings

If Compiler is smart enough it will throw warnings with some suggestion

So, while printf function is fetching data from the memory and it has fetched only 4Bytes because of %d.

NOTE: If formate specifier is wrongly used then output we get is also wrong.

We already learned about different data type and its formate specifiers.

Let’s correct the formate specifiers.


Example 3

Code:

#include <stdio.h>

int main(void)
{
     unsigned long int E = 1000000;
     
     printf("E = %lu \n", E);

     E = E*100;
     printf("E = %lu \n", E);

     E = E*100;
     printf("E = %lu \n", E);

     return 0;
}

Output:

E = 1000000 
E = 100000000 
E = 10000000000 
example 3 output

Explanation:

In this example we corrected formate specifier then compiled; we got desired result.


Conclusion

We learned,

  1. Select data type as per the need of the program.
  2. Use relevant Formate specifier.
  3. Warnings are annoying but work on their suggestions.

Still, if you are not having any Linux based system check out this chrome extension.

“Problems are not stop signs, they are guidelines.” – Robert H. Shuller

Leave a Comment