Question:

What is purpose of header files in C and C++? and Why we need header files in C and C++?

by  |  earlier

0 LIKES UnLike

What is purpose of header files in C and C++? and Why we need header files in C and C++?

 Tags:

   Report

3 ANSWERS


  1. Hex is correct as far as he goes.  Among other things, it allows us to keep our programs at a manageable size, by removing related routines, or even just debugged routines, to discreet header files and often libraries.

    Another issue my old C teacher introduced with the comment, C itself is a very powerful language. but without libraries it doesn't allow you to do very much.

    Over the last few years I've gotten a lot of questions about the windows.h file.  These have increased since I switched completely to Linux and X-Windows, which doesn't have a windows.h file.  There are certainly some routines which everyone uses.  There are also routines which groups of people use others have no use for (#include conio.h vs. #include curses.h).  There is no reason to either build them into the language itself or to make us reinvent our OSes to let us use them.  Header files and libraries make the language extensible.  It gives us the ability to do a lot we might otherwise find way too cumbersome, and to share our work with other people in an an efficient and economical (economics is defined as the allocation of scarce resources, such as time) manner.


  2. Header files allows the programmer to sort and skew his code in a way

    that it can be accessed without confusion or issue.

    If you're creating a very large application that is thousands of lines of

    code then you want to break your application up into parts. These

    parts are put into different libraries called Header files.

    An example would be what I am working on after my break is done.

    I'm currently creating a compiler, for this compiler I require about

    32 different header files, and each file contains many many lines of

    code which will allow me to access custom functions or classes

    that I've created.

    Some of their names:

    File.h

    Streams.h

    Sysconfig.h

    String_Custom.h

    Format;h

    structLimits.h

    iostream

    io.h

    etc. etc.

    In a nutshell -- Header files are a good way to move around code

    without having to re-write it over and over again. You just include

    the header file and go back to business.

    Words of wisdom,

    - Hex

  3. Header files keep the function declaration separate from function definition. Your programs often need to use functions "defined" elsewhere... inside other .c/.cpp files or inside binary .obj files. Using the header files, you can "compile" your programs without needing the function definition.

    Now, for example you have a function foo:

    // the part below is the declaration -- should go to a .h file

    int foo( int, float );

    // the part below is the definition -- should go to a .c/.cpp file

    int foo( int a, float b )

    {

    return something;

    }

    You can compile and link your function. From your .c/.cpp file, a .obj file is generated. You can now distribute your obj file along with the .h file. You now do not have to distribute the actual code.

    This is how you compile programs for windows... you don't have the source code for the windows functions. Instead you include windows.h file in your programs and the linker extracts the binary function definitions from the .obj files and merges them into your program's .exe file.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.