C Program to Find the Range of Data Types

Write a C program to find the range of data types using the c library and without it. In C programming, all the range information, such as the minimum and maximum constants, will be within the limits and float header files. The limits.h header file has information about integers and characters. At the same time, the float.h file has the floating point constants. In this c example, we use the limits header file and print the range of integers and characters.

#include <stdio.h>
#include<limits.h>

int main()
{   
    printf("The Range of Signed Characters   = %d to %d\n", SCHAR_MIN, SCHAR_MAX);
    
    printf("The Range of Unsigned Characters = 0 to %d\n", UCHAR_MAX);

    printf("The Range of Signed Integer      = %d to %d\n", INT_MIN, INT_MAX);
    
    printf("The Range of Unsigned Integer    = 0 to %d\n", UINT_MAX);

    printf("The Range of Signed Short Int    = %d to %d\n", SHRT_MIN, SHRT_MAX);
    
    printf("The Range of Unsigned Short Int  = 0 to %d\n", USHRT_MAX);

    printf("The Range of Signed Long         = %ld to %ld\n", LONG_MIN, LONG_MAX);
    
    printf("The Range of Unsigned Long       = 0 to %lu\n", ULONG_MAX);

    return 0;
}
C Program to Find the Range of Data Types

In this c example, we used the float.h header to find the range of floating point data types.

#include <stdio.h>
#include<float.h>

int main()
{   
    printf("The Range of Float       = %e to %e\n", FLT_MIN, FLT_MAX);

    printf("The Range of Double      = %e to %e\n", DBL_MIN, DBL_MAX);

    printf("The Range of Long Double = %Le to %Le\n", LDBL_MIN, LDBL_MAX);

    return 0;
}
The Range of Float       = 1.175494e-38 to 3.402823e+38
The Range of Double      = 2.225074e-308 to 1.797693e+308
The Range of Long Double = 3.362103e-4932 to 1.189731e+4932

In this C program, we will find the range of all the available data types manually by creating functions.

#include <stdio.h>

void signedRange(int bytes)
{
    int bits = 8 * bytes;
    long long min = -(1LL << (bits - 1));
    long long max = ((1LL << (bits - 1)) - 1);
    printf("%lld to %llu\n\n", min, max);
}

void unsignedRange(int bytes)
{
    int bits = 8 * bytes;
    unsigned long long max = (1LLU << (bits - 1)) + ((1LL << (bits - 1)) - 1);
    printf("0 to %llu\n\n", max);

}
int main()
{   
    printf("The Range of Signed Characters   = ");
    signedRange(sizeof(char)); 
    printf("The Range of Unsigned Characters = ");
    unsignedRange(sizeof(unsigned char));

    printf("The Range of Signed Integer      = ");
    signedRange(sizeof(int));
    printf("The Range of Unsigned Integer    = ");
    unsignedRange(sizeof(unsigned int));

    printf("The Range of Signed Short Int    = ");
    signedRange(sizeof(short));
    printf("The Range of Unsigned Short Int  = ");
    unsignedRange(sizeof(unsigned short));

    printf("The Range of Signed Long         = ");
    signedRange(sizeof(long));
    printf("The Range of Unsigned Long       = ");
    unsignedRange(sizeof(unsigned long));

    printf("The Range of Float               = ");
    signedRange(sizeof(float));

    printf("The Range of Double              = " );
    signedRange(sizeof(double));

    printf("The Range of Long Long           = ");
    signedRange(sizeof(long long));

    printf("The Range of Unsigned Long Long  = ");
    unsignedRange(sizeof(unsigned long long));
    return 0;
}
The Range of Signed Characters   = -128 to 127

The Range of Unsigned Characters = 0 to 255

The Range of Signed Integer      = -2147483648 to 2147483647

The Range of Unsigned Integer    = 0 to 4294967295

The Range of Signed Short Int    = -32768 to 32767

The Range of Unsigned Short Int  = 0 to 65535

The Range of Signed Long         = -9223372036854775808 to 9223372036854775807

The Range of Unsigned Long       = 0 to 18446744073709551615

The Range of Float               = -2147483648 to 2147483647

The Range of Double              = -9223372036854775808 to 9223372036854775807

The Range of Long Long           = -9223372036854775808 to 9223372036854775807

The Range of Unsigned Long Long  = 0 to 18446744073709551615