C function defined as int but having no return sta

2019-01-11 23:09发布

Say you have a C code like this:

#include <stdio.h>

int main(){
    printf("Hello, world!\n");
    printf("%d\n", f());    
}

int f(){

}

It compiles fine with gcc, and the output (on my system) is:

Hello, world!

14

But.. but.. how is that possible? I thought that C won't let you compile something like that because f() doesn't have a return statement returning an integer. Why is that allowed? Is it a C feature or compiler omission, and where did 14 come from?

标签: c compilation
7条回答
聊天终结者
2楼-- · 2019-01-11 23:54

I compile with -Werror=return-type to prevent this. GCC will give an error if you don't return from a function (unless it's void return).

查看更多
Fickle 薄情
3楼-- · 2019-01-11 23:55

The default return value from a function is int. In other words, unless explicitly specified the default return value by compiler would be integer value from function.

So, the ommiting of return statement is allowed, but undefined value will be returned, if you try to use it.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-11 23:57

18 is the return value of the first print statement. (the number of characters printed)

This value is stored in stack memory.

In second printf function the stack value is 'returned' by function f. But f just left the value that printf created in that slot on the stack.

for example, in this code:

#include <stdio.h>

int main(){
    printf("Hello, world!1234\n");
    printf("%d\n", f());    
}

int f(){

}

Which compiles fine with gcc, and the output (on my system) is:

Hello, world!

18

for details of printf() returning value mail me.

查看更多
贪生不怕死
5楼-- · 2019-01-11 23:59

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.

查看更多
【Aperson】
6楼-- · 2019-01-12 00:06

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.

查看更多
Viruses.
7楼-- · 2019-01-12 00:07

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.

查看更多
登录 后发表回答