Page 1 of 1

What is "#define" good for?

PostPosted: Mon Feb 18, 2008 6:02 pm
by asmodeus
I have seen scripts like this:
Code: Select all
#define BYTE unsigned char
#define WORD unsigned short int
 BYTE red; BYTE green; BYTE blue;
 WORD Height;
WORD Width;
WORD TMP;

What is it good for and how do I use this?

Re: What is "#define" good for?

PostPosted: Mon Feb 18, 2008 9:45 pm
by Fuzzy
#define is good for all sorts of things. What does it do? it takes the first set of letters, like BYTE for example and allows you to use BYTE where ever you might find unsigned char

Note that it doesnt use ; at the end of the line. ; is a legal character for substitution.

At a basic look, it can save you some typing. Once you realize the power of it, it can do some incredible things.

It is considered good form to use uppercase letters for your #define word. To use it,
1. start in global code and type
2. #define
3. put a space
4. use a keyword in capitals.
5. put another space
6. type a series of letters you wish to assign to the keyword. Spaces count, semi-colon ; counts.
7. dont use a semi-colon on the end. this type of code will not require it, and will not use it correctly
8. press enter and do the rest of your game

some examples
#define FUZZY "is a good programmer"
#define MOVERIGHT x = x + 1
#define MOVELEFT x = x - 1

note that in the second two examples it would just be easier to type it out. If these seem like variables, they can act somewhat like them. But they have more sophisticated uses, but you cannot do things with them like variables. You couldnt say..

#define X = 1

because X would be equivalent to = 1. it would NOT be equivalent to "= 1" either!
You couldnt do

X = X + 1; because you can only redefine the value with #define.


Any questions?

Re: What is "#define" good for?

PostPosted: Mon Feb 18, 2008 11:16 pm
by Game A Gogo
yes, and my favorite usage of this is
Code: Select all
#define true=1,false=0, flag unsigned char;


then you can use like:
Code: Select all
flag Player_Dead;
Player_Dead=false;


It helps also the brain to remember some functions etc... it's almost like creating your own library.

and a question to you, Fuzzy:
Is there a need of a semi-colon if there are more then one define arguments in one line?

Re: What is "#define" good for?

PostPosted: Tue Feb 19, 2008 1:38 am
by Fuzzy
Game A Gogo wrote:yes, and my favorite usage of this is
Code: Select all
#define true=1,false=0, flag unsigned char;


Is there a need of a semi-colon if there are more then one define arguments in one line?


Dont do it like this. As far as I know all characters are valid in #define and the only way it knows to end is with the invisible EOL characters which are generated with enter key. it will also insert that = into your code and the commas too, making possible errors.

You should also not end it with ; so that all lines in the code have a ; the reason we use ; in code is to mark end of line. end of line characters are possible in strings and should not terminate a code line, so semi-colon was introduced.

I prefer this
Code: Select all
#define TRUE 1
#define FALSE !TRUE
unsigned char flag;


you can see that only the variable definition uses a ; Thats proper use. By saying FALSE !TRUE we get to define FALSE as NOT TRUE, which allows some neat tricks like a statement in code
Code: Select all
flag = !flag;


Which flips it from whatever it was before. We dont have to check it first. It also means that flag can never be anything but false or true, thus avoiding a possible error.

This is syntactically correct but there is an error. can you spot it?
Code: Select all
#declare true 1
#declare false 0
flag = true;
flag++;
if (flag == true)
{
    putpixel(x, y);
}
else
{
    setpen(255, 0, 0, 0.0, 2);
    putpixel(x,y);
}


since flag is equal to 2, neither the if nor the else statement fires. No error is reported to the user though.

another way to do the true/false is with enum
in global code..

Code: Select all
enum logical{false, true}; // sets up the true false type. not a var

enum logical flag; creates a variable of that type for use.


my first way is shorter/cleaner so i dont do it like this.

Re: What is "#define" good for?

PostPosted: Tue Feb 19, 2008 1:49 am
by Game A Gogo
oh nice indeed. but when I use like
Code: Select all
#define true=1, false=0;
there is no bugs and works perfectly. but I will keep this in mind!

Re: What is "#define" good for?

PostPosted: Tue Feb 19, 2008 2:10 am
by Fuzzy
it probably works because the #define is passed to a precompiler that sorts things like #define into proper code. In regular C there are others like #ifdef that check to see if a variable exists...
being the clever programmer that he is, makslane probably accounted for the = and the , methods. Still you should stick to the c/c++ standard as much as you can.

Re: What is "#define" good for?

PostPosted: Tue Feb 19, 2008 2:42 am
by Game A Gogo
agreed :3