We can determine the minimum and maximum possible of values of data types in C by using predefined constants in a program. Here is the code for displaying the ranges of various data types in C:
Output of above program on Ideone compiler is:
Explanation:
The header file limits.h defines the following constants:
#include <stdio.h> #include <limits.h> #include <float.h> int main() { printf("CHAR_BIT : %d\n", CHAR_BIT); printf("CHAR_MAX : %d\n", CHAR_MAX); printf("CHAR_MIN : %d\n", CHAR_MIN); printf("\n"); printf("INT_MAX : %d\n", INT_MAX); printf("INT_MIN : %d\n", INT_MIN); printf("\n"); printf("SHRT_MAX : %d\n", SHRT_MAX); printf("SHRT_MIN : %d\n", SHRT_MIN); printf("\n"); printf("LONG_MAX : %ld\n", (long) LONG_MAX); printf("LONG_MIN : %ld\n", (long) LONG_MIN); printf("\n"); printf("LLONG_MAX : %lld\n", (long long) LLONG_MAX); printf("LLONG_MIN : %lld\n", (long long) LLONG_MIN); printf("\n"); printf("SCHAR_MAX : %d\n", SCHAR_MAX); printf("SCHAR_MIN : %d\n", SCHAR_MIN); printf("\n"); printf("UCHAR_MAX : %d\n", UCHAR_MAX); printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX); printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX); printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX); printf("\n"); printf("FLT_MAX : %g\n", (float) FLT_MAX); printf("FLT_MIN : %g\n", (float) FLT_MIN); printf("-FLT_MAX : %g\n", (float) -FLT_MAX); printf("-FLT_MIN : %g\n", (float) -FLT_MIN); printf("\n"); printf("DBL_MAX : %g\n", (double) DBL_MAX); printf("DBL_MIN : %g\n", (double) DBL_MIN); printf("-DBL_MAX : %g\n", (double) -DBL_MAX); printf("-DBL_MIN : %g\n", (double) -DBL_MIN); return 0; }
Output of above program on Ideone compiler is:
CHAR_BIT : 8 CHAR_MAX : 127 CHAR_MIN : -128 INT_MAX : 2147483647 INT_MIN : -2147483648 SHRT_MAX : 32767 SHRT_MIN : -32768 LONG_MAX : 2147483647 LONG_MIN : -2147483648 LLONG_MAX : 9223372036854775807 LLONG_MIN : -9223372036854775808 SCHAR_MAX : 127 SCHAR_MIN : -128 UCHAR_MAX : 255 UINT_MAX : 4294967295 ULONG_MAX : 4294967295 USHRT_MAX : 65535 FLT_MAX : 3.40282e+38 FLT_MIN : 1.17549e-38 -FLT_MAX : -3.40282e+38 -FLT_MIN : -1.17549e-38 DBL_MAX : 1.79769e+308 DBL_MIN : 2.22507e-308 -DBL_MAX : -1.79769e+308 -DBL_MIN : -2.22507e-308
Explanation:
The header file limits.h defines the following constants:
- CHAR_BIT = number of bits in a char
- 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
- CHAR_MAX = maximum value for a char
- MB_LEN_MAX = maximum multibyte length of a character accross locales
- SHRT_MIN = minimum value for a short
- SHRT_MAX = maximum value for a short
- USHRT_MAX = maximum value for an unsigned short
- 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
- LONG_MAX = maximum value for a long
- ULONG_MAX = maximum value for an unsigned long
- LLONG_MIN = minimum value for a long long
- LLONG_MAX = maximum value for a long long
- ULLONG_MAX = maximum value for an unsigned long long
(Here U*_MIN constants are omitted as any unsigned type has a minimum value of 0.)
- FLT_MIN = min value of a float
- FLT_MAX = max value of a float
- DBL_MIN = min value of a double
- DBL_MAX = max value of a double
- LDBL_MIN = min value of a long double
- LDBL_MAX = max value of a long double