Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.
Compile error · C++

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

All boardsUpdated 3 Nov 2026

Community draft — report a mistake

What you see

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

    Or print in pieces

    Serial.print("Temp: "); Serial.print(t); Serial.println(" C"); — no joining needed, and no extra RAM.

  3. 3

    Or use snprintf for fixed buffers

    char line[24]; snprintf(line, sizeof line, "Temp: %d C", t);. On AVR, %f isn’t supported — use dtostrf() for floats.

  4. 4

    Text + number is a different bug

    "Temp: " + 25 compiles but moves the pointer 25 characters on — printing garbage. Wrap with String(…) 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.

↑↓ to move · Enter to open