C++ 实例 - 查看变量大小
使用 C++ sizeof 运算符来计算 bool, char, int, long long, float, double变量占用的空间大小。
sizeof 运算符语法格式:
sizeof(dataType);
注意: 不同系统计算结果可能不一样。
实例
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << “bool: " << sizeof(bool) << " 字节" << endl;
cout << "char: " << sizeof(char) << " 字节" << endl;
cout << "int: " << sizeof(int) << " 字节" << endl;
cout << “long long: " << sizeof(long long) << " 字节" << endl;
cout << "float: " << sizeof(float) << " 字节" << endl;
cout << "double: " << sizeof(double) << " 字节" << endl;
return 0;
}
以上程序执行输出结果为:
bool: 1 字节
char: 1 字节
int: 4 字节
long long: 8 字节
float: 4 字节
double: 8 字节