Integers in C

In C programming, int keyword is used to declare or define integer variables. The integers in C are whole numbers, that means it can hold all the values zero, positive and negative except decimal values.

Let’s take an example :-

#include<stdio.h>
#include<conio.h>

int main( )
{
        int a=10;
        printf("%d\n", a);
        
        //changing value of a
        a = -2;
        printf("%d\n", a);
        
        // changing value of a
        a = -2;
        printf("%d\n", a);

        getch();
        return 0;

}

Then, the output of the following code will be :-

10
-2
0

But if we put real numbers( decimal values) to the integers, let’s see what happens ?

#include<stdio.h>
#include<conio.h>

int main()
{
        int a=12.3;

        printf("%d", a);

        getch();
        return 0;
}

The above program will not raise any kind of error. If you assign a value of any numeric type (integer, floating-point) to a variable of another numeric type, the value is implicitly converted to the target type.

There are two qualifiers  that can be applied to the integers in C for providing different length of integers.

Integers in C types

short integers

A short integer can represent a whole number that may take less storage, while having a smaller range, compared with a standard integer on the same machine.

In C language, it is denoted by a short keyword. It is required to be at least 16 bits, and is often smaller than a standard integer, but this is not required.

For example :-

#include<stdio.h>
#include<conio.h>

int main()
{
  short int x=10;
  printf("%d", sizeof(x));  

  //the size of operator return size ofvariable can hold

  getch();
  return 0;
}

The output of the above code for gcc compiler :-

2

long integers

A long integer can represent a whole number that may take more storage, whose range is greater (sometimes even double) than that of the standard data type integer In C language, it is denoted by a long keyword.

There are some literals, which are recommended to programmers to use with long - ‘l’ and ‘L’. Although both lower case `l' and upper case `L' are allowed as suffixes for long integers, it is strongly recommended to always use `L'.

The the output of the following code :-

These short and long integers are further divided into two categories, which are :-

Integers in C, short and long

Signed Integers in C

Signed variables  of int ( either short or long) can hold positive as well as negative values. Let’s understand this through an example :-

#include <stdio.h>
#include <stdlib.h>

int main()
{
    signed long int x=12l;
    signed short int r=-23;

    printf("The size of short and long signed integers are-\n%d\n%d",sizeof(r),sizeof(x));

    return 0;
}

The the output of the following program :-

The size of short and long signed integers are-
2
4

The range of short and long  signed integers varies from compiler to compiler in C. But for the 32-bit compiler the range of short and long signed integers can be calculated through the given formula, where n is the number of bits.

signed short integer range

Integers in C are by default a signed type, means they can hold negative , positive and zero.

Unsigned Integers

Unsigned variables are those variables, which can hold zero and positive values only. Let us understand this through an example :-

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned long int x=10;
    unsigned short int r=2;
    printf("%d\n%d",sizeof(r),sizeof(x));

    return 0;
}

The output of the following program will be :-

2
4

The range of short and long  unsigned integers varies from compiler to compiler in C. As we know for the 32 bit compiler, the size of unsigned short integers is 2 and unsigned long integers is 4.

So, the range of short and long unsigned integers can be calculated through the given formula :-

unsigned short integer range