基本类型取值范围

#include <iostream>
#include <limits>


using namespace std;

int main(int argc, char *argv[])
{
    cout << "bool\t\t" <<
        "Sizeof: " << sizeof(bool) << "\t\t" <<
        "Min: " << numeric_limits<bool>::min() << "\t\t\t\t" <<
        "Max: " << numeric_limits<bool>::max() << endl;

    cout << endl;

    cout << "char\t\t" <<
        "Sizeof: " << sizeof(char) << "\t\t" <<
        "Min: " << (double)numeric_limits<char>::min() << "\t\t\t" <<
        "Max: " << (double)numeric_limits<char>::max() << endl;

    cout << "unsigned char\t" <<
        "Sizeof: " << sizeof(unsigned char) << "\t\t" <<
        "Min: " << (double)numeric_limits<unsigned char>::min() << "\t\t\t\t" <<
        "Max: " << (double)numeric_limits<unsigned char>::max() << endl;

    cout << "wchar_t\t\t" <<
        "Sizeof: " << sizeof(wchar_t) << "\t\t" <<
        "Min: " << numeric_limits<wchar_t>::min() << "\t\t" <<
        "Max: " << numeric_limits<wchar_t>::max() << endl;

    cout << endl;

    cout << "short\t\t" <<
        "Sizeof: " << sizeof(short) << "\t\t" <<
        "Min: " << numeric_limits<short>::min() << "\t\t\t" <<
        "Max: " << numeric_limits<short>::max() << endl;

    cout << "unsigned short\t" <<
        "Sizeof: " << sizeof(unsigned short) << "\t\t" <<
        "Min: " << numeric_limits<unsigned short>::min() << "\t\t\t\t" <<
        "Max: " << numeric_limits<unsigned short>::max() << endl;

    cout << endl;

    cout << "int\t\t" <<
        "Sizeof: " << sizeof(int) << "\t\t" <<
        "Min: " << numeric_limits<int>::min() << "\t\t" <<
        "Max: " << numeric_limits<int>::max() << endl;

    cout << "unsigned int\t" <<
        "Sizeof: " << sizeof(unsigned int) << "\t\t" <<
        "Min: " << numeric_limits<unsigned int>::min() << "\t\t\t\t" <<
        "Max: " << numeric_limits<unsigned int>::max() << endl;

    cout << endl;

    cout << "size_t\t\t" <<
        "Sizeof: " << sizeof(size_t) << "\t\t" <<
        "Min: " << numeric_limits<size_t>::min() << "\t\t\t\t" <<
        "Max: " << numeric_limits<size_t>::max() << endl;

    cout << endl;

    cout << "long\t\t" <<
        "Sizeof: " << sizeof(long) << "\t\t" <<
        "Min: " << numeric_limits<long>::min() << "\t" <<
        "Max: " << numeric_limits<long>::max() << endl;

    cout << "unsigned long\t" <<
        "Sizeof: " << sizeof(unsigned long) << "\t\t" <<
        "Min: " << numeric_limits<unsigned long>::min() << "\t\t\t\t" <<
        "Max: " << numeric_limits<unsigned long>::max() << endl;

    cout << endl;

    cout << "double\t\t" <<
        "Sizeof: " << sizeof(double) << "\t\t" <<
        "Min: " << numeric_limits<double>::min() << "\t\t" <<
        "Max: " << numeric_limits<double>::max() << endl;

    cout << "long double\t" <<
        "Sizeof: " << sizeof(long double) << "\t\t" <<
        "Min: " << numeric_limits<long double>::min() << "\t\t" <<
        "Max: " << numeric_limits<long double>::max() << endl;

    return 0;
}

在我机子上输出结果:

bool            Sizeof: 1   Min: 0                      Max: 1

char            Sizeof: 1   Min: -128                   Max: 127
unsigned char   Sizeof: 1   Min: 0                      Max: 255
wchar_t         Sizeof: 4   Min: -2147483648            Max: 2147483647

short           Sizeof: 2   Min: -32768                 Max: 32767
unsigned short  Sizeof: 2   Min: 0                      Max: 65535

int             Sizeof: 4   Min: -2147483648            Max: 2147483647
unsigned int    Sizeof: 4   Min: 0                      Max: 4294967295

size_t          Sizeof: 8   Min: 0                      Max: 18446744073709551615

long            Sizeof: 8   Min: -9223372036854775808   Max: 9223372036854775807
unsigned long   Sizeof: 8   Min: 0                      Max: 18446744073709551615

double          Sizeof: 8   Min: 2.22507e-308           Max: 1.79769e+308
long double     Sizeof: 16  Min: 3.3621e-4932           Max: 1.18973e+4932