Thread: Stumble upon #ifndef

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    Stumble upon #ifndef

    I've never used these before, but from time to time when reading other peoples code I stumble upon #ifndef and #pragma. What does these do. they got a be in familly with #define and #include right?
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    #ifndef is most commonly seen with header files, to prevent from the class being #included twice.

    i.e.

    Code:
    #ifndef CLASS_H
    #define CLASS_H
    
    #endif
    not sure what #pragma does though.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    98
    Ok, straight from my book.The #pragma directive is a preprocessor. The #pragma directive has no formal definition.Compilers may use it in ways the compiler vender sees fit.Typical uses are to suppress and enable certain annoying warning messages.If the compiler uses the directive this way,you could use the custom #pragma to suppress compiler warning messages you do not wish to read.
    Another words, probably need to look at the documentation on your compiler.

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    #pragma is compiler specific, but here's one example of pragma use, which quiets the compiler up about unsafe boolean testing in macros (I think that's what I used it for) :

    Code:
    #pragma warning(disable:4804)
    which says: disable warning 4804

    and #ifndef is, "if the following is undefined...", compile this piece.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    what about endif then... it's always placed in my code automaticly by my compiler so I really don't know what it does.

    BTW can one use define to other things but values like including headers?
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>what about endif then... it's always placed in my code automaticly by my compiler so I really don't know what it does.
    #endif closes an #if block, if you don't use it then the compiler doesn't know where to stop preprocessing :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #ifndef explanation?
    By Zooker in forum C Programming
    Replies: 2
    Last Post: 02-12-2009, 06:59 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Warning on an ifndef directive
    By DavidP in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2007, 01:31 AM
  4. #ifndef
    By paperbox005 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 07:25 PM
  5. #ifndef wrapper to reference dll
    By joeyzt in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2004, 01:30 PM