Code:
printf("%u\n", sizeof(main));
Edit: And sizeof(main) will either not compile, or return the size of a pointer to a function, neither of which is particularly useful.
Not to mention that sizeof() doesn't return an unsigned int, so either a cast or the C99 %z format specifier would need to be used . . .
And I think it would compile, and return the size of a function pointer, pointing to main. Which would likely be the same size as any other pointer: 2 (for 16-bit), 4 (for 32-bit), or 8 bytes (for a 64-bit computer). Of course, main() might be different, because main() is unique and strange. (For example, you can't call it in C++.)
Well, that's what I thought . . . until I tested it.
Code:
$ cat sizeofmain.c
#include <stdio.h>
void func(void) {}
int main() {
void (*fp)(void) = func;
printf("main: %lu\n", (unsigned long)sizeof(main));
printf("func: %lu\n", (unsigned long)sizeof(func));
printf("fp : %lu\n", (unsigned long)sizeof(fp));
printf("cast: %lu\n", (unsigned long)sizeof((int (*)(void))main));
return 0;
}
$ ./sizeofmain
main: 1
func: 1
fp : 8
cast: 8
$
I'm working on a 64-bit computer at the moment, as you may have guessed. However, I have no idea why the sizeof a function is 1 and the sizeof a function pointer is 8 (or whatever). Any suggestions as to why this is?