3.1 limits.cpp

3.1 limits.cpp

1 程序清单 3.1

  1. limits.cpp

    // 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;
    
        cout << "Minimum int value = " << INT_MIN << endl;
        cout << "Bits per byte = " << CHAR_BIT << endl;
        
        cout << "char的位数[CHAR_BIT] : " << CHAR_BIT << endl;
     
        cout << "char的最大值[CHAR_MAX] : " << CHAR_MAX << endl;
        cout << "char的最小值[CHAR_MIN] : " << CHAR_MIN << endl;
    
        cout << "signed char 最大值[SCHAR_MAX] : " << SCHAR_MAX << endl;
        cout << "signed char 最小值[SCHAR_MIN] : " << SCHAR_MIN << endl;
        cout << "unsigned char 最大值[UCHAR_MAX] : " << UCHAR_MAX << endl;
    
        cout << "short 最大值[SHRT_MAX] : " << SHRT_MAX << endl;
        cout << "short 最小值[SHRT_MIN] : " << SHRT_MIN << endl;
        cout << "unsigned short 最大值[USHRT_MAX] : " << USHRT_MAX << endl;
    
        cout << "int 最大值[INT_MAX] : " << INT_MAX << endl;
        cout << "int 最小值[INT_MIN] : " << INT_MIN << endl;
        cout << "unsigned int 最大值[UINT_MAX] : " << UINT_MAX << endl;
    
        cout << "long 最大值[LONG_MAX] : " << LONG_MAX << endl;
        cout << "long 最小值[LONG_MIN] : " << LONG_MIN << endl;
        cout << "unsigned long 最大值[ULONG_MAX] : " << ULONG_MAX << endl;
    
        cout << "long long 最大值[LLONG_MAX] : " << LLONG_MAX << endl;
        cout << "long long 最小值[LLONG_MIN] : " << LLONG_MIN << endl;
        cout << "unsigned long long 最大值[ULLONG_MAX] : " << ULLONG_MAX << endl;
    
        return 0;
    }
    
  2. 运行结果

    int is 4 bytes.
    short is 2 bytes.
    long is 4 bytes.
    long long is 8 bytes.
    
    Maximum values:
    int: 2147483647
    short: 32767
    long: 2147483647
    long long: 9223372036854775807
    Minimum int value = -2147483648
    Bits per byte = 8
    char的位数[CHAR_BIT] : 8
    char的最大值[CHAR_MAX] : 127
    char的最小值[CHAR_MIN] : -128
    signed char 最大值[SCHAR_MAX] : 127
    signed char 最小值[SCHAR_MIN] : -128
    unsigned char 最大值[UCHAR_MAX] : 255
    short 最大值[SHRT_MAX] : 32767
    short 最小值[SHRT_MIN] : -32768
    unsigned short 最大值[USHRT_MAX] : 65535
    int 最大值[INT_MAX] : 2147483647
    int 最小值[INT_MIN] : -2147483648
    unsigned int 最大值[UINT_MAX] : 4294967295
    long 最大值[LONG_MAX] : 2147483647
    long 最小值[LONG_MIN] : -2147483648
    unsigned long 最大值[ULONG_MAX] : 4294967295
    long long 最大值[LLONG_MAX] : 9223372036854775807
    long long 最小值[LLONG_MIN] : -9223372036854775808
    unsigned long long 最大值[ULLONG_MAX] : 18446744073709551615
    

2 注意事项

  1. 如果您的系统不支持类型long long ,应删除使用该类型的代码行。
  2. 以上结果输出来自运行64位Windows7系统。
  3. 比源代码多部分内容。
posted @ 2022-01-15 23:19  kaizenly  阅读(72)  评论(0编辑  收藏  举报
打赏