Macro with two names

RayLivingston:
Neither one "executes" at all! #defines are pre-processor macros. They modify the source code BEFORE it is handed to the compiler, by simply doing a literal cut-and-paste of every instance of the macro name with the macro body. There is a bit more to it than that, but not much. Read up on the c pre-processor for more details.
If you write this in your .ino file:

#define HEEBIE for(int x=1; x<10; x)

#define JEEBIE { Serial.println(x*x); }

void setup()
{
HEEBIE JEEBIE
}




The source file passed to the compiler looks like this:


void setup()
{
for (int x=1; x<10; x++) { Serial.println(x*x); }
}




Every instance of the word "HEEBIE" will be replaced with the for statement. Every instance of the word "JEEBIE" is replaced with the print statement and curly-braces. The pre-processor does not evaluate the expression or check it for correct c/c++ syntax. It only does a fairly dumb string replace, checking only for the presence of OTHER previouslly defined macros within the macro body, and expanding them if found.
Note the blank lines at the top of the file, where the macro definitions have been deleted, as they are no longer needed after they are parsed by the pre-processor.

Regards,
Ray L.

This is really useful Ray. I have only been using macros to define simple things. I didn't know we could also define conditional statements.

If you want the two macros to always be identical I think PaulS's code is better because it guarantees this. If you decided to change value with your code you would need to change it in two different places. In the code aarg posted, it's better to define each macro separately because they just happen to have the same value by chance and there's no reason to link the two macros together.

It's the latter just like aarg posted.

An alternative (the #if is optional)...

Code: [Select]

#define NAME1 VALUE1

#if ! defined( NAME2 )
#define NAME2 NAME1
#endif

Another great example!

We don't know the real question because reply #5 was never answered. So we don't know whether that value is coincidentally the same value, or whether it is necessarily the same. Frequently, people explicitly ask for the wrong thing here...

This is just a question that is not yet implemented anywhere on my code..it's more about knowing the language.