×

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

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

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...

3 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

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.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

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.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.