• ### Popular Now

• 16
• 15
• 12
• 9
• 10

#### Archived

This topic is now archived and is closed to further replies.

# The destructive power of a \ ???

This topic is 5118 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Working with defines and comments today, I found out how destructive '\' can be be! Look at this example:
#define SB_TOP_OFFSET 10 //comment comment comment
#define SB_SCORE_OFFSET 50 //comment comment comment

I use both these defines in my program and it works fine... but when I add a simple \ at the end of the comment:
#define SB_TOP_OFFSET 10 //comment comment comment\
#define SB_SCORE_OFFSET 50 //comment comment comment

and try to use SB_SCORE_OFFSET - BAM - undefined object!!! Can anyone explain this ? if I put anything after the \, even a space bar, it'll work fine... The only thing I came up with is that the compiler takes \ and # from the next line and treats it as one character '\#' , therefore putting my bottom line "up" and including it in my first comment (so the whole second line becomes a part of my first line's commnet)... is that the case? ??? [edited by - Koobazaur on March 19, 2004 7:21:17 PM] [edited by - Koobazaur on March 19, 2004 7:21:57 PM] [edited by - Koobazaur on March 19, 2004 7:22:11 PM] [edited by - Koobazaur on March 19, 2004 7:22:34 PM] [edited by - Koobazaur on March 19, 2004 7:22:48 PM]

##### Share on other sites
It''s a very useful feature of the preprocessor.

The ''\'' at the end means the next line is a continuation of
the current line.

It''s hardly a "destructive power".

Kami no Itte ga ore ni zettai naru!

##### Share on other sites
ooh...ok, got me confused there :D ... damn those small details no one ever remembers

##### Share on other sites
''\'' is not destructive, it means that next line is part of this line.

For example:
#define SOMETHING(x) { \
x++; \
if (x > 0) \
printf("x is greater than zero\n"); \
}

Now in your program, you can write:
SOMETHING(54);
and it will increment x and print the statement above.
And if you pass -1, it will increment x to 0, and so if statement will fail, and you won''t see nothing.

If you put a space after ''\'' character, it no longer means that next line should be counted as this line.

##### Share on other sites
#define will define everything up to the end of line to a symbol. For instance

If you say :

#define X "TEST"

The X is "TEST"

but if you do this:

#define X "TEST" );

The X is "TEST" );

You can span multiple lines by placing a backslash '\' at the end of the line.

#define X "test""This is a test"

Now X is "test""This is a test"

#define SB_TOP_OFFSET 10 //comment comment comment#define SB_SCORE_OFFSET 50 //comment comment comment

SB_TOP_OFFSET is defined as the 10 and comment, as well as the next line:

10 //comment comment comment#define SB_SCORE_OFFSET 50 //comment comment comment

Hope this helps. . .

[edited by - ForeverStarlight on March 19, 2004 7:36:16 PM]

##### Share on other sites
Don''t abuse the preprocessor for setting constants.
A better way to set constants would be:

const unsigned int SB_TOP_OFFSET = 10; //comment comment comment
const unsigned int SB_SCORE_OFFSET = 50; //comment comment comment

Same goes for the "C-style macro functions".
Just put an inline function inside your header. It will be expanded inline in the code where it is called. Effectively producing the same effect as an #define macro.
(note sometimes an inline function cannot be expanded, eg. when it contains while or for loops. But this can depend on your compiler).

In general try to minimize preprocessor abuse to a minumum. The preprocessor should primarly be used to control the way your code is compiled. Not how you code will actually run.

##### Share on other sites
Actually I use quite a few defines for such as :
speed of moving stuff
width, height
position of certain things displayed on the screen
etc. etc.

so that I can modify these things easily... is it really that bad compared to consts??

##### Share on other sites
quote:
Original post by Direct4D
Don''t abuse the preprocessor for setting constants.

##### Share on other sites
quote:
Original post by Koobazaur
so that I can modify these things easily... is it really that bad compared to consts??

''Preprocessor abuse'' is a bit of a harsh term. It''s not a bad thing at all, and can be useful in practice. Using older C compilers it''s something you can''t live without. But using const values gives you the benefit of type safety. If you use a preprocessor constant such as ''#define DEFAULT_PLAYER_SPEED 0.6'', you can assign that value to any type and not get any warning.

##### Share on other sites
quote:

Over 30 000 preprocessor are abuse each day!!!

LOL

Aldecron is right, it is actually a bit harsh term.
And my post did sound a little too harsh, but i meant it more as an advice.
So keeping on the subject, let me redefine the intention of my initial post:
#define FORUM_POST THE_11TH_COMMANDMENT
becomes:

Edit: typos

[edited by - Direct4D on March 20, 2004 5:17:45 AM]