Sizeof Operator in C

The sizeof operator in C is used to determine the memory size (in bytes) of a variable, data type, or object. It is a compile-time operator.

Syntax

sizeof(expression_or_data_type);

Examples

  • int a; printf("%zu", sizeof(a)); // Size of integer variable
  • printf("%zu", sizeof(int)); // Size of int data type
  • float f; printf("%zu", sizeof(f)); // Size of float variable
  • double d; printf("%zu", sizeof(d)); // Size of double variable
  • char c; printf("%zu", sizeof(c)); // Size of char variable

Key Points

  • Returns the size in bytes of the operand
  • Can be used with variables, data types, arrays, and structures
  • Evaluated at compile-time, so it doesn’t affect runtime performance
  • For arrays, returns the total size (number of elements × size of each element)

Common Mistakes

  • Using sizeof on pointers to get array size (returns pointer size, not array size)
  • Assuming sizeof always returns the same value across different systems (depends on architecture)
  • For structures, not considering padding bytes
  • Using sizeof with dynamically allocated memory incorrectly