what is header file and how it works in c/c++ programming?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abcdefghijklmnopqrst
    New Member
    • Aug 2010
    • 1

    #1

    what is header file and how it works in c/c++ programming?

    what is header file and how it works in c/c++ programming?
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Header files contain declarations so that compiler understands what to do when it finds certain functions in the code.
    For eg. when cout is there in some code, it never defines cout, so the compiler would know what cout is by seeing header “iostream” which we usually define as
    Code:
    #include <iostream>

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What we casually call compilation actually consists of three steps: preprocessing, compiling, linking.

      The preprocessor opcodes all start with "#" (#include, #define, #if, etc.). In particular, it is the preprocessor that knits together all the files referenced by #include's into a single virtual source file that is passed to the subsequent build steps. The preprocessor neither understands nor cares what our intended purpose is for all those #included files; and the fact that certain pieces of the virtual source file came from different #included files is hidden from the compiler and linker.

      Over the years, the C/C++ programming community have developed idioms and style rules for how best to use the #include feature. The term "header file" comes from the style rule that all #include statements ought to be at the top of the source file.

      To understand header files and how they work you need to first understand the underlying #include feature and then the idioms and style rules that govern its use.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Adding to what has been said, suppose you have a struct of your own. Let's say it has 100 members. And let's say you have 1000 source files in your program.

        Your choices are:
        1) maintain a copy of the struct in each of the 1000 files. So when you add/remove a member of the struct you will have to change 1000 files.
        2) put the struct in a header file and #include the header file in the 1000 source files. Now when you add/remove a struct member you change only the struct in header file and recompile your program.

        Just pick choice 1 or choice 2.

        Comment

        • sharpdevloper
          New Member
          • Feb 2012
          • 1

          #5
          Header file contains functions, for example string.h header file contains function related to string like strrev, strcat etc. if you want to use that function then you have to add that header file. You can read more about header file here, It helped me a lot.

          Comment

          • coderHead
            New Member
            • Feb 2012
            • 6

            #6
            Actually, string.h doesn't contain the functions themselves, it only contains the prototypes of those functions. The functions themselves reside in the library, and are linked to the program by the linker. But because those functions are being called before they are linked in, the compiler needs to know about them and the header files provide the basic information about them.

            You can put anything in a header file that you want - it just gets typed out into your program by the pre-processor. For example, I could make a file called "myheader.h " and put this in it: Hello, my name is coderFoo! Then, I could put this line in my program:

            Code:
            #include<myheader.h>
            and then that header file would be replaced by the phrase: Hello, my name is coderFoo! and of course it would cause an error from the compiler.
            In other words, whatever is in the header file is simply typed out where it's at when you start to compile the program.

            You can put function definitions in there if you want, but all of the standard library functions remain in the library until compile - link time, and only the function prototypes are in those header files.
            Last edited by coderHead; Feb 7 '12, 06:51 AM. Reason: word correction

            Comment

            Working...