# Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs.

Firstly, we can learn the range of int:

The int is the integer data types which ranges 2 bytes in their internal memory allocation. In the C programming language, the Data types, Range, Size plays the crucial role in terms of memory. We have the sizeof() operator that allocates the certain number of the bits and the bytes are required for storing the value in their specific memory form.

C language specifies the perfectly or exactly minimum size of the storage for the integer value form. Consider an example for the data types for allocating the bytes for default. In general, let's see some data types which allocation of their memory.

• short data type occupies 2 bytes of the memory storage.
• int data type occupies 2 bytes of the memory storage.
• char data type occupies 1 byte of the memory storage.
• long data type occupies 8 bytes of the memory storage.

The system compiler can understand the given range of the data type and execute the program successfully.

We have the equation or the formula for determining the range over the data types is: -(2N-1) to (2N-1) - 1

In the above formula the N is the size of the data type and it will be multiplied with the total number of the bits which we used in the integer data type.

For unsigned integer value we have the formula as:

0 to (2N-1) +( (2N-1) - 1)

Examples

Let's see an example programs for easier way to understand

Example 1:

``````#include <stdio.h>  // preprocessor
void printUnsignedRange(int bytes)  // void function
{
int bits = 8 * bytes;
unsigned long long to = (1LLU << (bits - 1)) + ((1LL << (bits - 1)) - 1);;   // assigning the range for unsigned data types
printf(" 0 to %llu\n\n", to);   // printing values
}
void printSignedRange(int bytes)  // void function
{
int bits = 8 * bytes;
long longfrom  = -(1LL << (bits - 1));  // assigning range for long long from value
long long to    =  (1LL << (bits - 1)) - 1;  // assigning range for long long to value
printf(" %lld to %lld\n\n", from, to);  // printing values
}
int main()  // main function
{
printf("Range of int =");  // printing the range of integer data type
printSignedRange(sizeof(int));
printf("Range of unsigned int ="); // printing the range of unsigned integer       data type
printUnsignedRange(sizeof(unsigned int));
printf("The Range of the char is ="); // printing the range of char data type       printSignedRange(sizeof(char));
printf(" The Range of the unsigned char is =");  // printing the range of unsigned char data type
printUnsignedRange(sizeof(unsigned char));
printf("The Range of the long is ="); // printing the range of long data type
printSignedRange(sizeof(long));
printf("The Range of the unsigned long is =");  // printing range of unsigned long data type.
printUnsignedRange(sizeof(unsigned long));
printf("The Range of the short is =");  // printing the range of short data type printSignedRange(sizeof(short));
printf("The Range of the unsigned short is =");  // printing range of unsigned short data type.
printUnsignedRange(sizeof(unsigned short));
printf("The Range of the long long is=");  // printing the range of long long data type
printSignedRange(sizeof(long long));
printf("The Range of the unsigned long long is=");  // printing range of unsigned long long data type.
printUnsignedRange(sizeof(unsigned long long));
return 0;  // returning value
}
``````

Output:

Example 2:

``````#include <stdio.h>
int main()
{
printf("The sizeof(short) data type is = %d bytes\n",sizeof(short));
printf("The sizeof(int) data type is = %d bytes\n",sizeof(int));
printf("The sizeof(unsigned int) data type is = %d bytes\n",sizeof(unsigned int));
printf("The sizeof(long) data type is = %d bytes\n",sizeof(long));
return 0;
}
``````

Output:

In the above example we used the sizeof() operator to represent the each of the individual data type size in terms of the bytes.

Example 3:

``````#include <stdio.h>
int main() {
int a;
printf("\nEnter an integer value : ");
scanf("%d", &a);
if(a >=0 && a <= 50)
{
printf("The Range varies from [0, 50]\n");
}
else if(a >=49 && a <= 99)
{
printf("Range (51,100]\n");
}
else if(a >=100 && a <= 160)
{
printf("The Range varies from (75,125]\n");
}
else if(a >101 && a <= 180) {
printf("The Range varies from (101,180]\n");
}
else
{
printf("The range is outside\n");
}
return 0;
}
``````

Output: