• Home
  • 3.23 sizeof Operator

3.23 sizeof Operator

View Categories

3.23 sizeof Operator

1 min read

Table of Contents

The sizeof operator determines the size, in bytes, of a type or object. Its result has type size_t.

#include <stdio.h>

int main()
{
    int number = 10;
    char letter = 'A';
    double value = 3.14;

    printf("Size of int    = %zu bytes\n", sizeof(int));
    printf("Size of number = %zu bytes\n", sizeof(number));
    printf("Size of char   = %zu bytes\n", sizeof(letter));
    printf("Size of double = %zu bytes\n", sizeof(value));

    return 0;
}

Example Output #

The exact sizes are implementation-dependent. A common result is:

Size of int    = 4 bytes
Size of number = 4 bytes
Size of char   = 1 bytes
Size of double = 8 bytes

Explanation #

sizeof can be applied to a type:

sizeof(int)

or to an expression/object:

sizeof(number)

When applied to an object, it gives the number of bytes required to store that object’s type.

The result is of type size_t, so %zu is used with printf():

printf("%zu\n", sizeof(number));

The size of char is always 1 byte in C, while the number of bytes occupied by other types depends on the implementation.

sizeof does not evaluate its operand when the operand is an expression whose type is not a variable length array. For example:

sizeof(number++)

does not perform the increment.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *