2

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    The header `stdio.h` **declares** prototypes to functions **defined** in a libray. – alk Jan 01 '14 at 18:00
  • For your second question, if you fail to declare a function you use, it is typically implicitly declared as something like `int printf();` and if you call printf("foo") with this declaration you might even get it to work, however the behavior at this point is technically undefined because the number of arguments doesn't match the library definition for printf - https://www.securecoding.cert.org/confluence/display/seccode/EXP37-C.+Call+functions+with+the+correct+number+and+type+of+arguments – Brandin Jan 01 '14 at 23:18

4 Answers4

5

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.

sujin
  • 2,813
  • 2
  • 21
  • 33
  • thanks. so if I #include any non-standart lib, do I have to link this object file by myself or is it linked also automatically? –  Jan 01 '14 at 17:52
  • 1
    @int80: Yes, you need to tell this to the linker by using the option `-l`. Also you do include a *header* (to a library) not a *library*. The *library* is linked. – alk Jan 01 '14 at 17:58
  • To clarifiy, the standard library is the only library that is automatically linked (there's an option to prevent this, though). Other than that it's the same as every other one. – Guido Jan 01 '14 at 18:23
2

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.

alk
  • 69,737
  • 10
  • 105
  • 255
haccks
  • 104,019
  • 25
  • 176
  • 264
  • The only difference how standard libs and others are handled is that the standard lib is linked automatically. In all other aspects those libs are handled identically. Regarding if header are necessary or not and what issues can arise if needed header aren't included there also is no difference between standard and other libs/functions. – alk Jan 01 '14 at 18:04
  • @alk; *The only difference how standard libs and others are handled is that the standard lib is linked automatically.*: how would you link a non-standard lib to a program without including headers? – haccks Jan 01 '14 at 18:13
  • By telling the linker to do so like this: `-Lpath_to_a_non_standard_library -la_non_standard_library` – alk Jan 01 '14 at 18:15
  • @alk; Hmmm. Need to work on that :). I you find anything irrelevant, feel free to edit :) – haccks Jan 01 '14 at 18:16
0

Because few compiler includes those files and libraries by default!

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
0

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.

Joni
  • 108,737
  • 14
  • 143
  • 193