Macro with two names

Hi.

Is there a way to define a macro with two names like this

#define NAME1, NAME2  value

Thanks.

Is there a way to define a macro with two names like this

No, but you can use 2 #define statements, to assign the second name the value of the first name.

Like this ???

#define NAME1 VALUE
#define VALUE NAME2

#define NAME1 value
#define NAME2 value

jbellavance:
#define NAME1 value
#define NAME2 value

I was finding an alternative to that...

Can you explain in english (instead of C++) what you would like to do ?

Like this

No.

#define name1 value
#define name2 name1

I just have two values that are identical. The one you showed me(also the one I have been using) is fine. I was just wondering if there's a way two define macro of the same value with two names.

PaulS:
No.

#define name1 value
#define name2 name1

Thanks PaulS.

One question, is there really a difference between using yours and

#define NAME1 value
#define NAME2 value

Which executes faster?

#define MY_PIN 6

Then if your sketch says:

digitalRead(MY_PIN);

the pre-processor will do:

digitalRead(6);

before the compiler does it’s job.

Now if you have

#define MY_PIN 6
#define LIMIT 6

Then if your sketch says:

for (int i = 0 ; i < LIMIT ; i++) digitalRead(MY_PIN);

the pre-porcessor will do: :

for (int i = 0 ; i < 6 ; i++) digitalRead(6);

before the compiler does it’s job.

Thank you @jbellavance. It makes sense.

cyberjupiter:
Like this ???

#define NAME1 VALUE

#define VALUE NAME2

Which defines NAME1 to an empty string, THEN defines VALUE to "NAME2". Order of the #defines is important. Unlike c variable assignments, changing the definition of VALUE does NOT update any previous #defines that referenced it.

One could also reasonably ask why on earth anyone would WANT to create two #defines for the same value, other than as an entry to an obfuscated code contest...

Regards,
Ray L.

RayLivingston:
Which defines NAME1 to an empty string, THEN defines VALUE to "NAME2". Order of the #defines is important. Unlike c variable assignments, changing the definition of VALUE does NOT update any previous #defines that referenced it.

Well, I was just translating PaulS's words to code...maybe my comprehension is bad or PaulS's words are wrong, or both.

RayLivingston:
One could also reasonably ask why on earth anyone would WANT to create two #defines for the same value, other than as an entry to an obfuscated code contest...

It was just a question that I didn't know what to Google about.

One question, is there really a difference between using yours and

Not a bit.

Which executes faster?

It’s text substitution. The same amount of text is going to be substituted, so the execution time will be the same.

cyberjupiter:
Thanks PaulS.

One question, is there really a difference between using yours and

#define NAME1 value

#define NAME2 value




Which executes faster?

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.

RayLivingston:
One could also reasonably ask why on earth anyone would WANT to create two #defines for the same value, other than as an entry to an obfuscated code contest...

Regards,
Ray L.

Perhaps you could ask the author of Arduino.h ... :slight_smile:

#define HIGH 0x1
#define LOW  0x0

#define INPUT 0x0
#define OUTPUT 0x1

cyberjupiter:
One question, is there really a difference between using yours and

#define NAME1 value

#define NAME2 value

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.

aarg:
Perhaps you could ask the author of Arduino.h ... :slight_smile:

#define HIGH 0x1

#define LOW  0x0

#define INPUT 0x0
#define OUTPUT 0x1

Which has nothing whatsoever to do with the OPs question. Obviously, totally unrelated definitions will sometimes evaluate to the same numeric value, but the OP was explicitly asking how to give two names to the exact same value.
Regards,
Ray L.

RayLivingston:
Which has nothing whatsoever to do with the OPs question. Obviously, totally unrelated definitions will sometimes evaluate to the same numeric value, but the OP was explicitly asking how to give two names to the exact same value.
Regards,
Ray L.

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...

PaulS:
No, but you can use 2 #define statements, to assign the second name the value of the first name.

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

#define NAME1 VALUE1

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