define static strings that not waste RAM: use '#define' or declare them by name?

Hi!

I want to use some strings that are stored in the program memory only, so they don't waste any RAM. I've seen two ways of declaring them, namely

// option 1
#define MYTEXT_1 F("somestring")

// option 2
const char MYTEXT_2[] PROGMEM = "anotherstring";

Which way should I use, or are they completely equivalent?

There are only two differences that I can think of:

1. Redudancy

If referenced multiple times, option 2 should not lead to any changes. But what about option 1? The compiler should be smart enough to do some redudancy check and put all equivalent F("somestring") into the same memory area, right?

2. Unused references

What if the declared strings are not used at all? Because of its nature as a preprocessor macro option 1 definitley does not consume any space then, but what about the other one? Is the compiler smart enought to remove it from the resulting output (if optimization is enabled)?

Am I right with my assumptions? Are there any other differences?

Thanks in advance!

I'm not sure if the changes were implemented, but at one stage the F() macro did NOT combine literal strings. There was some people working on a solution. Like I said, I do not know if this was merged in.

Your method #2 will combine the similar strings. And the compiler is smart enough to remove unused variables. But you have a fairly simple scenario to test. Why not write a short sketch to test your questions?

pYro_65:
But you have a fairly simple scenario to test. Why not write a short sketch to test your questions?

Thanks for your answer. Well, to be honest, haven't thought of that :confused:. But nevertheless I wanted to ask because I may overlook something a guru here may see and answer immediately..