I have two functions without any implementation.
I expect that the linker returns an undefined reference to hello and world error.
But surprisingly, the code compiles and runs without any error.
#include <stdio.h>
int hello();
char world();
int main() {
printf("sizeof hello = %zu, sizeof world = %zu\n", sizeof(hello()), sizeof(world()));
}
sizeof hello = 4, sizeof world = 1
The
sizeof
operator in C returns the size in bytes of its operand. For function operands, it returns the size of a function pointer. In most implementations, a function pointer is the same size as a pointer to any other type, which is usually 4 bytes on a 32-bit system and 8 bytes on a 64-bit system. That’s why thesizeof
operator returns 4 forhello()
and 1 forworld()
.It’s important to note that the actual values of
sizeof(hello())
andsizeof(world())
are not related to the return type of the functions, but rather to the size of a function pointer in the implementation.Also, it’s important to note that the lack of implementation for the functions
hello
andworld
is not a compile-time error, but a link-time error. The code will compile without any issues, but when it is linked the linker will complain about the undefined reference tohello
andworld
as they are not implemented.The
sizeof
operator in C returns the size in bytes of its operand. For function operands, it returns the size of a function pointer. In most implementations, a function pointer is the same size as a pointer to any other type, which is usually 4 bytes on a 32-bit system and 8 bytes on a 64-bit system. That’s why thesizeof
operator returns 4 forhello()
and 1 forworld()
.It’s important to note that the actual values of
sizeof(hello())
andsizeof(world())
are not related to the return type of the functions, but rather to the size of a function pointer in the implementation.Also, it’s important to note that the lack of implementation for the functions
hello
andworld
is not a compile-time error, but a link-time error. The code will compile without any issues, but when it is linked the linker will complain about the undefined reference tohello
andworld
as they are not implemented.