Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does gcc include some header files automatically?

Tags:

c

linux

gcc

I have the following code:

int main()
{
    printf("Hello\n");
    return 0;
}

I compiled it using the following command:

gcc -o myprogram myfile.c

And it compiled without any error even though I did not #include <stdio.h>. So did gcc include this header file automatically?

My gcc version is 4.3.3

like image 278
Tom Avatar asked Apr 16 '26 14:04

Tom


1 Answers

In ANSI C, you can call functions you didn't declare. Such functions are implicitly declared the first time you call them. They are assumed to return int and take arguments according to the default argument promotions. Since you didn't include <stdio.h>, the compiler uses this rule and implicitly declares printf. Note that this is undefined behaviour as functions that take variable argument lists like printf must be declared explicitly. gcc usually warns you if it uses the implicit declaration rule since it's usually not intentionally used.

like image 102
fuz Avatar answered Apr 19 '26 09:04

fuz