C Program to Find the Range of Data Types

In this article, we will write a C program to find the range of different built-in data types. The range of a data type specifies the minimum and the maximum value that can be stored in the variable of the given data type.

Sample Example:

Range of the int data type:  -2147483648 to 2147483647
Range of the char data type:  -128 to 127

What will be the maximum and minimum value that can be stored in a given data type is actually decided by the number of bits that it uses to store the value in the memory.

For example, an int data type mostly uses 4 bytes to store a number. However, this can vary depending on the platform and compiler being used. But for most of the compilers, it is 4 bytes which is equal to 32 bits (4 bytes = 4 * 8 bits = 32 bits).

Here is the formula that you can use to find the minimum and maximum values that can be stored in a given data type.

For signed data types = -2(n-1) to (2(n-1)) – 1
For unsigned data types = 0 to (2n) – 1

Where n is the number of bits.

If you want to find out how many bytes the given data type uses to store a number of that data type in the memory, you can use the sizeof() function. This is a built-in function in C. It returns the total number of the bytes that the given data type uses.

The following C program shows how you can find the range of basic data types in C:

// C Program to find the range of data types
#include <stdio.h>
#include <math.h>

// Function to get signed data types range
void getSignedRange(int bytes){
    int n = bytes * 8;  // Get bits
    long int min = -pow(2, n - 1);
    long int max = pow(2, n - 1) - 1;
    printf("%ld to %ld", min, max);
}

// Function to get unsigned data types range
void getUnsignedRange(int bytes){
    int n = bytes * 8;  // Get bits
    long int min = 0;
    long int max = pow(2, n) - 1;
    printf("%ld to %ld", min, max);
}

int main() {
    
    printf("Range of Signed char: ");
    getSignedRange(sizeof(char));
    
    printf("\nRange of Unsigned char: ");
    getUnsignedRange(sizeof(char));
    
    printf("\nRange of Signed Int: ");
    getSignedRange(sizeof(int));
    
    printf("\nRange of Unsigned int: ");
    getUnsignedRange(sizeof(int));
    
    printf("\nRange of Signed short int: ");
    getSignedRange(sizeof(short int));
    
    printf("\nRange of Unsigned short int: ");
    getUnsignedRange(sizeof(short int));
    
    return 0;
}

Output:

Range of Signed char: -128 to 127
Range of Unsigned char: 0 to 255
Range of Signed Int: -2147483648 to 2147483647
Range of Unsigned int: 0 to 4294967295
Range of Signed short int: -32768 to 32767
Range of Unsigned short int: 0 to 65535

2. Using the <limits.h> Header Library

If you do not want to calculate the range of the data types manually, you can alternatively use the built-in <limits.h> header library.

The <limits.h> header library provides several constants such as INT_MIN, INT_MAX, etc. that you can directly use to get the value that can be stored in a given data type.

Here are some basic constants provided by the <limits.h> header library:

  • CHAR_BIT: Number of bits in a char (usually 8).
  • SCHAR_MIN: Minimum value for a signed char.
  • SCHAR_MAX: Maximum value for a signed char.
  • UCHAR_MAX: Maximum value for an unsigned char.
  • CHAR_MIN: Minimum value for a char (either signed or unsigned).
  • CHAR_MAX: Maximum value for a char (either signed or unsigned).
  • MB_LEN_MAX: Maximum number of bytes in a multibyte character.
  • SHRT_MIN: Minimum value for a short int.
  • SHRT_MAX: Maximum value for a short int.
  • USHRT_MAX: Maximum value for an unsigned short int.
  • INT_MIN: Minimum value for an int.
  • INT_MAX: Maximum value for an int.
  • UINT_MAX: Maximum value for an unsigned int.
  • LONG_MIN: Minimum value for a long int.
  • LONG_MAX: Maximum value for a long int.
  • ULONG_MAX: Maximum value for an unsigned long int.
  • LLONG_MIN: Minimum value for a long long int.
  • LLONG_MAX: Maximum value for a long long int.
  • ULLONG_MAX: Maximum value for an unsigned long long int

You can use these constants directly in your C program to get the maximum or minimum value stored in the respective data type.

See implementation in the following C program:

// C Program to find the range of data types
#include <stdio.h>
#include <limits.h>

int main() {
    
    printf("Range of Signed char: ");
    printf("%d to %d", SCHAR_MIN, SCHAR_MAX);
    
    printf("\nRange of Unsigned char: ");
    printf("%d to %d", 0, UCHAR_MAX);
    
    printf("\nRange of Signed Int: ");
    printf("%d to %d", INT_MIN, INT_MAX);
   
    printf("\nRange of Unsigned int: ");
    printf("%d to %lld", 0, UINT_MAX);
    
    printf("\nRange of Signed short int: ");
    printf("%d to %d", SHRT_MIN, SHRT_MAX);
    
    printf("\nRange of Unsigned short int: ");
    printf("%d to %d", 0, USHRT_MAX);
    
    return 0;
}

Output:

Range of Signed char: -128 to 127
Range of Unsigned char: 0 to 255
Range of Signed Int: -2147483648 to 2147483647
Range of Unsigned int: 0 to 4294967295
Range of Signed short int: -32768 to 32767
Range of Unsigned short int: 0 to 65535

Both approaches give you the same output. You can use any of the two whichever you find convenient.

I hope you will find this article helpful. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

Leave a Comment