Thread: Header File Clarification

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    111

    Header File Clarification

    Hiyas, I am wondering what exactly does go into a header file?

    From Wiki and a number of other pages (and assignments), I know programmers can break up larger programs into separate routines and such, and at the end everything can be put together to make the "master" program work

    From my understanding just function prototypes go into it, is that really true?

    For example,

    Code:
    In blahOther.c (library of functions)
    
    //proper headers
    
    struct node
    {
      void* data;
      struct node *left;
      struct node *right;
    }
    
    typedef struct node tree;
    
    int add(int a, int b)
    {
      return (a+b);
    }
    
    In blahTest.h
    
    #ifndef BLAHTEST 
    #define BLAHTEST
    
    extern  //do I need to include the struct??????
    
    /* adds two numbers */
    extern int add(int a, int b);
    
    #endif /* blahtest_h */
    
    
    In blahTest.c (contains main)
    
    #include "blahTest.h"
    
    ...
    
    int triple(int x)
    {
      return x*add(a,b);
    }
    Is that the jest of it or is there something major I am missing?

    Also if I am correct, and in my library of functions file (blahOther.c) I had some structs, specifically a binary tree (NOT BST), would I need to provide the blahText.h the typedefs for those structs?

    The whole purpose of my blahTest.c is just to test my library of functions.

    Of course all this is considering, that I am compiling the following way.

    gcc -c blah.c

    gcc -c blahTest.c

    gcc blah.o blahTest.o -o test

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    As a general rule headers contain no active code or variables... just definitions, prototypes and typedefs... So yes, you've got the idea.

    You don't need extern for for function prototypes.
    You do need it for variables declared in a different file...
    You only need to include the struct if one of your functions uses it in a different file.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Sweet, TY CommonTater

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File operation: Clarification for fwrite.
    By sureshreddy.ap in forum C Programming
    Replies: 2
    Last Post: 01-04-2011, 05:54 AM
  2. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  3. Including header file with in the header file
    By Arafat_211184 in forum C Programming
    Replies: 13
    Last Post: 12-19-2005, 10:03 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. Replies: 6
    Last Post: 04-02-2002, 05:46 AM