Thread: Header file include guards

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Header file include guards

    Hi all, odd question time again.

    I see include guards (I believe that is what they are called) used all the time. But I am not 100% sure of their use and syntax. There is nothing in the books that I own, and I can't find a great deal on the net, (but I may be searching for the wrong terms). I am pretty sure they are used to prevent a header file being included multiple times.

    a: Why the preceding double underscore?
    b: I read somewhere that the double underscore shouldn't be
    used for user defined header files, as these are reserved for
    internal use, and users should use a single underscore.
    Is this true?

    Code:
    #ifndef __GAMEVERSION_H
    #define __GAMEVERSION_H
    
    #define VERSION_NUMBER 1
    
    #endif

  2. #2
    Unregistered
    Guest
    Inclusion guards prevent a given header file from being included in a given .c file more than once. This can happen when include files become nested (foo.c includes bar.h and baz.h where baz.h *also* includes bar.h).

    What you read regarding the leading double underscore (or even a single underscore) is also correct. Preceding identifiers with an underscore invades the implementation's namespace and should therefore only be done by implementors.

    For my inclusion guards, I take the name of the header file in upper case and replace the dot by an underscore. So the inclusion guard for foo.h would look like this:

    #ifndef FOO_H
    #define FOO_H

    /* ... */

    #endif

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Thanks for that - that was exactly what I was looking for.

    Just another question though, why the underscore and not a ".", What is the actual reason from the preprocessors point of view, that the underscore is used in lieu of a full stop?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just another question though, why the underscore and not a ".", What is the actual reason from the preprocessors point of view, that the underscore is used in lieu of a full stop?
    You mean, why use:

    #define SOMETHING_DEFINED

    Instead of:

    #define SOMETHING.DEFINED

    Is that what you're talking about? Basicly, what you're doing is creating a variable. This isn't actually true, but you must use the same naming convention. Think of it as naming a variable. You cannot use a '.' in a variable name. This differs from:

    #include "something"
    #include "something.h"
    #include <something>
    #include <something.h>

    Because these are actual file names, and as such, what you include there has to follow naming convention for file names.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    I guess I didn't make the connection, that is is a variable (or at least requiring that it followed the conventions of variable declarations).

    Duh!

    Thanks Quzah

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-10-2009, 02:51 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. MS Visual C++ 6 wont include vector.h in a header file
    By bardsley99 in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2003, 12:05 PM