C++ preprocessor directives

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SlayerApocalypse666
    Banned
    • Aug 2006
    • 324

    #1

    C++ preprocessor directives

    Hi guys, i've been scratching my head about these for a while now, i'm not sure WHAT and WHY they should be used for.

    They are statement that are processed before the compilation is done, *as i've read*. But ... why would you use instead of just using a regular variable in your program ?? Anybody has any idea.

    #define ZERO 0;
    #define TRUE 1;

    instead of

    int ZERO{}; or int ZERO = 0; or int ZERO = {0}; or int ZERO = {};
    int TRUE{1};

    i just don't get it guys. Plus why does #define doesnt require a type for the variable.....? i'm really confused about em.

    Thx alot.
  • qqwref
    stepmania archaeologist
    FFR Simfile Author
    • Aug 2005
    • 4092

    #2
    Re: C++ preprocessor directives

    Originally posted by SlayerApocalypse666
    But ... why would you use instead of just using a regular variable in your program ??
    Basically, it's not a real variable, and it literally gets replaced in your code before it compiles. So it makes the code a little faster because it doesn't ever have to store or look up a variable. You can get similar results by declaring variables to be const, but #define is also guaranteed to apply to everything in your program.

    For a simple case (just defining a variable for a single-file program) there's very little advantage, but one cool thing is that because #defines are outside of the program itself you can make more complicated stuff happen. For instance, you can have it so certain parts of the code only make it into the final program if you have #defined the "DEBUG" variable to true. Once you've done that, you can put a "#define DEBUG true" at the very start to create the debug version of your program, and simply changing it to false will remove all of the debug code all at once. You can even do similar stuff with versions and literally have all the versions of a program in one file, with just one line needing to be changed to compile a different version of your code.

    Originally posted by SlayerApocalypse666
    i just don't get it guys. Plus why does #define doesnt require a type for the variable.....? i'm really confused about em.
    Again, the trick here is that it's not really a variable - #define actually replaces the text in the code. So if you have a constant integer ONE equal to 1, and you write "2 + ONE" in your code, if you compile and run it, your computer has to look up the value of ONE and add it to two before printing out the result. But if ONE is set with #define, then the code actually compiles into the statement "2 + 1". When the program is run, the computer just sees the equivalent of "2 + 1" and never knows there was a ONE in there in the first place. You can even #define more complicated expressions, like this:

    #define ELSEDIE else{exit(-1);}

    So then when you write something like

    if (x = 5) {
    return 5;
    } ELSEDIE

    it actually gets expanded out into an entire else clause. You can easily see how this couldn't have been represented as a variable.
    Last edited by qqwref; 10-19-2012, 06:58 PM.
    Best AAA: Policy In The Sky [Oni] (81)
    Best SDG: PANTS (86)
    Best FC: Future Invasion (93)

    Comment

    • FissionMailed1
      FFR Player
      • Feb 2012
      • 1267

      #3
      Re: C++ preprocessor directives

      Don't use macros in C++ unless you absolutely have to (e.g. include guards). They are not type safe and do not obey scope.

      #define ZERO 0;
      #define TRUE 1;
      is mostly in C code. Even then, the "stdbool.h" header should be used instead since this is in this header.

      Also, as qqwref said, they are faster since they are direct plain text substitutions rather than variables.


      YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

      MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

      YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

      YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

      THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

      I LOVE YOU CHEETOS.

      Comment

      • SlayerApocalypse666
        Banned
        • Aug 2006
        • 324

        #4
        Re: C++ preprocessor directives

        Thanks alot, also i would like to know if i should use Constants or Define for my special characters, like this.

        #define EAIGUE char(130)
        #define AGRAVE char(133)
        #define ACIRCONFLEX char(131)
        #define EGRAVE char(138)
        #define ETREMA char(137)
        #define MEAIGUE char(144)

        since i'm french i put this at the beginning of my programs for characters with accents instead of having to remember the ascii code, is it a bad practice ?

        edit: this is only for outputs in dos on my 1 system, i do not need to import the program on other OS's or machines.
        Last edited by SlayerApocalypse666; 10-19-2012, 07:25 PM.

        Comment

        • Patashu
          FFR Simfile Author
          FFR Simfile Author
          • Apr 2006
          • 8609

          #5
          Re: C++ preprocessor directives

          Originally posted by SlayerApocalypse666
          Hi guys, i've been scratching my head about these for a while now, i'm not sure WHAT and WHY they should be used for.

          They are statement that are processed before the compilation is done, *as i've read*. But ... why would you use instead of just using a regular variable in your program ?? Anybody has any idea.

          #define ZERO 0;
          #define TRUE 1;

          instead of

          int ZERO{}; or int ZERO = 0; or int ZERO = {0}; or int ZERO = {};
          int TRUE{1};

          i just don't get it guys. Plus why does #define doesnt require a type for the variable.....? i'm really confused about em.

          Thx alot.
          Macros for things like that are not very interesting, unless you want to programmatically define a constant that doesn't take up any space in the memory of your program.

          More interesting macros define constants, short pieces of logic and do token pasting ( http://en.wikipedia.org/wiki/C_prepr..._concatenation ). However, because the preprocessor is not very smart and does not do type checking, can change the logical structure of your program (beware macros that insert {}s, ;s, ifs, fors and so on - they might not do what you expect depending on what you surround them with!) you have to be careful about how you use macros.
          Patashu makes Chiptunes in Famitracker:
          http://soundcloud.com/patashu/8bit-progressive-metal-fading-world
          http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png
          Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1)
          http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png

          Comment

          • Patashu
            FFR Simfile Author
            FFR Simfile Author
            • Apr 2006
            • 8609

            #6
            Re: C++ preprocessor directives

            Originally posted by SlayerApocalypse666
            Thanks alot, also i would like to know if i should use Constants or Define for my special characters, like this.

            #define EAIGUE char(130)
            #define AGRAVE char(133)
            #define ACIRCONFLEX char(131)
            #define EGRAVE char(138)
            #define ETREMA char(137)
            #define MEAIGUE char(144)

            since i'm french i put this at the beginning of my programs for characters with accents instead of having to remember the ascii code, is it a bad practice ?

            edit: this is only for outputs in dos on my 1 system, i do not need to import the program on other OS's or machines.
            Doing that is fine.
            Patashu makes Chiptunes in Famitracker:
            http://soundcloud.com/patashu/8bit-progressive-metal-fading-world
            http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png
            Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1)
            http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png

            Comment

            • FissionMailed1
              FFR Player
              • Feb 2012
              • 1267

              #7
              Re: C++ preprocessor directives

              Originally posted by SlayerApocalypse666
              Thanks alot, also i would like to know if i should use Constants or Define for my special characters, like this.

              #define EAIGUE char(130)
              #define AGRAVE char(133)
              #define ACIRCONFLEX char(131)
              #define EGRAVE char(138)
              #define ETREMA char(137)
              #define MEAIGUE char(144)

              since i'm french i put this at the beginning of my programs for characters with accents instead of having to remember the ascii code, is it a bad practice ?

              edit: this is only for outputs in dos on my 1 system, i do not need to import the program on other OS's or machines.
              Have you tried doing something like:
              const char EAIGUE = char(130);
              const char AGRAVE = char(133);
              const char ACIRCONFLEX = char(131);
              const char EGRAVE = char(138);
              const char ETREMA = char(137);
              const char MEAIGUE = char(144);

              And no, it isn't bad practice.


              YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

              MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

              YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

              YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

              THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

              I LOVE YOU CHEETOS.

              Comment

              • SlayerApocalypse666
                Banned
                • Aug 2006
                • 324

                #8
                Re: C++ preprocessor directives

                Originally posted by FissionMailed1
                Have you tried doing something like:
                const char EAIGUE = char(130);
                const char AGRAVE = char(133);
                const char ACIRCONFLEX = char(131);
                const char EGRAVE = char(138);
                const char ETREMA = char(137);
                const char MEAIGUE = char(144);

                And no, it isn't bad practice.
                Yep, just tried, it works pretty well but i wonder if it doesnt use useless space since they are declared variables. But yeah it works just the same.
                Tho i'm doing this.
                const char EAIGUE{char(130)};
                const char AGRAVE{char(133)};
                const char ACIRCONFLEX{char(131)};
                const char EGRAVE{char(138)};
                const char ETREMA{char(137)};
                const char MEAIGUE{char(144)};

                cause i think its prettier lmao.

                Comment

                • qqwref
                  stepmania archaeologist
                  FFR Simfile Author
                  • Aug 2005
                  • 4092

                  #9
                  Re: C++ preprocessor directives

                  Yeah, those are fine, you can do it either way.
                  Best AAA: Policy In The Sky [Oni] (81)
                  Best SDG: PANTS (86)
                  Best FC: Future Invasion (93)

                  Comment

                  • BahamutZER0
                    FFR Veteran
                    • Oct 2007
                    • 94

                    #10
                    Re: C++ preprocessor directives

                    preprocessor statements have some pretty important uses in anything that's meant to be multiplatform

                    Comment

                    Working...