C++ サンプル - int, float, double および char 変数のサイズを確認

C++ 实例C++ サンプル

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 字节

C++ 实例C++ サンプル

その他の拡張