#define problem

OP #1645965
Rate this post
useful
not useful
Say I define this:

#define MENU(A, B, C, D) lcd_DspStr("A  \x29  B  \x29  C  \x29  D")

Then do this in my program:

MENU(\x30, \x30, \x30, \x30);

lcd_DspStr() is a function I wrote to write to my LCD and it works fine 
when placing text straight into the quotes.  In this case, I'm using a 
special font that contains symbols, rather than ASCII characters, so 
that's why I'm using "\x30" or similar.  It works perfect if I forget 
about using the #define.

Anyway, I would expect this setup to work, if it replaced A with \x30 
and so on.  But it doesn't.  It compiles fine, but I get weird results 
so the define is obviously not doing what I thought it would.  Where am 
I going wrong?
Guest #1645978
Rate this post
useful
not useful
> Anyway, I would expect this setup to work, if it replaced A with \x30
> and so on.  But it doesn't.  It compiles fine, but I get weird results
> so the define is obviously not doing what I thought it would.  Where am
> I going wrong?

#define doesn't replace anything within a string.

Try:

#define MENU(A, B, C, D) lcd_DspStr(#A "  \x29  " #B "  \x29  " #C " 
\x29  " #D)

Then do this in my program:

MENU(\x30, \x30, \x30, \x30);
Guest #1646560
Rate this post
useful
not useful
And the other thing Rolf used, is the fact that the compiler has to 
catanate strings on its own if they aray not seperated by some other 
character then whitespace

   "Hello" " World"

is identical to "Hello World"

So he first used the stringize operator in
1
#define MENU(A, B, C, D) lcd_DspStr(#A "  \x29  " #B "  \x29  " #C " \x29  " #D)

with a macro call of
1
  MENU(\x30, \x30, \x30, \x30);

to first have A, B, C, D replaced with the given macro arguments, where 
each argument is enclosed in quotes (by the stringize operator), which 
gives
1
  lcd_DspStr("\x30" "  \x29  " "\x30" "  \x29  " "\x30" " \x29  " "\x30")

which in turn is string-catanated by the compiler to become
1
  lcd_DspStr("\x30  \x29  \x30  \x29  \x30 \x29  \x30")

... the intendet result.

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now