Using a character name as a string literal in a #define function macro
I want the debug function macro to work like this:
int myVar = 5;
PRINTVAR(myVar); // macro
// which expands to something like...
print("myVar: ");
println(myVar);
Basically, I want to use the identifier as a string literal as well as a variable.
I'm just a little tired of repeating myself when I want to dump a lot of variables to stdout.
My silly attempt, which of course doesn't work:
#define PRINT_VAR(x) Serial.print("x: "); Serial.println(x);
Is it possible?
+3
source to share
3 answers
Look at the contraction operator , #, when you use a macro prefix with this, it puts it as a string, not expands it.
+2
source to share