47

Possible Duplicates:
[C] Header per source file.
In C++ why have header files and cpp files?
C++ - What should go into an .h file?

Is the only reason header files exist in C
so a developer can quickly see what functions are available
and what arguments they can take?

Or is it something to do with the compiler?

Why has no other language used this method?

Is it just me, or does it seem that having 2 sets of function definitions
will only lead to more maintenance and more room for errors?

Or is knowing about header files just something every C developer must know?

Oreo
  • 529
  • 3
  • 16
alex
  • 479,566
  • 201
  • 878
  • 984

4 Answers4

41

Header files are needed to declare functions and variables that are available. You might not have access to the definitions (=the .c files) at all; C supports binary-only distribution of code in libraries.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 5
    why it doesn't scan directly included included C files to check what is inside, this is what i don't understand. – chris Sep 14 '16 at 16:43
  • 1
    @chris As I said, there's no requirement that you have the source code for a function you want to call. And there's no requirement that a platform's binary format contains information that a compiler can read, it's not done that way. Perhaps "stupidly", but that's how it is. – unwind Sep 15 '16 at 08:19
  • When you DO have the .c source code in the same file, or you include another c source file, I think compilation succeeds but only if functions are defined before they are called. This is a problem when you have circular calls (though in theory you should be able to write code such that there are no circular dependencies). I guess you could say they are like symlinks in an otherwise tree structured file system. – Sridhar Sarnobat Dec 28 '19 at 10:52
25

The compiler needs the information in the header files to know what functions, structures, etc are available and how to use them.

All languages needs this kind of information, although they retrieve the information in different ways. For example, a Java compiler does this by scanning either the class-file or the java source code to retrieve the information.

The drawback with the Java-way is that the compiler potentially needs to hold a much more of information in its memory to be able to do this. This is no big deal today, but in the seventies, when the C language was created, it was simply not possible to keep that much information in memory.

Hans Insulander
  • 371
  • 2
  • 4
  • 14
    It seems most new languages even compiled ones don't bother with header files anymore. So would you say header files are redundant nowadays if we were to recreate C? – CMCDragonkai Jul 22 '14 at 08:04
17

The main reason headers exist is to share declarations among multiple source files.

Say you have the function float *f(int a, int b) defined in the file a.c and reused in b.c and d.c. To allow the compiler to properly check arguments and return values you either put the function prototype in an header file and include it in the .c source files or you repeat the prototype in each source file.

Same goes for typedef etc.

While you could, in theory, repeat the same declaration in each source file, it would become a real nightmare to properly manage it.

Some language uses the same approach. I remember the TurboPascal units being not very different. You would put use ... at the beginning to signal that you were going to require functions that were defined elsewhere. I can't remember if that was passed into Delphi as well.

Remo.D
  • 16,122
  • 6
  • 43
  • 74
7
  1. Know what is in a library at your disposal.
  2. Split the program into bite-size chunks for the compiler. Compiling a megabyte of C files simultaneously will take more resources than most modern hardware can offer.
  3. Reduce compiler load. Why should it know in screen display procedures about deep database engine? Let it learn only of functions it needs now.
  4. Separate private and public data. This use isn't frequent but you may implement in C what C++ uses private fields for: each .c file includes two .h files, one with declarations of private stuff, the other with whatever others may require from the file. Less chance of a namespace conflict, safer due to hermetization.
  5. Alternate configs. Makefile decides which header to use, and the same code may service two different platforms given two different header files.

probably more.

SF.
  • 13,549
  • 14
  • 71
  • 107
  • "Why should it know in screen display procedures about deep database engine"...although if this is the case in the implementation, then the project probably has bigger problems than build resources. – Carl G Feb 09 '14 at 15:24
  • @CarlG: You never programmed embedded devices, did you? – SF. Feb 09 '14 at 17:19
  • 2
    No. It's not possible to modularize view from storage in such systems? Or just impractical? I would have supposed that a deep database engine would be better offloaded to a separate embedded system or even (networked?) service if possible. – Carl G Feb 09 '14 at 23:14
  • 4
    @CarlG: Depending on the system, usually just very impractical. Modularize, on code level; compile both into one binary loaded into one processor, often without OS and/or filesystem. Offloading them to two different processors just for the sake of modularization will give you so many headaches connected with controlling the hardware bus between the two it's really not worth it. (theoretically it's just 2 I/O ports connected with a straightforward bus. Practically, debug ringing, parasitic loads, parasitic inductances and capacitances, EMI, timing caveats, levels, clock synchronization...) – SF. Feb 10 '14 at 00:43
  • 4
    @CarlG: and offloading them to two separate executables that are to run on the same processor is often connected with writing some kind of OS which would arbitrate access to shared resources, synchronize timing and provide context switching; usually a task more complex than each of the sub-tasks taken separately. Keeping these things within the single binary really vastly simplifies the job. – SF. Feb 10 '14 at 00:47