C sizeof operator

    The C sizeof operator accepts exactly one argument and returns the its size, measured in bytes. The result from the operator is of type size_t, which is defined in the header <stddef.h>.

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!

Syntax

    Sizeof is an unary operator, meaning that it accepts exactly one argument (operand). Depending on the operand, you will or you won’t need to use parenthesis around it:

You can use it to get the size of any type:
    s
izeof(<type>);

..or get the size of an expression:
    sizeof
<expression>;

Rules and restrictions

Rules:

  • When the argument is a type name you must use the parenthesis.
  • If you test for the size of an expression you can omit the parenthesis.
  • The result from the operator is of type size_t. It is defined in the header <stddef.h>. It is an unsigned integer. It is also safe to cast the result to unsigned long.
  • The returned value is the actual number of bytes that the argument takes in memory.
  • sizeof(char)will  always return 1.

Do not use the C sizeof operator with:

  • an incomplete type
  • a bit-field object
  • a function type

Examples

Using the C sizeof with a type name

Here you can check the size of any type.
    sizeof(char);        //This will always return 1:

You can test and print the size of the different data types like this:
    printf("%
u\n", sizeof(short));
    printf("%u\n", sizeof(int));
    printf("%u\n", sizeof(double));

Using the C sizeof with an expression

    sizeof 5; // This will return the size of the int type(4 bytes in my case)

    double price = 299.90;
    printf("%u\n", sizeof price); // This will print the size of the double data type and not the value, saved in the variable price

    int numbers[15];
    int arraySize = sizeof numbers;
    printf("%u\n", arraySize);

    Note for the last example: We will not get the number of elements in the array, nor the size of the int type. What we will get is the actual amount of memory, needed to store the array. It is equal to the number of elements multiplied by the array type. On my computer this is 15 * 4 = 60 Bytes.

Getting the number of elements in an array

In C, the standard way to get the number of elements in an array is like this:
    int elementsCount = sizeof arrayName / sizeof arrayName[0];

    In other words, we divide the size of the array by the size of one element. Logically, the result is the number of elements in that array.

     Note: this will not work in a function that received the array as a parameter. That’s because the actual parameter is a pointer to the array and not the array itself. This is the reason why in C you should always pass the size of the array as a complementary argument to the array.

Related reading:

Previous keywordshort

Next keywordstatic

Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Start now!


› sizeof

Did this help? Support me with your vote ;-)

Did this help?