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

后端 未结 7 2156
走了就别回头了
走了就别回头了 2020-11-28 13:48

Say you have a C code like this:

#include 

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

in         


        
7条回答
  •  清歌不尽
    2020-11-28 14:11

    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 
    
    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.

提交回复
热议问题