Why and how does GCC compile a function with a missing return statement?

后端 未结 8 1026
萌比男神i
萌比男神i 2020-12-01 23:57
#include 

char toUpper(char);

int main(void)
{
    char ch, ch2;
    printf(\"lowercase input : \");
    ch = getchar();
    ch2 = toUpper(ch);
             


        
8条回答
  •  抹茶落季
    2020-12-02 00:38

    What happened for you is that when the C program was compiled into assembly language, your toUpper function ended up like this, perhaps:

    _toUpper:
    LFB4:
            pushq   %rbp
    LCFI3:
            movq    %rsp, %rbp
    LCFI4:
            movb    %dil, -4(%rbp)
            cmpb    $96, -4(%rbp)
            jle     L8
            cmpb    $122, -4(%rbp)
            jg      L8
            movzbl  -4(%rbp), %eax
            subl    $32, %eax
            movb    %al, -4(%rbp)
    L8:
            leave
            ret
    

    The subtraction of 32 was carried out in the %eax register. And in the x86 calling convention, that is the register in which the return value is expected to be! So... you got lucky.

    But please pay attention to the warnings. They are there for a reason!

提交回复
热议问题