C++ Standard Library <climits>

<climits>is a header file in the C++ standard library that provides limits and characteristics related to integer types. It defines a set of constants that describe various integer types (such aschar、int、longetc.), including their minimum values, maximum values, and other related attributes. These constants come from the C standard library's<limits.h>Header file.

<climits>Provided constants

These constants describe the characteristics of different integer types on specific platforms. The following are some commonly used constants:

1. Character types

  • CHAR_BIT:charnumber of bits of a type (usually 8).
  • CHAR_MIN:charThe minimum value of the type.
  • CHAR_MAX:charThe maximum value of the type.
  • SCHAR_MIN: signedcharThe minimum value of the type.
  • SCHAR_MAX: signedcharThe maximum value of the type.
  • UCHAR_MAX: unsignedcharThe maximum value of the type.

2. short integer

  • SHRT_MIN:shortThe minimum value of the type.
  • SHRT_MAX:shortThe maximum value of the type.
  • USHRT_MAX: unsignedshortThe maximum value of the type.

3. integer

  • INT_MIN:intThe minimum value of the type.
  • INT_MAX:intThe maximum value of the type.
  • UINT_MAX: unsignedintThe maximum value of the type.

4. long integer

  • LONG_MIN:longThe minimum value of the type.
  • LONG_MAX:longThe maximum value of the type.
  • ULONG_MAX: unsignedlongThe maximum value of the type.

5. long long integer

  • LLONG_MIN:long longThe minimum value of the type.
  • LLONG_MAX:long longThe maximum value of the type.
  • ULLONG_MAX: unsignedlong longThe maximum value of the type.

Example

Below is an example using<climits>Example program of constants defined in the header file:

Example

#include <iostream>
#include <climits>

int main() {
    // Print the maximum and minimum values of int
    std::cout << "The maximum value of int is:" << INT_MAX << std::endl;
    std::cout << "The minimum value of int is:" << INT_MIN << std::endl;

    // Print the maximum and minimum values of long
    std::cout << "The maximum value of long is:" << LONG_MAX << std::endl;
    std::cout << "The minimum value of long is:" << LONG_MIN << std::endl;

    // Print the maximum value of unsigned long
    std::cout << "The maximum value of unsigned long is:" << ULONG_MAX << std::endl;

    // Print the maximum and minimum values of char
    std::cout << "The maximum value of char is:" << CHAR_MAX << std::endl;
    std::cout << "The minimum value of char is:" << CHAR_MIN << std::endl;

    return 0;
}

Output result

When you run the above program, the output will be similar to the following (specific values may vary depending on the compiler and platform):

int 的最大值是:2147483647
int 的最小值是:-2147483648
long 的最大值是:9223372036854775807
long 的最小值是:-9223372036854775808
unsigned long 的最大值是:18446744073709551615
char 的最大值是:127
char 的最小值是:-128

<climits>The constants in the header file provide useful information about the representation of integer types, enabling programmers to write platform-independent code and ensuring consistent behavior across different platforms. Understanding the meaning and usage of these constants is particularly important for applications that require high precision and range control. If you have specific usage requirements or questions, you can discuss further.

other extensions