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.