# “expected primary-expression before '.' token” in debug code

I'm making some code for school, and is adding debug code to it. When compiling, I get the error as written in the title. This is the code in question:

#define DEBUG 1

#ifdef DEBUG
#define Serial.println(x) DEBUG_PRINT(x)
#else
#define DEBUG_PRINT(x)
#endif


And the code in my void setup, which has Serial.begin:

#ifdef DEBUG
Serial.begin(9600);
DEBUG_PRINT("Asteroid");
#endif


What am I getting wrong? I get the feeling it's my define text, but I'm unsure.

Thank you for your time Gaarden

• Macros cannot contain a .. Thus you cannot define a macro Serial.println(x). You probably wanted to define it the other way around anyway. #define DEBUG_PRINT(x) Serial.println(x) – Kwasmich Sep 11 '19 at 7:49
• I switched them around, per Kwasmich's comment. Now the error message is "expected ';' before 'Serial'" – TheGaardenator Sep 11 '19 at 7:52
• Somewhere in your code (not shown above) you have a DEBUG_PRINT statement where in the line before that you have skipped the ; – Kwasmich Sep 11 '19 at 7:56
• Didn't know it would give me an error message on that line if a line further down had a problem. Thank you! – TheGaardenator Sep 11 '19 at 7:58
• @Kwasmich You posted an answer as a comment. Please make a proper answer. Then you will get rep for it, and Stack Exchange will consider the question answered. Win win! – Nick Gammon Sep 11 '19 at 8:12

In short: you have your macro backwards.

Macros are not formed as "Take this set of commands and call it X" but "Make this macro X and have it equate to this set of commands".

Where you have:

#define Serial.println(x) DEBUG_PRINT(x)


#define DEBUG_PRINT(x) Serial.println(x)


When you call it, because the macro doesn't contain a semi-colon in it, you need to specify the semi-colon as if you were calling a function:

DEBUG_PRINT("Aardvark");


You can omit the semi-colon only if you have a semi-colon in your macro:

#define DEBUG_PRINT(x) Serial.println(x);


and then:

DEBUG_PRINT("Aardvark")


By doing it that way your "empty" macro now does become truly empty when used. Without the semi-colon in the macro you would have to use:

DEBUG_PRINT("Aardvark");


Which for the empty variant would end up as:

;


Not always desirable, and can cause some problems.

So by adding the semi-colon into the macro you can omit it from the usage of the macro, and your empty variant now becomes:




You defined the pre-processor macro the wrong way around.
Think it is like an assign statement. You come up with a name and assign something to it.

#define SHORT_NAME(ARG) some_real_function_that_has_an_awkward_long_name(ARG)

See the manual of your favourite compiler GCC for reference.
It is always a good idea to do that when using features you haven't used before. Sometimes it can be enlighting.