Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Preprocessor confusion

Preprocessor confusion

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    s196675m
    wrote on last edited by
    #1

    Here is two sets of preprocessor I did not understand completely, First one is trying to define the class if it is not already defined and the second one is trying to define a header file if it is not already defined. But my quesion is what is the syntax or rules for the identifier. Why there is a underscore before the word Class and H (in second example). The way I understand is _Class means for Class and _H means for Header. In header case, it is all upper case. Why it that. I understand #define, #ifndef but I am not clear about the systax for identifier. If any body can answer my confusion or direct me to any article I will be glad.

    ////////////////////////////
    #ifndef TEggCodeParser_Class
    #define TEggCodeParser_Class
    ////////////////////////////

    ////////////////////////////
    #if !defined( EXAMPLE_H )
    #define EXAMPLE_H
    //////////////////////////

    Thank you.

    M C 2 Replies Last reply
    0
    • S s196675m

      Here is two sets of preprocessor I did not understand completely, First one is trying to define the class if it is not already defined and the second one is trying to define a header file if it is not already defined. But my quesion is what is the syntax or rules for the identifier. Why there is a underscore before the word Class and H (in second example). The way I understand is _Class means for Class and _H means for Header. In header case, it is all upper case. Why it that. I understand #define, #ifndef but I am not clear about the systax for identifier. If any body can answer my confusion or direct me to any article I will be glad.

      ////////////////////////////
      #ifndef TEggCodeParser_Class
      #define TEggCodeParser_Class
      ////////////////////////////

      ////////////////////////////
      #if !defined( EXAMPLE_H )
      #define EXAMPLE_H
      //////////////////////////

      Thank you.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      You can use any valid identifier. Underscores can be used to prevent future clashes with similar identifiers, or just to indicate that the identifier is a macro. Just a matter of convention. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • S s196675m

        Here is two sets of preprocessor I did not understand completely, First one is trying to define the class if it is not already defined and the second one is trying to define a header file if it is not already defined. But my quesion is what is the syntax or rules for the identifier. Why there is a underscore before the word Class and H (in second example). The way I understand is _Class means for Class and _H means for Header. In header case, it is all upper case. Why it that. I understand #define, #ifndef but I am not clear about the systax for identifier. If any body can answer my confusion or direct me to any article I will be glad.

        ////////////////////////////
        #ifndef TEggCodeParser_Class
        #define TEggCodeParser_Class
        ////////////////////////////

        ////////////////////////////
        #if !defined( EXAMPLE_H )
        #define EXAMPLE_H
        //////////////////////////

        Thank you.

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        In fact the rule is really simple: you can merely put 'almost' what you want. Preprocess is in fact far less complicated that you might think, it is really no more than text processing. In this case, the #define TEggCodeParser_Class can be used later as a kind of flag: if you check for the existance of TEggCodeParser_Class, you can remove certain part of your code from the compilation. The second example is used to avoid including multiple time the same header file. Don't forget that both of these conditions should be terminated by a #endif. If EXAMPLE_H was already defined (because the header file was already included for example), then everything which lies between the #if !defined and the #endif will be 'discarded'.

        Cédric Moonen Software developer
        Charting control [v1.5 - Updated] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups