Range of Int in C

In this article, we are going to discuss the range of int in C with programs.

Range of Int

Data types, and their size and range, play an important role in C programming. The sizeof() operator gives the number of bytes required to store a value in memory of a specific form. However, to prevent overflow and underflow errors in programming, we must be aware of a type's range. C specifies the exact minimum storage size for each integer form. For example, short requires at least two bytes, and long requires at least four bytes. The compiler determines the size and range of a data type. As a result, we should not hardcode the size and range values in the program.

Find a range of data types manually without a C library

The given formula defines the minimum and maximum range of a signed type:

  • -(2N-1) to 2N-1 - 1 (Where N is sizeof(type) * 8 (it is the total number of bits used by integer type)).

The given formula defines the minimum and maximum range of an unsigned type:

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

Examples:

Let's take an example to find the range of integers in C programming.

Output: After executing this code, we will get the output as shown below:

Range of int = -2147483648 to 2147483647
Range of unsigned int = 0 to 4294967295
Range of char = -128 to 127
Range of unsigned char = 0 to 255
Range of long = -9223372036854775808 to 9223372036854775807
Range of unsigned long = 0 to 18446744073709551615
Range of short = -32768 to 32767
Range of unsigned short = 0 to 65535
Range of long long = -9223372036854775808 to 9223372036854775807
Range of unsigned long long = 0 to 18446744073709551615

Find the range of data types using a C library

The method described above for obtaining any form of the range is interesting, but it is not recommended for use. Using the power of a pre-defined C library is often recommended.

Limits.h and float.h are two header files in C programming that define minimum and maximum constants. limits.h specifies constants for integer type and character types. Small and maximum size ranges, total bits, and many others. The float.h defines the floating-point numbers.

Example:

Now, let's take an example to understand how we can find the range of integers by using the C library.

Output: After executing this above code, we will get the output as shown below:

The output is:

Range of signed int -2147483648 to 2147483647
Range of unsigned int 0 to 4294967295
Range of signed char -128 to 127
Range of unsigned char 0 to 255
Range of signed long int -9223372036854775808 to 9223372036854775807
Range of unsigned long int 0 to 18446744073709551615
Range of signed short int -32768 to 32767
Range of unsigned short int 0 to 65535
Range of float 1.175494e-38 to 3.402823e+38
Range of double 2.225074e-308 to 1.797693e+308
Range of long double 3.362103e-4932 to 1.189731e+4932

Example:

Write a program that reads an integer type and checks it against the given range to see where it belongs.

Output: After executing this above code, we will get the output as shown below:

The Output is:

Input an integer: 28
Range [26,50]

Example:

The sizeof() operator is used to determine the size of an integer form or some other type. The given below program shows how to use the sizeof() operator to determine the sizes of various integer types in the system.

Output: After executing this above code, we will get the output as shown below:

The output is:

sizeof(short) = 2 bytes
sizeof(int) = 4 bytes
sizeof(unsigned int) = 4 bytes
sizeof(long) = 8 bytes





Latest Courses