C Standard Library 简明教程

C Library - <limits.h>

C 库 limits.h 头文件定义了不同变量类型的各种属性。在此头文件中定义的宏限制各种变量类型的值,如 char、int 和 long。

The C Library limits.h header file defines various properties of the different variable types. The macros defined in this header, limits the values of various variable types like char, int and long.

这些限制指定变量不能存储超出这些限制的任何值,例如无符号字符最多可以存储 255 的最大值。

These limits specify that a variable cannot store any value beyond these limits, for example an unsigned character can store up to a maximum value of 255.

<limits.h> Library Macros

以下值是实现特定的,并使用 #define 指令定义。但是,这些值可能不会低于给定的值。

The following values are implementation-specific and defined using the #define directive. However, these values may not be lower than the given ones.

Macro

Value

Description

CHAR_BIT

8

Defines the number of bits in a byte.

SCHAR_MIN

-128

Defines the minimum value for a signed char.

SCHAR_MAX

+127

Defines the maximum value for a signed char.

UCHAR_MAX

255

Defines the maximum value for an unsigned char.

CHAR_MIN

-128

Defines the minimum value for type char and its value will be equal to SCHAR_MIN if char represents negative values, otherwise zero.

CHAR_MAX

+127

Defines the value for type char and its value will be equal to SCHAR_MAX if char represents negative values, otherwise UCHAR_MAX.

MB_LEN_MAX

16

Defines the maximum number of bytes in a multi-byte character.

SHRT_MIN

-32768

Defines the minimum value for a short int.

SHRT_MAX

+32767

Defines the maximum value for a short int.

USHRT_MAX

65535

Defines the maximum value for an unsigned short int.

INT_MIN

-2147483648

Defines the minimum value for an int.

INT_MAX

+2147483647

Defines the maximum value for an int.

UINT_MAX

4294967295

Defines the maximum value for an unsigned int.

LONG_MIN

-9223372036854775808

Defines the minimum value for a long int.

LONG_MAX

+9223372036854775807

Defines the maximum value for a long int.

ULONG_MAX

18446744073709551615

Defines the maximum value for an unsigned long int.

请注意,以上宏可帮助开发人员编写可在不同平台和编译器中始终保持一致的高效代码。

Note that, the above macros help developer to write efficient code that works consistently across different platforms and compilers.

Example 1

以下是用来展示 limits.h 头文件下的各种宏范围值的 C 库程序基本代码。

Following is the basic C Library program to display the range value of various macros under the limits.h header file.

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

int main() {
   printf("The value of CHAR_BIT: %d\n", CHAR_BIT);
   printf("The value of SCHAR_MIN: %d\n", SCHAR_MIN);
   printf("The value of SCHAR_MAX: %d\n", SCHAR_MAX);
   printf("The value of UCHAR_MAX: %u\n", UCHAR_MAX);
   printf("The value of CHAR_MIN: %d\n", CHAR_MIN);
   printf("The value of CHAR_MAX: %d\n", CHAR_MAX);
   printf("The value of MB_LEN_MAX: %d\n", MB_LEN_MAX);
   printf("The value of SHRT_MIN: %d\n", SHRT_MIN);
   printf("The value of SHRT_MAX: %d\n", SHRT_MAX);
   printf("The value of USHRT_MAX: %u\n", USHRT_MAX);
   printf("The value of INT_MIN: %d\n", INT_MIN);
   printf("The value of INT_MAX: %d\n", INT_MAX);
   printf("The value of UINT_MAX: %u\n", UINT_MAX);
   printf("The value of LONG_MIN: %ld\n", LONG_MIN);
   printf("The value of LONG_MAX: %ld\n", LONG_MAX);
   printf("The value of ULONG_MAX: %lu\n", ULONG_MAX);
   return 0;
}

Output

执行以上代码,将获得以下结果 −

On execution of above code, we get the following result −

The value of CHAR_BIT: 8
The value of SCHAR_MIN: -128
The value of SCHAR_MAX: 127
The value of UCHAR_MAX: 255
The value of CHAR_MIN: -128
The value of CHAR_MAX: 127
The value of MB_LEN_MAX: 16
The value of SHRT_MIN: -32768
The value of SHRT_MAX: 32767
The value of USHRT_MAX: 65535
The value of INT_MIN: -2147483648
The value of INT_MAX: 2147483647
The value of UINT_MAX: 4294967295
The value of LONG_MIN: -9223372036854775808
The value of LONG_MAX: 9223372036854775807
The value of ULONG_MAX: 18446744073709551615

Example 2

在此示例中,我们说明用来查找最小值和最大值的程序。

In this example, we illustrate the program for finding the minimum and maximum values.

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

int main() {
   int input[] = {89, 5, 21, 10, 45, 31, 98};
   int n = sizeof(input) / sizeof(input[0]);

   // Initialization of macros
   int min_value = INT_MAX;
   int max_value = INT_MIN;

   // loop iteration for min and max value
   for (int i = 0; i < n; ++i) {
       if (input[i] < min_value) {
           min_value = input[i];
       }
       if (input[i] > max_value) {
           max_value = input[i];
       }
   }
   // Display the result
   printf("Minimum value: %d\n", min_value);
   printf("Maximum value: %d\n", max_value);

   return 0;
}

Output

执行以上代码,将获得以下结果 −

On execution of above code, we get the following result −

Minimum value: 5
Maximum value: 98

Example 3

在下面,程序初始化 limit 宏(CHAR_MIN/MAX)以生成给定字符串中的最小长度和最大长度字符。

Below the program initializes limit macros(CHAR_MIN/MAX) to generate the minimum and maximum length of character from the given string.

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

int main() {
   char* char_input[] = {"RUSSIA", "UK", "INDIA", "NEWYORK", "INDONESIA", "DUBAI"};
   int num_strings = sizeof(char_input) / sizeof(char_input[0]);

   // Initialization of CHAR macros
   int min_length = CHAR_MAX;
   int max_length = CHAR_MIN;

   for (int i = 0; i < num_strings; ++i) {
       int current_length = strlen(char_input[i]);
       if (current_length < min_length) {
           min_length = current_length;
       }
       if (current_length > max_length) {
           max_length = current_length;
       }
   }

   printf("Minimum length of characters: %d\n", min_length);
   printf("Maximum length of characters: %d\n", max_length);

   return 0;
}

Output

以上代码生成以下结果 −

The above code produces the following result −

Minimum length of characters: 2
Maximum length of characters: 9