way1:
通过打印标准的头文件中的相应的值来完成
 
 
符号常量表示
CHAR_BITchar 的位数
CHAR_MAXchar 的最大值
CHAR_MINchar 的最小值
SCHAR_MAXsigned char 的最大值
SCHAR_MINsigned char 的最小值
UCHAR_MAXunsigned char 的最大值
SHRT_MAXshort 的最大值
SHRT_MINshort 的最小值
USHRT_MAXunsigned short 的最大值
INT_MAXint 的最大值
INT_MINint 的最小值
UNIT_MAXunsigned int 的最大值
LONG_MAXlong 的最大值
LONG_MINlong 的最小值
LONG_MAXunsigned long 的最大值
 
#include <iostream>
#include <climits>
using namespace std;
int main()
{
    cout << "Size:" << endl;
    cout << "int is     "   << sizeof (int)     << "bytes." << endl;
    cout << "short is   "   << sizeof (short)   << "bytes." << endl;
    cout << "long is    "   << sizeof (long)    << "bytes." << endl << endl;

    cout << "Bits per byte = " << CHAR_BIT << endl << endl;

    cout << "Maximum values:" << endl;
    cout << "int:           "   << INT_MAX << endl;
    cout << "short:         "   << SHRT_MAX << endl;
    cout << "long:          "   << LONG_MAX << endl;
    cout << "char:          "   << CHAR_MAX << endl;
    cout << "signed char:   "   << SCHAR_MAX << endl;
    cout << "unsigned int:  "   << UINT_MAX << endl;
    cout << "unsigned short:"   << USHRT_MAX << endl;
    cout << "unsigned long: "   << ULONG_MAX << endl;
    cout << "unsigned char: "   << UCHAR_MAX << endl << endl;

    cout << "Minimum values:" << endl;
    cout << "int:           "   << INT_MIN << endl;
    cout << "short:         "   << SHRT_MIN << endl;
    cout << "long:          "   << LONG_MIN <<endl;
    cout << "char:          "   << CHAR_MIN <<endl;
    cout << "signed char:   "   << SCHAR_MIN  <<endl;

    system("pause");

    return 0;
}
 

way2:
自行通过计算得到
way2:

#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    size_t sz = sizeof(int);
    __int64 base = 1;
    __int64 range = base<<(sz*8-1);  //1位符号位,这里打印出最大值,最小值同理
    cout<<range<<endl;
return 0;
}


【thinkinnight】:
还有,这里使用__int64来存取,否则会溢出

【steedhorse】:
#include <limits>
#include <iostream>
using namespace std;

template<typename INT_TYPE>
struct to_int {
typedef INT_TYPE int_type;
};

template<>
struct to_int<signed char> {
typedef signed int int_type;
};

template<>
struct to_int<unsigned char> {
typedef unsigned int int_type;
};

template<typename INT_TYPE>
void print_scope() {
cout << typeid(INT_TYPE).name() << ":\t";
cout << (typename to_int<INT_TYPE>::int_type)numeric_limits<INT_TYPE>::min() << " - " 
<< (typename to_int<INT_TYPE>::int_type)numeric_limits<INT_TYPE>::max() << endl;
}

int main() {
print_scope<signed char>();
print_scope<unsigned char>();
print_scope<signed short>();
print_scope<unsigned short>();
print_scope<signed int>();
print_scope<unsigned int>();
print_scope<signed long>();
print_scope<unsigned long>();

return 0;
}