C言語の例 - int、float、double、char のバイトサイズを計算

C 语言实例C言語の例

sizeof 演算子を使用して int、float、double、char の4つの変数のバイトサイズを計算します。

sizeof は C 言語の単項演算子の一つであり、C言語の他の演算子 ++、-- などと同様で、関数ではありません。

sizeof 演算子は、オペランドの記憶域のサイズをバイト単位で示します。

例

#include <stdio.h> int main() { int integerType; float floatType; double doubleType; char charType; // sizeof 演算子は変数のバイトサイズを計算するために使われます printf("Size of int: %ld bytes\n",sizeof(integerType)); printf("Size of float: %ld bytes\n",sizeof(floatType)); printf("Size of double: %ld bytes\n",sizeof(doubleType)); printf("Size of char: %ld byte\n",sizeof(charType)); return 0; }

実行結果:

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

long long、long double のバイトサイズを計算

例

#include <stdio.h> int main() { int a; long b; long long c; double e; long double f; printf("Size of int = %ld bytes \n", sizeof(a)); printf("Size of long = %ld bytes\n", sizeof(b)); printf("Size of long long = %ld bytes\n", sizeof(c)); printf("Size of double = %ld bytes\n", sizeof(e)); printf("Size of long double = %ld bytes\n", sizeof(f)); return 0; }

実行結果:

Size of int = 4 bytes 
Size of long = 8 bytes
Size of long long = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes

C 语言实例C言語の例

その他の拡張