another newbie String related question

Hi,
If I do Serial.print("Starting") or if I do Serial.print("Starting" + " step 1"), it means that I make use of String (library), right? I think the problem is the same if I use lcd.print().
So, in order to avoid this, it is better to declare a char array, initialize it with the value "Starting" and then print it? Even if I will not use this variable again in my sketch?
Thank you!

With the 8 bit Arduino boards it is always better to use strings (null terminated character arrays). Use of the String class can cause hard to find memory bugs. See the evils of strings page for explanation and how to use strings in the place of Strings.

Here is a reference to some of the functions used to search, concatenate, copy, etc. strings.

doni1:
Hi,
If I do Serial.print("Starting") or if I do Serial.print("Starting" + " step 1"), it means that I make use of String (library), right?

Nope. You can't do

Serial.print("Starting" + " step 1")

So, in order to avoid this, it is better to declare a char array, initialize it with the value "Starting" and then print it? Even if I will not use this variable again in my sketch?

If I were only going to use (print) the string once, I would just print it using the F macro. The F macro places the string literal in program memory so will not take up any valuable SRAM.

Serial.println(F("Starting"));

Using the Serial monitor, if you say:

Serial.print(F("Starting"));

The literal string is stored in program memory instead of data memory (RAM), whether that works with the LCD library, I'm not sure, but you can try it.

LCD.print(F("Starting"));

Also:

JCA34F:
Using the Serial monitor, if you say:

Serial.print(F("Starting"));

The literal string is stored in program memory instead of data memory (RAM), whether that works with the LCD library, I'm not sure, but you can try it.

LCD.print(F("Starting"));

If your LCD library inherits from Print, you can use F()

TheMemberFormerlyKnownAsAWOL:
If your LCD library inherits from Print, you can use F()

JCA34F:
Using the Serial monitor, if you say:

Serial.print(F("Starting"));

The literal string is stored in program memory instead of data memory (RAM), whether that works with the LCD library, I'm not sure, but you can try it.

LCD.print(F("Starting"));

groundFungus:
If I were only going to use (print) the string once, I would just print it using the F macro. The F macro places the string literal in program memory so will not take up any valuable SRAM.

Serial.println(F("Starting"));

Yes, I knew about this, but is not critical to me, as I read, the RAM would be saved (which is not a problem), but the speed would be slower.
All I want to avoid is memory fragmentation.

ToddL1962:
Nope. You can't do

Serial.print("Starting" + " step 1")

You are right, my mistake. The question remains only for the variant Serial.print("Starting")

Thank you all for your explanations!

but the speed would be slower.

You're writing to an LCD, and concerned about speed ?

Or, thinking deeper, the fragmentation problem actually may not exist if I don't do string operations..? :o
Anyway, the function Serial.print("Starting") takes the argument as char array?

groundFungus:
If I were only going to use (print) the string once ...

And if I print THE SAME text repeatedly?

edit:

TheMemberFormerlyKnownAsAWOL:
You're writing to an LCD, and concerned about speed ?

Yes, and a lot of things are happening in the loop (calculations, sensor readings)

Use of the String class objects and functions can lead to memory fragmentation. Use of strings (null terminated character arrays) and string functions will not fragment memory.