C function defined as int but having no return statement in the body still compiles C function defined as int but having no return statement in the body still compiles c c

C function defined as int but having no return statement in the body still compiles


The return value in this case, depending on the exact platform, will likely be whatever random value happened to be left in the return register (e.g. EAX on x86) at the assembly level. Not explicitly returning a value is allowed, but gives an undefined value.

In this case, the 14 is the return value from printf.


compile with -Wall to enable more sanity checking in the compiler.

gcc -Wall /tmp/a.c/tmp/a.c: In function ‘main’:/tmp/a.c:5: warning: implicit declaration of function ‘f’/tmp/a.c:6: warning: control reaches end of non-void function/tmp/a.c: In function ‘f’:/tmp/a.c:10: warning: control reaches end of non-void function

Note how it flags up the missing return statements - as "control reaches end of non-void function"?

Always compile using -Wall or similar - you will save yourself heartache later on.


I can recall several occasions when this exact issue has caused hours or days of debugging to fix - it sorta works until it doesn't one day.


14 is exactly the return value of the first printf, also. Probably, the compiler optimized out the call to f() and then we're left with 14 on EAX.

Try compiling your code with different optimization levels (-O0, -O1, -O2, -O3, -Os) and see if the output changes.