way1:
通过打印标准的头文件中的相应的值来完成
通过打印标准的头文件中的相应的值来完成
符号常量 | 表示 |
CHAR_BIT | char 的位数 |
CHAR_MAX | char 的最大值 |
CHAR_MIN | char 的最小值 |
SCHAR_MAX | signed char 的最大值 |
SCHAR_MIN | signed char 的最小值 |
UCHAR_MAX | unsigned char 的最大值 |
SHRT_MAX | short 的最大值 |
SHRT_MIN | short 的最小值 |
USHRT_MAX | unsigned short 的最大值 |
INT_MAX | int 的最大值 |
INT_MIN | int 的最小值 |
UNIT_MAX | unsigned int 的最大值 |
LONG_MAX | long 的最大值 |
LONG_MIN | long 的最小值 |
LONG_MAX | unsigned 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;
}
#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;
}
#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;
}
转载于:https://blog.51cto.com/xiaotou/203257