help with header files..

Discussion in 'C Programming' started by johnnash, Mar 6, 2008.

  1. johnnash

    johnnash Guest

    ok so i was just reading about #ifndef and header files. I have a few
    doubts. It would be great help if someone can clear my doubts..

    #ifndef A_H
    #define A_H
    ....
    code
    .....
    #endif

    ^^ lets say A.h is included in B.h which is included in C.h..so how
    will the compiler resolve everything ??

    also i saw some programs where there are many files like a.h, a.c,
    b.h, b.c, c.h, c.c etc and then there is a all.h file
    which goes like this -

    #ifndef A_H
    #include "a.h"
    #endif

    #ifndef B_H
    #include "b.h"
    #endif

    #ifndef C_H
    #include "c.h"
    #endif


    then only all.h is included in a.c, b.c and c.c

    how did it work ??
    johnnash, Mar 6, 2008
    #1
    1. Advertising

  2. On 6 Mar, 07:30, johnnash <> wrote:

    > ok so i was just reading about #ifndef and header files. I have a few
    > doubts. It would be great help if someone can clear my doubts..


    what are your "doubts"? You aren't clear about what is unclear!

    #include "xyz.h"

    simply means "include the text of the file xz.h here"


    > #ifndef A_H


    if A_H (a preprocessor macro) is not defined then include (see above)
    the following text.


    > #define A_H


    define the preprocessor macro A_H. Subsequent
    #ifndef A_H will not "fail"- that is *not* include
    the following text


    > ...
    > code
    > ....
    > #endif


    marks the end of the text taht is included after a "successful"
    #if (or #ifdef or #ifndef etc).



    > ^^ lets say A.h is included in B.h which is included in C.h..so how
    > will the compiler resolve everything ??


    what is the problem? Suppose D.c includes C.h and you compile D.c
    This is what the compiler sees after the preprocessor has been.
    I've added indents to make it clearerer. I've skiiped A.h as I got
    bored :)


    D.c
    #include C.h
    #ifndef C_H
    #define C_H
    #include B.h
    #ifndef B_H
    #define B_H
    B code
    #endif
    C code
    #endif

    I don't see the problem


    > also i saw some programs where there are many files like a.h, a.c,
    > b.h, b.c, c.h, c.c etc and then there is a all.h file
    >  which goes like this -
    >
    > #ifndef A_H
    > #include "a.h"
    > #endif


    this simply does the test outside the header a.h file.
    It arguably makes for a faster compile (a.h is never
    openened) but I find it harder to administer and harder
    to read (zillions of #ifndefs to amnage)


    > #ifndef B_H
    > #include "b.h"
    > #endif
    >
    > #ifndef C_H
    > #include "c.h"
    > #endif
    >
    > then only all.h is included in a.c, b.c and c.c
    >
    > how did it work ??


    again I don't see the problem...
    a.c includes all.h which includes a.h, b.h and c.h.

    Is it the recursion that bothers you. An include file
    including an include file?

    Try working through some examples on paper


    --
    Nick Keighley
    Nick Keighley, Mar 6, 2008
    #2
    1. Advertising

  3. johnnash

    SM Ryan Guest

    # also i saw some programs where there are many files like a.h, a.c,
    # b.h, b.c, c.h, c.c etc and then there is a all.h file
    # which goes like this -

    Put the guards for file x.h inside file x.h, and everything else
    inside the guards.

    file a.h:
    #ifndef A_H
    #define A_H
    #include "b.h"
    #include "c.h"

    declarations of a.h
    #endif
    file b.h:
    #ifndef B_H
    #define B_H
    #include "a.h"
    #include "c.h"

    declarations of b.h
    #endif
    file c.h:
    #ifndef C_H
    #define C_H
    #include "a.h"
    #include "b.h"

    declarations of c.h
    #endif

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Title does not dictate behaviour.
    SM Ryan, Mar 6, 2008
    #3
  4. johnnash

    Thad Smith Guest

    SM Ryan wrote:
    > # also i saw some programs where there are many files like a.h, a.c,
    > # b.h, b.c, c.h, c.c etc and then there is a all.h file
    > # which goes like this -
    >
    > Put the guards for file x.h inside file x.h, and everything else
    > inside the guards.
    >
    > file a.h:
    > #ifndef A_H
    > #define A_H
    > #include "b.h"
    > #include "c.h"
    >
    > declarations of a.h
    > #endif
    > file b.h:
    > #ifndef B_H
    > #define B_H
    > #include "a.h"
    > #include "c.h"
    >
    > declarations of b.h
    > #endif
    > file c.h:
    > #ifndef C_H
    > #define C_H
    > #include "a.h"
    > #include "b.h"
    >
    > declarations of c.h
    > #endif


    What is the purpose of including each other?

    If you choose to use the style in which header files include other header
    files, you should either
    1. use the "all.h" style (which I seldom use) or
    2. have each include file include the other files that are needed for
    dependencies in the declaration within the outside header file.

    Your example isn't the first case. And if each header file (a.h, b.h and
    c.h) required declarations from both of the other files, they are designed
    poorly. Dependencies should be hierarchical, not circular.

    --
    Thad
    Thad Smith, Mar 8, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    692
    lilburne
    Nov 2, 2003
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    596
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,280
    Walter Roberson
    May 1, 2006
  4. Bit byte
    Replies:
    1
    Views:
    640
    benben
    Apr 19, 2006
  5. mlt
    Replies:
    2
    Views:
    820
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page