limits.h头文件决定了各种变量类型的各种属性。定义在该头文件中的宏限制了各种变量类型的值。
这些限制指定了变量不能存储任何超过这些限制值,例如一个无符号可以存储的最大值是255.

库宏

C 标准库 limits - 图1

实例

  1. #include <stdio.h>
  2. #include <limits.h>
  3. int main()
  4. {
  5. printf("The number of bits in a byte %d\n", CHAR_BIT);
  6. printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN);
  7. printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX);
  8. printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX);
  9. printf("The minimum value of SHORT INT = %d\n", SHRT_MIN);
  10. printf("The maximum value of SHORT INT = %d\n", SHRT_MAX);
  11. printf("The minimum value of INT = %d\n", INT_MIN);
  12. printf("The maximum value of INT = %d\n", INT_MAX);
  13. printf("The minimum value of CHAR = %d\n", CHAR_MIN);
  14. printf("The maximum value of CHAR = %d\n", CHAR_MAX);
  15. printf("The minimum value of LONG = %ld\n", LONG_MIN);
  16. printf("The maximum value of LONG = %ld\n", LONG_MAX);
  17. return(0);
  18. }

让我们编译和运行上面的程序,这将产生下列结果:

  1. The number of bits in a byte 8
  2. The minimum value of SIGNED CHAR = -128
  3. The maximum value of SIGNED CHAR = 127
  4. The maximum value of UNSIGNED CHAR = 255
  5. The minimum value of SHORT INT = -32768
  6. The maximum value of SHORT INT = 32767
  7. The minimum value of INT = -32768
  8. The maximum value of INT = 32767
  9. The minimum value of CHAR = -128
  10. The maximum value of CHAR = 127
  11. The minimum value of LONG = -2147483648
  12. The maximum value of LONG = 2147483647