As is often the case with questions about specific results in c , the reason here is undefined behavior . Standard project C11 n1570 6.7.2.3p3 :
Form type specifier
enum identifier
no list of enumerators should appear only after the type that it defines is completed.
And completeness in C11 6.7.2.2p4 :
- [...] The enumerated type is incomplete until the moment immediately following}, which completes the list of declarations of the enumerator and ends after that.
As it was violated in the restrictions section, the corresponding compiler should output a diagnostic message - however, GCC is by default not an appropriate implementation unless you ask it to be -pedantic :
ISO C forbids forward references to 'enum' types [-Werror=pedantic] enum foo foo_test(void); ^
Now it seems that the compiler uses the shortest possible enumerations for any type. Since you used enum foo , before it was actually determined what was there, the compiler had to use int for its type, otherwise char used. It can be played using -fshort-enums . Your program prints 4 1 , while this program prints 1 1 with -fshort-enums on my gcc.
#include <stdio.h> enum foo { FOO_0 }; enum foo foo_test(void); enum bar { BAR_0 }; int main(int argc, char **argv) { printf("sizeof(enum foo) %zu, sizeof(enum bar) %zu\n", sizeof(enum foo), sizeof(enum bar)); return 0; }
source share