Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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.

Because few compiler includes those files and libraries by default!

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top