Using a macro with and without double-hyphens

Hi everybody,

I want to use the same Strings, as simple examples just named
Item1
Item2
Item3

These strings shall be in all cases the exact same character-sequence.
I tried to realise this with macros

I want to use these character-sequences at different places
which means different function-calls and initialisers

some initialisers need the sequence without double-hyphen

Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD,"String1","Item1,Item2,Item3");

some need the character-sequences WITH double-hyphen

const char MyNames[][8]  = {
  "Item1",            
  "Item2",            
  "Item3"
};

all character-sequences comma-separated as a SINGLE string

"Item1,Item2,Item3"

each character-sequences with their own double-hyphens " "

const char MyNames[][8]  = {
  "Item1",            
  "Item2",            
  "Item3"
};
#define macro1 Item1
#define macro2 Item2
#define macro3 Item3

How can I use the marcos for both cases

Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD,"String1","Item1,Item2,Item3");

This will not work

Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD,"String1","macro1,macro2,macro3");

defining the macros as

#define macro1 "Item1"
#define macro2 "Item2"
#define macro3 "Item3"

does not work either

nor does it work to define

#define AllTogether "macro1,macro2,macro3"
Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD,"String1",AllTogether);

So are there any advanced tricks with macros to realise this?

best regards Stefan

When you say "double-hyphens", do you mean double quotes: these: " ?

ie, ASCII Code 34 (0x22)

image

Yes I mean this character with ASCII-code 34

If that's the case, then you need to double them up for C++ to recognise them as the double-quote character - rather than as the delimiter for a string; eg,

const char MyNames[][8]  = {
  """Item1""",            
  """Item2""",            
  """Item3"""
};

EDIT:

Note that, in this case, you end up with sequences of three double quotes - because one is the string delimiter, and the other two make the literal character.

If this was within a string, it would look like, eg:

const char MyNames[][8]  = {
  "Item1's name is ""Tom""",            
  "Item2ls name is ""Dick""",            
  "Item3's name is ""Harry"""
};

EDIT 2: This is all rubbish - I am talking in the wrong language!
:man_facepalming: :man_facepalming: :man_facepalming:

Or use a backslash to "escape" the quote:

const char MyNames[][8]  = {
  "\"Item1\"",            
  "\"Item2\"",            
  "\"Item3\""
};

EDIT: It's not an "or" - this is the only way!

Hi @awneil
thank you for answering.

You have posted an example with string-constants

I want to use a macro

#define myMacro1   "myName"

Serial.println(myMacro1);
would result in printing

myName

So how would this look like for

#define myMacro1   myName
#define myMacro2   myCar
#define myMacro3   myHouse

without the double-qouting-marks

Serial.print(myMacro1);
Serial.print(myMacro2);
Serial.print(myMacro3);

would result in errors
myName is not defined in this scope
myCar is not defined in this scope
myHouse is not defined in this scope
because the speech-marks are missing
though coding

Serial.print("myMacro1");
Serial.print("myMacro2");
Serial.print("myMacro3");

would result in printing

myMacro1
myMacro2
myMacro3

but it should be

myName
myCar
myHouse

Can you please post an example where the macro is used?

not this way
const char MyNames[][8] = {
""Item1"",
""Item2"",
""Item3"",
};

it shall use the macros
const char MyNames[][8] = {
myMacro1 ,
myMacro2 ,
myMacro3 ,
};

Not exactly sure what your are asking, I'm not getting the same idea as others.
Have you tried:

#define macro1 "Item1"
#define macro2 "Item2"
#define macro3 "Item3"

Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD, macro1 "," macro2 "," macro3);

const char MyNames[][8]  = {
  macro1,            
  macro2,            
  macro3
};

The compiler will combine adjacent quoted text into a single text.

As far as I understand macros the macro replaces the macroname with what follows the macroname

#define myButtonPin 5
pinMode(myButtonPin, OUTPUT);

results in

pinMode(5, OUTPUT);

so defining macros as

#define macro1 "Item1"
#define macro2 "Item2"
#define macro3 "Item3"

and used like this

Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD, macro1 "," macro2 "," macro3);

would'nt this result in

"Item1"
Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD, "Item1" "," "Item1" "," "Item1");

which would be way too much double-quoting-marks???

What happened when you tried that?

a7

No, that is the intended result.

Effectively the text will be "Item1,Item2,Item3"

#define macro1 "Item1"
#define macro2 "Item2"
#define macro3 "Item3"

Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD, macro1 "," macro2 "," macro3);

Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD, "Item1" "," "Item2" "," "Item3");

If the compiler meets end-double-qoute begin-double-quote

Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD, "Item1 " "," "Item2" "," "Item3");

which essentially means the finally compiled code is
Card MessStellenDropDownCard(&myDashboard, DROPDOWN_CARD, "Item1,Item2,Item3");

it works ! Crazy ! but a very clever way to enable combining macros

I remember seeing that done for the first time. It must have come into fashion, or possible that is to say, when I am not keeping up with every additional feature.

Or was always a part of I am not a C++ programmer.

It still looks odd.

This seems like a good place to ask about triple quotes:

On another thread, it was suggested to escape the quote mark an inquirer wanted to include in a string constant.

Yes, I will try it later, just curious now, when Oh wait, I can try it.

It doesn't do what I thought, or what the suggestion implied. Goolging only turns up Python triple quotes, where the shallowest of dives seems to say there it has an unrelated different roll.

Are triple quotes used in C/C++?

Added: I found

"""""This"""""

to be the same as

"This"

When used like

const char *foo = "This";

Time for some more googling. :expressionless:

Added: it was during the rinse part of rinse and repeat, while the warm water was massaging my brain, that I figured out I had merely written

const char *foo =  "" "This" "";

without spaces. An even number of quote marks on each side makes for an error.

a7

Use the stringification operator #. You need two levels of macros to get the definition of the macro intead of the macro name.

#define STR(x) #x
#define STR2(x) STR(x)

#define macro1 Item1

STR(macro1)  → "macro1"
STR2(macro1) → "Item1"
1 Like

This is almost like the programming language brainfuck.
Just out of curiosity: does there exist a competetion

most complex C++-macro?
I guess that this macrolanguage could even do for-loops and things like that.

You can, of course, also do silly things like this

#define sketch void setup(){Serial.begin(115200);} void loop(){Serial.println("Hello");delay(1000);}
sketch
1 Like

There are no C++ macros.

Have a nice day and enjoy coding in C++.

The usual critique without presenting a solution.
macros you can use with the arduino-IDE.
Arduino-IDE configured to use the usually installed tool-chain

There is no solution due to the fact that C++ provides no macros.

Maybe it's time to read, study and understand a C++ textbook.

Have a nice day and enjoy coding in C++.

Of course there are macros in C++. They are handled by the pre-processor, similar to the way they are handled in C.

1 Like

For shure.

The compiler is reading all #directives to make an interpretation. These #directives may contain languages elements of C++ but these #directives are languages elements of the compiler.
These precompiler directives aren´t languages elements of C++.

Have a nice day and enjoy coding in C++.