Archived

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

Koobazaur

The destructive power of a \ ???

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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"

so in your code:

#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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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:
#define FORUM_POST ADVICE



Edit: typos

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

Share this post


Link to post
Share on other sites
Wow, I never new about that one, can in be used in normal code too?

And if it does the does anyone know if it works in java? I know VB has the underscore which does the same thing, but VB needs it dues to it''s single line crap.

Share this post


Link to post
Share on other sites
quote:
Original post by Direct4D
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



Now that depends, Koobazaur didn't specify if he was using C or C++. "const" means something different in C than it does in C++. In C it means "read only", but in C++ it means "constant". This can make a difference. For example:

#define ARRAY_SIZE_1 10
const size_t ARRAY_SIZE_2 = 10;

int main(void)
{
int array1[ARRAY_SIZE_1] = {0};
int array2[ARRAY_SIZE_2] = {0};
}

array1 is an array in C89, C99, and C++. array2 is illegal in C89, a VLA in C99, and an array in C++.

quote:

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).



A #define will inline no matter what. Also, #define is the only way to inline in C89, and inline has different semantics in C99 and C++. (inline in C++ implies static, but doesn't in C99)

quote:

In general try to minimize preprocessor abuse to a minumum.



Good advice for any sort of abuse.


quote:
Aldacron
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.



I have two somewhat naive, but honest, questions. Where would you get a useful warning when using const but not #define in C++? How about in C?

EDIT: Slight clarification

[edited by - Way Walker on March 20, 2004 2:23:40 PM]

Share this post


Link to post
Share on other sites