Thread: Online Resources for Preprocessor Directives

  1. #1
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38

    Online Resources for Preprocessor Directives

    Does anyone know of a tutorial or a lesson on Preprocessor Directives?
    I looked at MSDN Visual C++ refrence material but it is written in such a way that I barely understand it. I am looking for something that explains it in plain english!
    We're all Fu##ed aren't we?

  2. #2
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    Here is one: http://www.cplusplus.com/doc/tutorial/preprocessor.html

    There really aren't many preprocessor directives. The ones you will most likely use are:

    Code:
    #define
    #include
    #ifdef
    #else
    #endif
    #if 
    #pragma
    Honestly, I never really used anything else but those.
    Founder and avid member of the Internationsl Typo Associateion

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How much explaining does #include, #define and #if take?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    #define is quite complex - macro substitution has some strange rules.

    There's also #error, which is very simple.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I've never used #line myself, but I've used tools which have (i.e. bison).

    I've often used defined(), which isn't a preprocessor directive, but it is related. And what about #elif?

    (Side question: are you supposed to put double-quotes around the text after a #error directive?)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    (Side answer: no.)

    defined() is a preprocessor keyword.

    #if cond1
    #elif cond2
    #endif

    ==

    #if cond1
    #else
    #if cond2
    #endif
    #endif

    Some other preprocessors don't support #elif, by the way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    (Aside: thank you.)

    As far as I know, #elif is ANSI standard . . . are you referring to other types of preprocessors, or what?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Old, old, old preprocessors. But they still exist in some unices.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I believe there is also #ifndef, #undef and the string-ify operation.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Stringify, yes, and token concatenation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Don't forget the Preprocessor variables too, like __LINE__ and such, if you want to learn about the Preprocessor capabilities. I used __LINE__ just the other day writing a return code checker macro when writing a C program that uses a lot of the new "xxxx_s" safe functions.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in distributing .exe online
    By GamesSmash in forum C++ Programming
    Replies: 31
    Last Post: 11-27-2007, 04:41 PM
  2. online resources for win dev?
    By ItalianStallion in forum Windows Programming
    Replies: 2
    Last Post: 07-26-2005, 10:09 AM
  3. 2D Spaceship shooter online - Need 2 keen programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 05-19-2005, 07:40 AM