C Language Example - Calculate the Byte Sizes of int, float, double and char

C 语言实例C Language Examples

Use the sizeof operator to calculate the byte sizes of the four variable types: int, float, double, and char.

sizeof is a unary operator in C language, like other operators in C such as ++, --, etc. It is not a function.

The sizeof operator gives the storage size of its operand in bytes.

Example

#include <stdio.h> int main() { int integerType; float floatType; double doubleType; char charType; // The sizeof operator is used to calculate the byte size of a variable 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; }

Run result:

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

Calculate the byte sizes of long long and long double.

Example

#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; }

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 Language Examples

other extensions