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?
> 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);
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"
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
Log in with Google account
No account? Register here.