Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Why is EAX being cleared before calling a function if I don't include the header?

In the following C code:

#include <stdio.h>
int main(void){getchar();}

It produces the following asm:

main:
        push    rbp
        mov     rbp, rsp
                 # no extra instruction here when header is included
        call    getchar
        mov     eax, 0
        pop     rbp
        ret

However, if I don't include stdio.h in the file, then it still compiles, but adds in what looks like a random mov eax, 0 instruction:

enter image description here

Here is Compiler Explorer: https://godbolt.org/z/3fTcss. Is this just part of "undefined behavior", or is there a particular reason that the instruction before call getchar is added there?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Without the header, gcc provides an implicit declaration of getchar when you use it, as if you had previously declared

int getchar();

(This behavior was guaranteed by older versions of the C standard. Current versions make it undefined behavior to use a function which was not previously declared, but gcc still provides the old behavior as an extension.)

This declaration provides no information about the types of arguments which getchar expects. (Remember that unlike in C++, a declaration with () doesn't declare a function as taking no arguments, but as taking unspecified arguments, leaving it up to the programmer to know what the function expects and pass the proper number and type of arguments.) For all the compiler knows, it could even be variadic, and per the x86-64 SysV ABI, variadic functions expect in al the number of vector registers which are being used to pass in arguments. Here no vector registers are being used, so the compiler sets al to 0 before the call. (It's slightly more efficient to actually zero all of rax so it does that instead.)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!