整型数的限制
头文件limits中包含了整型限制的信息
#include <iostream>
#include<limits>
using namespace std;
int main()
{
int n_int = INT_MAX; //最大的int型数
short n_short = SHRT_MAX; //最大的short型数
long n_long = LONG_MAX; //最大的long型数
cout << "不同整型数的字节数" << endl;
cout << "int is " << sizeof(n_int) << " bytes." << endl;
cout << "short is " << sizeof(n_short) << " bytes." << endl;
cout << "long is " << sizeof(n_long) << " bytes." << endl;
cout << "---------------------------------------------------" << endl;
cout << "---------------------------------------------------" << endl;
cout << "Maximum values:" << endl;
cout << "int: " << n_int << endl;
cout << "short: "<<n_short << endl;
cout << "long: " << n_long << endl;
}
如上图所示,int型和long型都是4个字节,short是2个字节,但是有的系统中int 是16位即2个字节。