Fix “invalid operands of types 'const char [7]' and 'const char [3]' to binary 'operator+'”
Two quoted texts can’t be joined with + in C++. Make the first one a String, print the parts separately, or build the line with snprintf().
Community draft — report a mistake
What you see
Thermometer.ino: In function 'void setup()':
Thermometer.ino:3:26: error: invalid operands of types 'const char [7]' and 'const char [3]' to binary 'operator+'
String line = "Temp: " + " C";
~~~~~~~~~^~~~~~
exit status 1
Compilation error: invalid operands of types 'const char [7]' and 'const char [3]' to binary 'operator+'Thermometer.ino: In function 'void setup()':
Thermometer.ino:3:26: error: invalid operands of types 'const char [7]' and 'const char [3]' to binary 'operator+'
String line = "Temp: " + " C";
~~~~~~~~~^~~~~~
exit status 1
Compilation error: invalid operands of types 'const char [7]' and 'const char [3]' to binary 'operator+'
In C++ a quoted text is a fixed array of characters, not a string object. The + operator isn’t defined between two arrays, so "Temp: " + " C" fails. Make the first operand an Arduino String and the rest follows:
float t = 31.5;
String line = String("Temp: ") + t + " C"; // OK
Serial.println(line);
Serial.print(F("Temp: ")); // or, cheaper on RAM
Serial.print(t);
Serial.println(F(" C"));Quick fix — try in order
- 1
Start the expression with a String
String line = String("Temp: ") + t + " C";. Once the left side is a String, every+after it appends. - 2
Or print in pieces
Serial.print("Temp: "); Serial.print(t); Serial.println(" C");— no joining needed, and no extra RAM. - 3
Or use snprintf for fixed buffers
char line[24]; snprintf(line, sizeof line, "Temp: %d C", t);. On AVR, %f isn’t supported — usedtostrf()for floats. - 4
Text + number is a different bug
"Temp: " + 25compiles but moves the pointer 25 characters on — printing garbage. Wrap withString(…)there too.
Why it happens
What is const char [7]?
A string literal: "Temp: " is 6 characters plus the terminating zero. Two literals are two arrays, and C++ has no + for arrays.
Why does String(...) fix it?
The Arduino String class defines operator+. The compiler needs one side of the first + to be a String to use it.
Display libraries
lcd.print("T=" + t) has the same problem. Print the parts with two calls.
Board-specific notes
Arduino Uno: Joining Strings in loop() fragments the 2 KB heap over time. Printing in pieces is the safe pattern on AVR.
ESP32: Serial.printf("Temp: %.1f C\n", t); is available and the easiest option.
Did this fix it?
Thanks — that helps us keep this page right.