Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Without header(stdio.h) file, how can the following code run? [duplicate]

Tags:

c

How this following code can run without including header file for printf function?

int main(){
printf("%d",1);
return 0;}
like image 335
Govinda Raj Avatar asked Feb 21 '26 16:02

Govinda Raj


1 Answers

Note: This is a hand-wavy answer that will roughly be correct. Someone else who knows the gory details (for gcc, e.g.) may enlighten both of us. Here goes:

Because in C — at least for some compilers — implicitly defined functions are fine. So it compiles it, then hands it off to the linker. It sees a reference to printf, and since the linker by default links with the C runtime library, it will resolve that symbol to the correct function.

I guess an implicit function like that will get a default signature, typically expecting to return an int. As for the arguments to the function, those can't be type checked at compile time, because the compiler doesn't know what the actual function signature is. So it will just use standard calling convention, e.g. pass arguments by registers or something like that.

like image 73
csl Avatar answered Feb 24 '26 08:02

csl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!