Why the code can be comilped?

void setup() {
  initSensors();
}

void loop() {}

#undef BBB
#if defined BBB
  void aaa2() {}
#endif

void initSensors() {
  if( 0 ) aaa2();
}

who can help me? Thx

Hello and welcome,

What is your question exactly?

Hi, what re you trying to do?
What is supposed to be wrong with your sketch?

Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom...... :slight_smile:

In the code above. BBB is undef. Function aaa2() should be not a part of code. So the code should be not compiled correctly.

But I found the code can be compiled. Why? That's my question. Thx.

TomGeorge:
Hi, what re you trying to do?
What is supposed to be wrong with your sketch?

Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom...... :slight_smile:

Thank you. I have modified the post with tag "code".

It's because you do "if ( 0 )", which will never evaluate to True (because 0 is a constant), so the compiler doesn't bother compiling it at all :wink:

If you remove this condition or change it to if ( 1 ), then your code will not compile because aaa2 is not defined.

However, if you do this:

bool b = false;
if ( b )
  aaa2();

The compiler will try and fail, because b is not a constant, it's a variable. Add the const keyword and it will compile again

const bool b = false;

guix:
It's because you do "if ( 0 )", which will never evaluate to True (because 0 is a constant), so the compiler doesn't bother compiling it at all :wink:

If you remove this condition or change it to if ( 1 ), then your code will not compile because aaa2 is not defined.

However, if you do this:

bool b = false;

if ( b )
  aaa2();



The compiler will try and fail, because b is not a constant, it's a variable. Add the const keyword and it will compile again


const bool b = false;

I see. Thank you.

JoshuaShaw:

Little modified to check stuff 

void setup() {
 Serial.begin  (115200);
 delay(2000);
 initSensors();
}

void loop() {}

#undef BBB           // undefines macro (name)
#if defined BBB      //checks if macro is defined
void aaa2()          // so the compiler should skip these lines
{
 Serial.print("TEST");
}
#endif

void initSensors() {
 if ( 0 )
 {
   Serial.print("True");
   aaa2();                     // and complain this is undefined
 }
 else
 {
   Serial.print("False");
   aaa2();                    // and complain this is undefined
 }
 aaa2();                      // and complain this is undefined
}

But it compiles and RUNS the else Serial.print ( as it should)  but no aaa2() function is executed !
My best guess is that you really did not defined macro - just name.
But it should fail to compile.