tags:

views:

124

answers:

3

Possible Duplicates:
Why does C++ need a separate header file?
In C++ why have header files and cpp files?

Hi geeks,

I have been using header files for almost everyday. It's kind of an instinct to me now. Suddenly, I ask myself, What on earth do we include a header file for?

Currently, I think of the following reason:

The header file is the only source of clue for the compiler to generate the proper code to push the parameters of a certain method onto the stack. And thus is the invoking of that method is possible. The invoking action could be resolved by the linker at load time or run time, but the preparation instructions (i.e. parameter pushing) must be generated by the compiler at compile time.

Thanks for correcting me.

A: 

A Header file is simply a collection of functions already written for you. It is perfectly possible for you to write everything from scratch any of the functions you might use from a header file....but as you may see "why reinvent the wheel......(so many times)?"

You may/ may not choose the ones you need. So they are grouped together according to similar functions like for header file for Input/Output, System calls....etc. The compiler as you may have noticed, compiles all those lines inside the header files, treating them as your own single code file, merging with your own. So there is no question of handling them differently.

loxxy
Thanks loxxy for your answer.
smwikipedia
A: 

To separate declaration from definition

Edit: I see this q is widely duplicated too

Steve Townsend
A: 

It is one possible way of solving a couple of problems. Some other languages have different ways of solving the problems.

As you note, one problem is that the compiler may need certain information about a function, for instance that it is implemented to use Pascal linkage (arguments pushed left-to-right, called function restores stack, must have fixed number of arguments) instead of C linkage (arguments pushed right-to-left, caller restores stack, can have variable number of arguments).

The other problem is that the compiler only needs some information; by using a separate header file we don't have to give the compiler the full implementation file.

The simple (but hardly satisfying) answer to the question is that of the many possible ways to solve certain problems, and the many different ways other languages do it, this is the way C and C++ do it.

Mark Lutton
Thanks Mark. Your answer is a clean shot. :) Sorry for duplication.
smwikipedia