5

Why do we include header files in C

I cant understand what is the need of adding header files in C. I mean what will happen if I don't use them

Anirudh Garg
  • 87
  • 1
  • 2
  • 5

7 Answers7

22

It was originally a compilation problem with cross recursion :

void foo() {
  //bar? what is this function???
  bar();
}

void bar() {
  //I know foo() because it appears before. 
  foo();
}
  • foo can call bar only if it is defined before.
  • bar can only call foo if it is defined before

So which one do we define first???

To solve this problem came the prototypes.

//prototype of foo()
void foo();
//prototype of bar()
void bar();

void foo() {
  // I can call bar() because I know it exists
  bar();
}

void bar() {
  // I can call foo() because I know it exists
  foo();
}

Then the prototypes were gathered in a .h files.

It then became a good practice as it separates the interface from the implementation.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
9

Large projects are compiled into object files ".o" and then linked together in one binary file.

This means that when compiling, if you access functions that are located in another object file, the compiler has no knowledge of them, so by including an .h file you declare the functions that will be available at link time, so the compile trusts that they will be there without raising an error about a missing function.

LtWorf
  • 7,286
  • 6
  • 31
  • 45
  • Sorry but header files are not primarily intended for that. You can directly `#include "another.c"` and it will work provided you don't have problem of cross recursion. Please check my answer for more explanation. (But I admit that in most cases `#include "another.c"` is a very bad practice). – Arnaud Denoyelle Sep 30 '13 at 08:51
  • 1
    Sure it will work... It will also need much more time to compile, because it will recompile everything and not just the files that have been changed. – LtWorf Sep 30 '13 at 10:42
4

There is no actual need of using them. They spare you from including the definitions for all the functions you're using in every source file you have. Header files are nothing more than inserting the contents of them at the place where you use #include. You can write all that on your own if you want to.

Joey
  • 344,408
  • 85
  • 689
  • 683
3

Wikipedia:

A header file is a file that allows programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers. Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required. This is to keep the interface in the header separate from the implementation.

sara
  • 3,824
  • 9
  • 43
  • 71
0

Declarations from other header files will not be visible in that file, unless you include the header or provide an identical declaration (exact duplicate).

Using a header file is the easiest (read: only sane) way to use another program within that source file. By including, you direct the compiler to read the file's contents, so that you can easily use anything in that file.

justin
  • 104,054
  • 14
  • 179
  • 226
0

Computers cannot find out anything by itself.It is just what we tells it does[programs].The files we include are doing the same,It gives the basic concept about the commands we use in our program to how to make it work.This information exist in the included files.

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

It is useful for hiberarchy design. You can write c without headers, then you will see that every source file becomes too big to understand or use, and when you need the same code, you need copy it or rewrite it.With 'headers', we could write different part of the system into headers respectively, then we could test or reuse it.

YaleCheung
  • 630
  • 3
  • 9