The following table shows the symbols names corresponding to the limits for each signed integer type.
Type | Lower limit | Upper limit |
---|---|---|
char | CHAR_MIN | CHAR_MAX |
short | SHRT_MIN | SHRT_MAX |
int | INT_MIN | INT_MAX |
long | LONG_MIN | LONG_MAX |
long long | LLONG_MIN | LLONG_MAX |
The lower limits for the unsigned integer types are all 0, so there are no symbols for these.
The symbols corresponding to the upper limits for the unsigned integer types are UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, and ULLONG_MAX.
To be able to use any of these symbols in a program, you must have an #include directive for the limits.h header file in the source file:
#include
You could initialize a variable with the maximum possible value for type int like this:
int number = INT_MAX;
This statement sets the value of number to be the maximum possible, whatever that may be for the compiler used to compile the code.
The following table lists Symbols Representing Range Limits for Floating-Point Types
Type | Lower limit | Upper limit |
---|---|---|
float | FLT_MIN | FLT_MAX |
double | DBL_MIN | DBL_MAX |
long double | LDBL_MIN | LDBL_MAX |
This program outputs the values corresponding to the symbols defined in the header files.
#include <stdio.h> // For command line input and output
#include <limits.h> // For limits on integer types
#include <float.h> // For limits on floating-point types
/*w w w .j a v a 2s.c o m*/
int main(void)
{
printf("Variables of type char store values from %d to %d\n", CHAR_MIN, CHAR_MAX);
printf("Variables of type unsigned char store values from 0 to %u\n", UCHAR_MAX);
printf("Variables of type short store values from %d to %d\n", SHRT_MIN, SHRT_MAX);
printf("Variables of type unsigned short store values from 0 to %u\n", USHRT_MAX);
printf("Variables of type int store values from %d to %d\n", INT_MIN, INT_MAX);
printf("Variables of type unsigned int store values from 0 to %u\n", UINT_MAX);
printf("Variables of type long store values from %ld to %ld\n", LONG_MIN, LONG_MAX);
printf("Variables of type unsigned long store values from 0 to %lu\n", ULONG_MAX);
printf("Variables of type long long store values from %lld to %lld\n", LLONG_MIN, LLONG_MAX);
printf("Variables of type unsigned long long store values from 0 to %llu\n", ULLONG_MAX);
printf("\nThe size of the smallest positive non-zero value of type float is %.3e\n", FLT_MIN);
printf("The size of the largest value of type float is %.3e\n", FLT_MAX);
printf("The size of the smallest non-zero value of type double is %.3e\n", DBL_MIN);
printf("The size of the largest value of type double is %.3e\n", DBL_MAX);
printf("The size of the smallest non-zero value of type long double is %.3Le\n", LDBL_MIN);
printf("The size of the largest value of type long double is %.3Le\n", LDBL_MAX);
printf("\n Variables of type float provide %u decimal digits precision. \n", FLT_DIG);
printf("Variables of type double provide %u decimal digits precision. \n", DBL_DIG);
printf("Variables of type long double provide %u decimal digits precision. \n",
LDBL_DIG);
return 0;
}
The code above generates the following result.
You can find out how many bytes are occupied by a given type by using the sizeof operator.
The expression sizeof(int) will result in the number of bytes occupied by a variable of type int, and the result is an integer of type size_t.
Type size_t is defined in the standard header file stddef.h, and will correspond to one of the basic integer types.
You could store a value that results from applying the sizeof operator:
size_t size = sizeof(long long);
This program will output the number of bytes occupied by each numeric type:
#include <stdio.h>
// ww w . j a v a 2s .co m
int main(void)
{
printf("Variables of type char occupy %u bytes\n", sizeof(char));
printf("Variables of type short occupy %u bytes\n", sizeof(short));
printf("Variables of type int occupy %u bytes\n", sizeof(int));
printf("Variables of type long occupy %u bytes\n", sizeof(long));
printf("Variables of type long long occupy %u bytes\n", sizeof(long long));
printf("Variables of type float occupy %u bytes\n", sizeof(float));
printf("Variables of type double occupy %u bytes\n", sizeof(double));
printf("Variables of type long double occupy %u bytes\n", sizeof(long double));
return 0;
}
The code above generates the following result.