limits.h

Content

整数值的最大值和最小值非常有用,或者简单地说,任何整数类型的极限在编程中都起着重要作用。无需记住这些值,可以使用不同的宏。**<climits>(limits.h)**定义整数类型的大小。[2]

name[1]expressespossible value*
CHAR_BITNumber of bits in a char object (byte)8 or greater*
SCHAR_MINMinimum value for an object of type signed char-127 (-27+1) or less*
SCHAR_MAXMaximum value for an object of type signed char127 (27-1) or greater*
UCHAR_MAXMaximum value for an object of type unsigned char255 (28-1) or greater*
CHAR_MINMinimum value for an object of type chareither SCHAR_MIN or 0
CHAR_MAXMaximum value for an object of type chareither SCHAR_MAX or UCHAR_MAX
MB_LEN_MAXMaximum number of bytes in a multibyte character, for any locale1 or greater*
SHRT_MINMinimum value for an object of type short int-32767 (-215+1) or less*
SHRT_MAXMaximum value for an object of type short int32767 (215-1) or greater*
USHRT_MAXMaximum value for an object of type unsigned short int65535 (216-1) or greater*
INT_MINMinimum value for an object of type int-32767 (-215+1) or less*
INT_MAXMaximum value for an object of type int32767 (215-1) or greater*
UINT_MAXMaximum value for an object of type unsigned int65535 (216-1) or greater*
LONG_MINMinimum value for an object of type long int-2147483647 (-231+1) or less*
LONG_MAXMaximum value for an object of type long int2147483647 (231-1) or greater*
ULONG_MAXMaximum value for an object of type unsigned long int4294967295 (232-1) or greater*
LLONG_MINMinimum value for an object of type long long int-9223372036854775807 (-263+1) or less*
LLONG_MAXMaximum value for an object of type long long int9223372036854775807 (263-1) or greater*
ULLONG_MAXMaximum value for an object of type unsigned long long int18446744073709551615 (264-1) or greater*

Demo

C
#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;
}
C++
// limits.cpp -- some integer limits
#include <iostream>
#include <climits> // use limits.h for older systems
int main()
{
    using namespace std;
    int n_int = INT_MAX;      // initialize n_int to max int value
    short n_short = SHRT_MAX; // symbols defined in climits file
    long n_long = LONG_MAX;
    long long n_llong = LLONG_MAX;

    // sizeof operator yields size of type or of variable
    cout << "int is " << sizeof(int) << " bytes." << endl;
    cout << "short is " << sizeof n_short << " bytes." << endl;
    cout << "long is " << sizeof n_long << " bytes." << endl;
    cout << "long long is " << sizeof n_llong << " bytes." << endl;
    cout << endl;

    cout << "Maximum values:" << endl;
    cout << "int: " << n_int << endl;
    cout << "short: " << n_short << endl;
    cout << "long: " << n_long << endl;
    cout << "long long: " << n_llong << endl
         << endl;

    cout << "Minimum int value = " << INT_MIN << endl;
    cout << "Bits per byte = " << CHAR_BIT << endl;
    // cin.get();
    return 0;
}

// (base) kimshan@MacBook-Pro output % ./"limits"
// int is 4 bytes.
// short is 2 bytes.
// long is 8 bytes.
// long long is 8 bytes.

// Maximum values:
// int: 2147483647
// short: 32767
// long: 9223372036854775807
// long long: 9223372036854775807

// Minimum int value = -2147483648
// Bits per byte = 8

Resources

[1] https://cplusplus.com/reference/climits/

[2] https://www.geeksforgeeks.org/climits-limits-h-cc/