C programming directives #include<stdio.h> printf

625 views Asked by At

I have that simple hello world program. First row it says #include <stdio.h>.

I googled it and it tells basically the preprocessor to define the functions for input/output.

First question:
I read that the actually code of the function first compiled to an object file. So if I say #include <foo.bar> it automatically linkes that object file?

Second question:
When I removed the include, the program still works... I mean the printf statement... why?

4

There are 4 answers

3
sujin On BEST ANSWER

printf() is located in standard C library and linker links standard library to your program.

So any standard functions will not be any linking problems.

If you compile the program without #include<stdio.h> using gcc you will get the warning.

0
Digital_Reality On

Because few compiler includes those files and libraries by default!

0
Joni On

The printf function is defined in the standard C library, which your compiler links automatically to your program, unless told otherwise. The header file only has the function declaration so removing the include directive does not make the function unavailable.

4
haccks On

In some older compilers without including the headers for standard library function your code will not compile.

In some modern compilers the standard library is linked by default.

If the header for any library used is not included a warning is issued like the following:

 [Warning] implicit declaration of function 'printf' [-Wimplicit-function-declaration]

For non standard library function you must have to link it with your program . Do not forget to include its header.