跳到主要内容

C++ 编程:在你的系统中找到int, float, double和char的大小

要找出变量的大小,使用 sizeof 运算符。

sizeof(数据类型);

示例:查找变量的大小

#include <iostream>
using namespace std;

int main()
{
cout << "char 的大小: " << sizeof(char) << " 字节" << endl;
cout << "int 的大小: " << sizeof(int) << " 字节" << endl;
cout << "float 的大小: " << sizeof(float) << " 字节" << endl;
cout << "double 的大小: " << sizeof(double) << " 字节" << endl;

return 0;
}

输出

char 的大小: 1 字节
int 的大小: 4 字节
float 的大小: 4 字节
double 的大小: 8 字节

注意: 如果您使用的是较老的计算机,您可能会得到不同的结果。