sizeof(hello())
is the size of the the return type, int
, not the size of the function. The function is not called.
The function does not need to be defined to determine its return type declared by int hello();
.
Deeper (in C)
sizeof
works with sizeof unary-expression
and sizeof ( type-name )
.
The size is determined from the type of the operand. The result
is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant. C17dr § 6.5.3.4 2
Since the type of the operand hello()
is an int
(and not a variable length array), the operand is not evaluated and is then like sizeof(int)
.
Aside: sizeof
returns a size_t
.
"%zu"
is a correct specifier to match a size_t
. "%ld"
is not specified to work.
// printf("sizeof hello = %ld, sizeof world = %ld\n", sizeof(hello()), sizeof(world()));
printf("sizeof hello = %zu, sizeof world = %zu\n", sizeof(hello()), sizeof(world()));