数据类型都占有一定的内存,用sizeof()函数可以求得各数据类型所占有的字节数。
【代码示例】
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int a, b, c, d, e, f, g, h,i,j,k;
a=sizeof(char);
b = sizeof(short);
c = sizeof(int);
d = sizeof(long);
e= sizeof(unsigned int);
f = sizeof(long long);
g = sizeof(float);
h = sizeof(double);
cout << "char:" << a;
cout << "字节\nshort:" << b << "字节\nint:" << c << "字节\nlong:" << d << "字节\nunsigned int:" << e << "字节\nlong long:" << f;
cout << "字节\nfloat:" << g << "字节\ndouble:" << h <<"字节"<< endl;
system("pause");
return 0;
}
【演示结果】