×

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:

Range of Int in C

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:

Range of Int in C

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:

Range of Int in C

Related Topics

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.