Flash Flash Revolution

Flash Flash Revolution (http://www.flashflashrevolution.com/vbz/index.php)
-   Technology (http://www.flashflashrevolution.com/vbz/forumdisplay.php?f=74)
-   -   C++ preprocessor directives (http://www.flashflashrevolution.com/vbz/showthread.php?t=126714)

SlayerApocalypse666 10-19-2012 05:37 PM

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 10-19-2012 07:56 PM

Re: C++ preprocessor directives
 
Quote:

Originally Posted by SlayerApocalypse666 (Post 3789810)
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 (Post 3789810)
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.

FissionMailed1 10-19-2012 08:15 PM

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.

SlayerApocalypse666 10-19-2012 08:21 PM

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.

Patashu 10-19-2012 08:28 PM

Re: C++ preprocessor directives
 
Quote:

Originally Posted by SlayerApocalypse666 (Post 3789810)
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 10-19-2012 08:32 PM

Re: C++ preprocessor directives
 
Quote:

Originally Posted by SlayerApocalypse666 (Post 3789858)
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.

FissionMailed1 10-19-2012 08:45 PM

Re: C++ preprocessor directives
 
Quote:

Originally Posted by SlayerApocalypse666 (Post 3789858)
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.

SlayerApocalypse666 10-19-2012 08:55 PM

Re: C++ preprocessor directives
 
Quote:

Originally Posted by FissionMailed1 (Post 3789864)
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.

qqwref 10-19-2012 08:59 PM

Re: C++ preprocessor directives
 
Yeah, those are fine, you can do it either way.

BahamutZER0 10-20-2012 12:33 AM

Re: C++ preprocessor directives
 
preprocessor statements have some pretty important uses in anything that's meant to be multiplatform


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

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