Why does this basic statement fail?

Hey all, I’ve been writing several things in Particle (this is for the Photon) and have been banging my head as to why my code won’t compile. Finally simplified it to the most basic program possible where I can cause it to fail (this is only one of many odd failures). Happens in my local environment and web environment.

The full code:

void setup() { }
void loop() {
if (0) { }
else if (1) { }
}

This refuses to compile. Any thoughts on why? Error is below:

attempting to compile firmware
Compile failed. Exiting.
bal2.cpp:5:1: error: expected unqualified-id before 'else’
else if (1);
^
make[1]: *** […/build/target/user/platform-6bal2.o] Error 1
make: *** [user] Error 2

That’s probably again one of the cases where the Wiring preprocessor trips.

This builds tho’

#pragma SPARK_NO_PREPROCESSOR
#include "Particle.h"

void setup() { }
void loop() {
if (0) { }
else if (1) { }
}
1 Like

That solved it, thanks, but it’s quite disconcerting. Am I going to be missing some features by not using the preprocessor? Is there a way to see the code after the preprocessor runs if I don’t disable it?

Thanks!

You won’t miss out on any functionality with the prepeocessor disabled.
It’s just a device of convenience for less experienced programmers who aren’t used to the C/C++ requirement to provide forward declarations/functio prototypes and it just adds that `#include “Particle.h” for you.
And somewhat hidden is the “activation” of C++ calling convention for pure C code.

So when your code build you’ll have access to every feature you’d have with the preprocessor too.

One other advantage of disabling it is that error messages will show the correct code excerpt again while with it on, they are always off by a few lines (the ones added by the preprocessor itself).

Unfortunately there is no way to see the intermediate code.

BTW, the preprocessor only acts on .ino files anyway - .h/.cpp files are unaffected by it even when it’s on.

Got it, thanks!