C++ 實例 - 查看 int, float, double 和 char 變數大小
使用 C++ sizeof 運算符來計算 int, float, double 和 char 變數佔用的空間大小。
sizeof 運算符語法格式:
sizeof(dataType);
注意:不同系統計算結果可能不一樣。
實例
#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 位元組