10-19-2012, 05:37 PM | #1 |
Banned
Join Date: Aug 2006
Posts: 324
|
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. |
10-19-2012, 07:56 PM | #2 | ||
stepmania archaeologist
Join Date: Aug 2005
Age: 34
Posts: 4,090
|
Re: C++ preprocessor directives
Quote:
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:
#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.. |
||
10-19-2012, 08:15 PM | #3 |
FFR Player
|
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.
__________________
|
10-19-2012, 08:21 PM | #4 |
Banned
Join Date: Aug 2006
Posts: 324
|
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.. |
10-19-2012, 08:28 PM | #5 | |
FFR Simfile Author
|
Re: C++ preprocessor directives
Quote:
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 |
|
10-19-2012, 08:32 PM | #6 | |
FFR Simfile Author
|
Re: C++ preprocessor directives
Quote:
__________________
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 |
|
10-19-2012, 08:45 PM | #7 | |
FFR Player
|
Re: C++ preprocessor directives
Quote:
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.
__________________
|
|
10-19-2012, 08:55 PM | #8 | |
Banned
Join Date: Aug 2006
Posts: 324
|
Re: C++ preprocessor directives
Quote:
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. |
|
10-19-2012, 08:59 PM | #9 |
stepmania archaeologist
Join Date: Aug 2005
Age: 34
Posts: 4,090
|
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) |
10-20-2012, 12:33 AM | #10 |
FFR Veteran
Join Date: Oct 2007
Age: 33
Posts: 94
|
Re: C++ preprocessor directives
preprocessor statements have some pretty important uses in anything that's meant to be multiplatform
__________________
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|