Preprocessor #if statement doesn't work

I have tried to compile and execute this sketch:

#include <WiFi.h>

#define TEST pillo

void setup()
{
Serial.begin(115200,SERIAL_8N1);

#if TEST == pollo
Serial.println("TEST is pollo");
#else
Serial.println("TEST is not pollo");
#endif
}

void loop()
{
}

You can assign to TEST any value but the outut is always "TEST is pollo". Why? Can anyone help me?
I am using ESP32 library 1.0.6 and Arduino 1.8.16 (windows store 1.8.51.0).

Good idea to check the preprocessor reference:

https://gcc.gnu.org/onlinedocs/gcc-3.0.2/cpp_4.html

Oops, I just noticed you are using the ESP32. That is not an Arduino.

If you try this sketch:

#include <WiFi.h>

#define SDBG Serial

// SERIAL 1 PINS
#define RX1_PIN 4
#define TX1_PIN 2

void setup()
{
#if SDBG == Serial1
SDBG.begin(115200, SERIAL_8N1, RX1_PIN, TX1_PIN);
SDBG.println("Serial1");
#else
SDBG.begin(115200,SERIAL_8N1);
SDBG.println("No Serial1");
#endif
}

void loop()
{
}

The output is always "Serial1". How can i test this with preprocessor?

Sorry, but this is the first time that I write in the forum

The Arduino forum is generally for MCU boards and software manufactured and distributed by Arduino.

Other manufacturers and volunteers have modified the Arduino IDE to work with their compilers and linkers, and nothing is guaranteed to work as it does (well, if it does) with Arduino boards. You might try the ESP32 forum.

Thank you. Bye

Meh. Even though they have the same name, the Arduino project is much bigger than the Arduino company. All things related to the Arduino project are welcome here. And in the case of the ESP32, many of the official boards actually have an ESP32 microcontroller in their WiFi modem.

But this question is not even specific to any one microcontroller. This is about C++.

From § 16.1 ¶4 of the C++11 standard:

After all replacements due to macro expansion and the defined unary operator
have been performed, all remaining identifiers and keywords, except for true and false, are replaced with the pp-number 0

So what that means is your conditional is equivalent to this:

#if 0 == 0

Thank you very much for answer. Bye

You're welcome. Bye!

Hi @Connetsrl,

Correct way to use directives
RV mineirin

#include <WiFi.h>

#define pillo      //  <<--------    Correct form

void setup()
{
  Serial.begin(115200, SERIAL_8N1);

#ifdef pollo    //  <<--------    Correct form
  Serial.println("TEST is pollo");
#else
  Serial.println("TEST is not pollo");
#endif
}

void loop()
{
}

Wow- make a newcomer feel unwelcome, why don't you?

This is the first time Connetsrl has posted — let’s welcome them to our community!

There is absolutely NOTHING in Connestri's question that is restricted to Arduino boards only that rates this kind of dismissal. Preprocessor statements in the Arduino IDE work the same whether it's an Arduino board or an ESP board.

You don’t have to load the sketch onto the Arduino to test the behavior of the preprocessor #if directive. The #warning directive will display a message at compile time (gcc compiler )

#if TEST == pollo
#warning "TEST is pollo" 
#else
#warning "TEST is not pollo"
#endif

If you WANT to have symbol definitions for your #defines, you can "cheat" by pre-defining all of your symbolic names with unique numeric values, off in some .h file. Such as:

//testnames.h
#define pillo 0x12340001
#define pollo 0x12340002
//main.c
#include "testnames.h"
#define TEST pillo
  : 
#if TEST == pillo
 // stuff.
#elif TEST == pollo
 // other stuff
#else
  #error undefined test
#endif

the current Optiboot source code uses this extensively to allow compile time naming of chip pins like avr-gcc optiboot.c -DLED=B5 -DUARTRX=D0 and similar. It's "ugly" in the implementation (10 ports with ~8 bits each makes for #elif chains 500+ lines long!), but it's not difficult to implement (a good editor with macro capabilities helps a lot), and it makes things much easier for the "user."