Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of header files in C? [duplicate]

Tags:

c

header-files

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?

like image 499
alex Avatar asked Feb 02 '10 14:02

alex


People also ask

What happens if we include a header file twice in C?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.

What is the point of C header files?

The header file eliminates the labor of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program. In C, the usual convention is to give header files names that end with .

What is the reason for header files?

The purpose of header files are to give the compiler the information it needs to share definitions between compilation units (. cpp source files).

Why include header files instead of C files?

Because the C-library contains pre-compiled versions of the library functions that are then linked with your source file by the linker. You need to differentiate between header files, source files, object files, and library files.


2 Answers

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.

like image 68
unwind Avatar answered Oct 29 '22 22:10

unwind


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.

like image 40
Hans Insulander Avatar answered Oct 29 '22 21:10

Hans Insulander