#include<iostream> #include<climits> //use limits.h for older system
intmain(){ usingnamespacestd; int n_int = INT_MAX; short n_short = SHRT_MAX; //symbols defined in climts file long n_long = LONG_MAX; longlong n_llong = LLONG_MAX;
cout << "Minimum int value = "<<INT_MIN<<endl; cout << "BNits per bytr = "<< CHAR_BIT <<endl; return0; }
执行结果:
Results
1 2 3 4 5 6 7 8 9 10 11 12
int is 4 bytes short is 2 bytes long is 8 bytes longlong is 8 bytes
Maximum value: int : 2147483647 short : 32767 long : 9223372036854775807 longlong : 9223372036854775807 Minimum int value = -2147483648 BNits per bytr = 8
climits文件的符号常量
符号常量
极值意义
CHAR_INT
char的位数
CHAR_MAX
char的最大值
CHAR_INT
char的最小值
SCHAR_MAX
signed char的最大值
UCHAR_MAX
unsigned char的最大值
SHRT_MAX
short的最大值
USHRT_MAX
unsigned short的最大值
INT_MAX
int的最大值
LONG_MAX
long的最大值
LLONG_MAX
long long的最大值
初始化
1 2 3 4 5 6 7 8 9 10 11 12 13
int n_int = INT_MAX;
short year; year = 1492;
//C++11的初始化方式 int hamburgers = {24}; //set hanmburgers to 24
int emus{7}; //设置emus为7 int rheas = {12}; //设置rheas为12