Easy Tutorial
❮ C Macro Erange C Exercise Example67 ❯

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

C Language Examples

Using the sizeof operator to calculate the byte size of int, float, double, and char variables.

sizeof is a unary operator in C, similar to other operators like ++ and --, and it is not a function.

The sizeof operator provides 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 variables
    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;
}

Execution Result:

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

Calculate Byte Size 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;
}

Execution 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

❮ C Macro Erange C Exercise Example67 ❯