数据类型都占有一定的内存,用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;
}【演示结果】
本文通过一个简单的C++程序展示了各种基本数据类型所占用的字节数,包括整型、浮点型等,并提供了完整的代码示例及运行结果。
2738

被折叠的 条评论
为什么被折叠?



