Sizeof() Operator in C Programming Language

Sizeof() Operator in C

The sizeof() is much used operator by programmers. It is very useful for developing portable programs.


Read more - Operators in C programming

Syntax to use sizeof() operator

sizeof() operator can be used in various way.


Example to use sizeof() operator

#include <stdio.h>

int main()
    int integerVar;

    printf("Size of char = %d\n", sizeof(char));                   // sizeof(type)
    printf("Size of int = %d\n", sizeof(integerVar));              // sizeof(variable-name)
    printf("Size of expression (3+2.5) = %d\n", sizeof(3 + 2.5));  // sizeof(expression)

    return 0;

Output of the above program.

Size of char = 1
Size of int = 4
Size of expression (3+2.5) = 8

Wondering, how sizeof(3 + 2.5) is 8? C performs integer promotion to make sure all operands in an expression are of similar type. All operands of expression is promoted to higher type i.e. double type (since 2.5 is of double type). Hence the expression sizeof(3 + 2.5) is equivalent to sizeof(double).

Note: The above program is compiled on GCC C compiler. The results may vary from compiler to compiler.