EmbDev.net

Forum: ARM programming with GCC/GNU tools #define problem


Author: jrmymllr jrmymllr (jrmymllr)
Posted on:

Rate this post
0 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?

Author: Rolf Magnus (Guest)
Posted on:

Rate this post
0 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);

Author: Clifford Slocombe (clifford)
Posted on:

Rate this post
0 useful
not useful
What Rolf said.  For reference: 
http://www.cppreference.com/wiki/preprocessor/sharp

For the purposes of Googling the # used in this manner is called the 
"preprocesor stringization operator"

Author: kbuchegg (Guest)
Posted on:

Rate this post
0 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
#define MENU(A, B, C, D) lcd_DspStr(#A "  \x29  " #B "  \x29  " #C " \x29  " #D)

with a macro call of
  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
  lcd_DspStr("\x30" "  \x29  " "\x30" "  \x29  " "\x30" " \x29  " "\x30")

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

... the intendet result.

Author: jrmymllr jrmymllr (jrmymllr)
Posted on:

Rate this post
0 useful
not useful
Thanks everyone.  This fixed my problem.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.