how to create a header file in c please explain by defining simple function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnagJohari
    New Member
    • May 2010
    • 96

    how to create a header file in c please explain by defining simple function?

    please tell me how ro define a header file for a perticular function so i can use in another file
    please explain by giving example of any simple function suppose like addition of number just wanna know the basic concept.
    thank you
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Supppose you have this function in one file:

    Code:
    void Hello()
    {
       printf("Hello\n");
    }
    and you need to call it in another file:

    Code:
    Hello();
    In this case the compiler will produce an error that the Hello function does not exist. You know it does exist in the other file, so you tell the compiler that there is a Hello() function but it's no in this file:

    Code:
    void Hello();
    Hello();
    The function prototype tells the compiler not to worry about the missing Hello() function. The linker will find it.

    Now you have a choice: a) write "void Hello();" in all 5000 of your source files or b) write "void Hello() once in a file by itself and include that file in all 5000 source files.

    If you choose (b), then in one file you write:

    Code:
    MyStuff.txt
    
    void Hello();"
    And include this file in your other source files:

    Code:
    #include <MyStuff.txt>
    Hello();
    Since these files are included at the top (or head) of the source files, they are called header files. By convention in C, they have a .h extension. In C++, they have no extension:

    Code:
    C
    #include <MyStuff.h>
    Hello();
    
    C++
    #include <MyStuff>
    Hello();

    Comment

    Working...