下表显示了与每个有符号整数类型的限制相对应的符号名称。
类型 | 下限 | 上限 |
---|---|---|
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 |
无符号整数类型的下限全部为0,因此没有符号。
对应于无符号整数类型的上限的符号是UCHAR_MAX,USHRT_MAX,UINT_MAX,ULONG_MAX和ULLONG_MAX。
为了能够在程序中使用任何这些符号,您必须在源文件中的limits.h头文件中使用#include指令:
#include
您可以初始化一个类型为int的最大可能值的变量,如下所示:
int number = INT_MAX;
此语句将数值的值设置为尽可能大的值,无论编译器用于编译代码的可能性如何。
下表列出了表示浮点类型的范围限制的符号
类型 | 下限 | 上限 |
---|---|---|
float | FLT_MIN | FLT_MAX |
double | DBL_MIN | DBL_MAX |
long double | LDBL_MIN | LDBL_MAX |
该程序输出与头文件中定义的符号对应的值。
#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
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;
}
上面的代码生成以下结果。
表达式sizeof(int)将导致类型为int的变量占用的字节数,结果是size_t类型的整数。
类型size_t在标准头文件stddef.h中定义,并将对应于基本整数类型之一。
您可以存储应用sizeof运算符产生的值:
size_t size = sizeof(long long);
该程序将输出每个数字类型占用的字节数:
#include <stdio.h>
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;
}
上面的代码生成以下结果。