What are the point of header files in C? [closed]?

Up vote 5 down vote favorite share g+ share fb share tw.

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 is 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?

C header-files link|improve this question asked Feb 2 '10 at 14:20alex97.3k28125259 91% accept rate.

1 The first answer to this stackoverflow.com/questions/1167875/c-he... covers what you want. – anon Feb 2 '10 at 14:23 1 Another stackoverflow.com/questions/333889 – Brian Rasmussen Feb 2 '10 at 14:24 1 And stackoverflow.com/questions/1945846/… ... Really, why this is voted up 3 times? – MSalters Feb 2 '10 at 14:36.

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.

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.

The main reason header exists is to share declarations among multiple source files. Say you have the function float *f(int a, int b) that you define in the file a. C and reuse in b.

C and d.c. To allow the compiler to properly check arguments and return values you either put the function prototype in and 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.

Know what is in a library at your disposal. 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.

Reduce compiler load. Why should it know in screen display procedures about deep database engine? Let it learn only of functions it needs now.

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. Alternate configs.

Makefile decides which header to use, and the same code may service two different platforms given two different header files. Probably more.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions