sizeof

Is sizeof() a Function in C?

No, sizeof() is an operator built into C, not a function. Learn why it looks like a function, how it differs from one, and when parentheses are needed.

On this page
  1. Short answer
  2. Why it looks like a function
  3. How sizeof differs from a function
  4. A real function to compare: strlen
  5. In short

Short answer

No. sizeof is an operator built into the language. The parentheses in sizeof(int) make it look like a function call, but the compiler treats it differently. Many people search for the sizeof function in C, so this distinction is worth knowing early.

Why it looks like a function

Parentheses are used for function calls and for sizeof, so the two look alike:

printf("%zu\n", sizeof(int));  // sizeof applied to a type name

The parentheses are required only when the operand is a type name. With an expression, they are optional, so sizeof value is also valid. That flexibility is one clue that sizeof is not an ordinary function.

How sizeof differs from a function

  • The operand can be a type. sizeof(int) passes a type, not a value. A function cannot take a type as an argument.
  • Most results are fixed at compile time. The compiler works out the size, so no function call happens at runtime for most operands.
  • The operand is not evaluated. sizeof(i++) does not change i. The exception is a variable length array in C.
  • No header is needed for the operator. It is part of the language itself. The size_t type it returns is declared in <stddef.h> in C.
  • It cannot be used as a function. You cannot take its address or pass it as a function pointer.

Like the other unary operators in C, sizeof takes one operand. It is also a reserved keyword in both C and C++, so you cannot use the name for a variable.

A real function to compare: strlen

strlen() is a real function. It is declared in <string.h> in C, or <cstring> in C++, and it runs at runtime. It counts characters up to the null terminator:

#include <stdio.h>
#include <string.h>

int main(void) {
    char word[] = "Hello";
    printf("sizeof: %zu\n", sizeof(word));  // 6, including '\0'
    printf("strlen: %zu\n", strlen(word));  // 5
    return 0;
}

sizeof reports the size of the array, which includes the terminating null character. strlen reports the number of characters before that null character.

In short

sizeof is an operator, not a function. It takes a type or an expression, the compiler resolves its value, and it does not need a function call. Use it for sizes, and use strlen() when you need the length of a string at runtime.

The complete guide to sizeof

The sizeof Operator in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The const Keyword in C++

Learn what the const keyword does in C++, how it applies to variables, pointers, references and member functions, and how the compiler enforces it.

const 5 min read

std::function in C++

std::function is a C++ wrapper that holds any callable with a matching signature, such as a function, lambda or functor, and calls it the same way.

std::function 5 min read