help with header files..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnnash

    help with header files..

    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 ??
  • Nick Keighley

    #2
    Re: help with header files..

    On 6 Mar, 07:30, johnnash <johnnas...@gma il.comwrote:
    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





    Comment

    Working...