header file reg

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeyshree
    New Member
    • Jul 2010
    • 75

    header file reg

    Hai,
    I wrote two functions in separate files named fibonacci.c and factorial.c
    Will you please let me know how to place those two functions in a header file named hai.h,so that i can use them in other c files?Thanks for your reply in advance.
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    in hai.h, declare the function prototypes.

    Code:
    #ifndef __MYHEADER_HAI__
    #define __MYHEADER_HAI__
    
    void foo_1(void); // from fibonacci.c
    void foo_2(void); // from factorial.c
    
    #endif
    include the header in the source files

    Code:
    // fibonacci.c
    
    #include "hai.h"
    
    void foo_1(void) {
       // definition
    }
    Code:
    // factorial.c
    
    #include "hai.h"
    
    void foo_2(void) {
       // definition
    }
    in the main source file

    Code:
    #include "hai.h"
    
    int main(void) {
       // do something with foo_1() and foo_2()
    }
    compile the sources; for example
    cl main.c fibonacci.c factorial.c

    I usually have good luck with something like that.

    Comment

    Working...