Old 10-19-2012, 05:37 PM   #1
SlayerApocalypse666
Banned
FFR Veteran
 
Join Date: Aug 2006
Posts: 324
Default 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.
SlayerApocalypse666 is offline   Reply With Quote
Old 10-19-2012, 07:56 PM   #2
qqwref
stepmania archaeologist
Retired StaffFFR Simfile AuthorFFR Veteran
 
qqwref's Avatar
 
Join Date: Aug 2005
Age: 34
Posts: 4,090
Default Re: C++ preprocessor directives

Quote:
Originally Posted by SlayerApocalypse666 View Post
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.

Quote:
Originally Posted by SlayerApocalypse666 View Post
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.
__________________
Best AAA: Policy In The Sky [Oni] (81)
Best SDG: PANTS (86)
Best FC: Future Invasion (93)

Last edited by qqwref; 10-19-2012 at 07:58 PM..
qqwref is offline   Reply With Quote
Old 10-19-2012, 08:15 PM   #3
FissionMailed1
FFR Player
 
FissionMailed1's Avatar
 
Join Date: Feb 2012
Location: Massachusetts
Age: 33
Posts: 1,267
Send a message via Skype™ to FissionMailed1
Default 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.
FissionMailed1 is offline   Reply With Quote
Old 10-19-2012, 08:21 PM   #4
SlayerApocalypse666
Banned
FFR Veteran
 
Join Date: Aug 2006
Posts: 324
Default 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 at 08:25 PM..
SlayerApocalypse666 is offline   Reply With Quote
Old 10-19-2012, 08:28 PM   #5
Patashu
FFR Simfile Author
Retired StaffFFR Simfile Author
 
Patashu's Avatar
 
Join Date: Apr 2006
Location: we traced the call...it's coming from inside the house
Age: 33
Posts: 8,609
Send a message via AIM to Patashu Send a message via MSN to Patashu Send a message via Yahoo to Patashu
Default Re: C++ preprocessor directives

Quote:
Originally Posted by SlayerApocalypse666 View Post
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
Patashu is offline   Reply With Quote
Old 10-19-2012, 08:32 PM   #6
Patashu
FFR Simfile Author
Retired StaffFFR Simfile Author
 
Patashu's Avatar
 
Join Date: Apr 2006
Location: we traced the call...it's coming from inside the house
Age: 33
Posts: 8,609
Send a message via AIM to Patashu Send a message via MSN to Patashu Send a message via Yahoo to Patashu
Default Re: C++ preprocessor directives

Quote:
Originally Posted by SlayerApocalypse666 View Post
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
Patashu is offline   Reply With Quote
Old 10-19-2012, 08:45 PM   #7
FissionMailed1
FFR Player
 
FissionMailed1's Avatar
 
Join Date: Feb 2012
Location: Massachusetts
Age: 33
Posts: 1,267
Send a message via Skype™ to FissionMailed1
Default Re: C++ preprocessor directives

Quote:
Originally Posted by SlayerApocalypse666 View Post
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.
FissionMailed1 is offline   Reply With Quote
Old 10-19-2012, 08:55 PM   #8
SlayerApocalypse666
Banned
FFR Veteran
 
Join Date: Aug 2006
Posts: 324
Default Re: C++ preprocessor directives

Quote:
Originally Posted by FissionMailed1 View Post
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.
SlayerApocalypse666 is offline   Reply With Quote
Old 10-19-2012, 08:59 PM   #9
qqwref
stepmania archaeologist
Retired StaffFFR Simfile AuthorFFR Veteran
 
qqwref's Avatar
 
Join Date: Aug 2005
Age: 34
Posts: 4,090
Default 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)
qqwref is offline   Reply With Quote
Old 10-20-2012, 12:33 AM   #10
BahamutZER0
FFR Veteran
FFR Veteran
 
BahamutZER0's Avatar
 
Join Date: Oct 2007
Age: 33
Posts: 94
Default Re: C++ preprocessor directives

preprocessor statements have some pretty important uses in anything that's meant to be multiplatform
__________________
BahamutZER0 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 10:09 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution