2

What is the reason for including standard header files in C such as stdio.h stdlib.h etc.. gcc compiler just show a warning and declaring it implicitly like

implicit declaration of function 'printf'

and program executing successfully...
Rather than to remove the warning, is there any reason to include header files?

Seki
  • 11,135
  • 7
  • 46
  • 70
Embedded_User
  • 211
  • 2
  • 12
  • *`FILE`* to name one struct that is useful and declared in stdio.h – AndersK Feb 17 '14 at 12:46
  • You already answered your question, the point of including the headers is to stop the compiler from generating a warning. Stopping the compiler from generating the warning is a good thing. The fact that the compiler shows you a warning is also a good thing. Btw if it's easier you can put common includes together and just say `#include "stanard_headers.h"` or something like that to make it less typing if you want. – Brandin Feb 17 '14 at 22:57

1 Answers1

4

Well yes, of course there's a reason.

The reason is that with the headers, you get the proper declarations, otherwise you get the implicit declaration where every function is assumed to return int. Since there are many functions (for instance malloc()) that don't return int, assuming that they do can be very harmful.

With function declarations the compiler can actually check that arguments and return values match the usage, which is very good.

Also, there are of course cases where headers declare data types, enumerations and so on that you need, there's more than functions in headers.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    it's not only return value but all parameter types are also implicit int, that makes problem with types longer than int such as long long int – phuclv Feb 17 '14 at 15:07
  • also, on 64-bit OSes, pointers are 64-bit so implicit int may cause serious problem – phuclv Feb 17 '14 at 15:09