Cplusplus-cpp-sizeof-operator

提供:Dev Guides
移動先:案内検索

C ++ sizeof演算子

*sizeof* はキーワードですが、変数またはデータ型のサイズをバイト単位で決定するコンパイル時演算子です。

sizeof演算子を使用して、クラス、構造、ユニオン、およびその他のユーザー定義のデータ型のサイズを取得できます。

sizeofを使用する構文は次のとおりです-

sizeof (data type)

データ型は、クラス、構造、ユニオン、およびその他のユーザー定義のデータ型を含む目的のデータ型です。

C で使用可能なすべてのsizeof演算子を理解するには、次の例を試してください。 test.cppファイルに次のC プログラムをコピーして貼り付け、このプログラムをコンパイルして実行します。

#include <iostream>
using namespace std;

int main() {
   cout << "Size of char : " << sizeof(char) << endl;
   cout << "Size of int : " << sizeof(int) << endl;
   cout << "Size of short int : " << sizeof(short int) << endl;
   cout << "Size of long int : " << sizeof(long int) << endl;
   cout << "Size of float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;
   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;

   return 0;
}

上記のコードがコンパイルおよび実行されると、次の結果が生成されます。これは、マシンごとに異なる場合があります-

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4