003--sizeof的使用

sizeof用于获取类型或变量的内存大小。可对类型名或变量使用sizeof。对类型(如int)使用sizeof运算符时,应该将名称放在括号中;但对变量名(n_short)使用该运算符,括号是可选的:

 1 #include <iostream>
 2 #include <climits>
 3 
 4 int main(){
 5     using namespace std;
 6     int n_int=INT_MAX;
 7     short n_short=SHRT_MAX;
 8     long n_long=LONG_MAX;
 9     long long n_llong=LLONG_MAX;
10 
11     cout<<"int is "<<sizeof(int)<<" bytes."<<endl;
12     cout<<"short is "<<sizeof n_short<<" bytes."<<endl;
13     cout<<"long is "<<sizeof n_long<<" bytes."<<endl;
14     cout<<"long long is "<<sizeof n_llong<<" bytes."<<endl;
15     cout<<endl;
16 
17     cout<<"Maximum values:"<<endl;
18     cout<<"int: "<<n_int<<endl;
19     cout<<"short: "<<n_short<<endl;
20     cout<<"long: "<<n_long<<endl;
21     cout<<"long long: "<<n_llong<<endl;
22 
23     cout<<"Minimum int value:"<<INT_MIN<<endl;
24     cout<<"Bits per byte: "<<CHAR_BIT<<endl;
25 
26     return 0;
27 }
View Code
原文地址:https://www.cnblogs.com/gis-user/p/4907110.html