C-Sprachbeispiel - Berechnen der Bytegrößen von int, float, double und char

C 语言实例C-Sprachbeispiele

Verwenden Sie den sizeof-Operator, um die Bytegrößen der vier Variablentypen zu berechnen: int, float, double und char.

sizeof ist ein unärer Operator in der Sprache C, wie andere Operatoren in C wie ++, -- usw. Es ist keine Funktion.

Der sizeof-Operator gibt die Speichergröße seines Operanden in Bytes an.

Beispiel

#include <stdio.h> int main() { int integerType; float floatType; double doubleType; char charType; // Der sizeof-Operator wird verwendet, um die Bytegröße einer Variablen zu berechnen printf("Größe von int: %ld bytes\n",sizeof(integerType)); printf("Größe von float: %ld bytes\n",sizeof(floatType)); printf("Größe von double: %ld bytes\n",sizeof(doubleType)); printf("Größe von char: %ld byte\n",sizeof(charType)); return 0; }

Run result:

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

Berechnen Sie die Bytegrößen von long long und long double.

Beispiel

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

Run result:

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-Sprachbeispiele

Weitere Erweiterungen