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 changei. 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_ttype 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.