Learn

Fixing "stray '\302' in program" in Arduino

Beginner

Or see every lesson.

  • 455parts on the bench
  • 43boards running now
  • 1.00xreal time, on every board
Arduino Uno: a DHT11, for pasting intolive0.000 s 0.00x
Click to open it in the editor
It prints the temperature and humidity every two seconds. Paste a line from a web page into it to see what the build tab says.

stray '\302' in program means the sketch has a character that is not plain ASCII, outside quotes and comments, where the compiler only accepts code. 302 is the byte in octal, 0xC2, and it is the first byte of every character from U+0080 to U+00BF. In a sketch it is nearly always a non-breaking space, the invisible space web pages and documents use to keep code indented, copied along with the code. Each one gives two errors, one per byte: stray '\302' and then stray '\240'.

The circuit above is the DHT11 sketch, which prints "temperature 22 C, humidity 45 %". Copy a line of it through a web page and its indentation can come back as non-breaking spaces. Below is how each character behaves in the Arduino IDE and in Mokxi, which compiles with clang and handles the commonest one differently, and how to clean the line.

The non-breaking space: \302\240

A non-breaking space is U+00A0, two bytes in UTF-8: 302 240 in octal, C2 A0 in hex. It looks exactly like a space, which is why the error is so confusing: the line looks right. The GCC that builds for AVR boards in the Arduino IDE, version 7, reports each byte separately as stray. GCC 10 and later name the character instead: g++ 13 said extended character, then the invisible space itself, then is not valid in an identifier, and after it '\U000000a0' was not declared in this scope, because it read the space as part of a name.

Clang treats it as the space it looks like. We put two non-breaking spaces in front of dht.begin(); in the sketch above: Mokxi built it with two warnings, treating Unicode character as whitespace, one for each space, and it ran and printed "temperature 22 C, humidity 45 %" as before. The build tab says "A character here was pasted from a web page or a document: a curly quote (“ ” ‘ ’), a long dash or an invisible space. Retype the quotes and dashes on this line with the keyboard." and offers a button, Retype the pasted quotes and dashes, which turned both into ordinary spaces; the sketch then built with no warnings at all.

So a sketch that builds here can still stop the Arduino IDE. If the warning is in the build tab, press the button before you copy the code to a real board.

A degree sign, µ or ± outside the quotes

The other characters that start with \302 are the symbols of the Latin-1 block: ° is 302 260, ± 302 261, ² 302 262 and µ 302 265. Inside double quotes they are text and every compiler accepts them. Serial.println(" °C"); built and printed " °C" here. Outside the quotes they are not code. Serial.println(°C); stopped Mokxi’s build with unexpected character <U+00B0> and then use of undeclared identifier 'C'; g++ 13 said extended character ° is not valid in an identifier.

Put the symbol inside the quotes it was meant to be in, or write it out: " deg C". In a comment it does no harm at all; a sketch with non-breaking spaces only inside a // comment built in Mokxi and in g++ 13 without a word.

The degree sign inside the quotes
void setup() {
  Serial.begin(9600);
  Serial.println(" °C");
}

void loop() {
}

\303: accented letters and the times sign

Characters from U+00C0 to U+00FF start with \303: é is 303 251, ü 303 274, ñ 303 261 and × is 303 227. A variable named in French or German, such as int température = 22;, gives stray '\303' in GCC before version 10. Newer compilers allow letters like these in names: that line built in g++ 13 and in Mokxi, and printed 22. For a sketch that has to build in every IDE, stick to plain letters, digits and underscores.

The times sign is never allowed. int x = 5 × 2;, copied from a page that formats its math, gave clang’s unexpected character <U+00D7> and g++ 13’s extended character × is not valid in an identifier. Multiplication is *.

\342: curly quotes and long dashes

If the byte is \342, the character is from U+0800 to U+FFFF, and in a sketch that is almost always a curly quote (“ ” ‘ ’), a long dash in place of a minus sign, or a zero-width space. They come from word processors and chat apps rather than from web page indentation, and they get their own section on the "expected ';' before" page, which has the clang messages for each one. Mokxi’s Retype button fixes those too.

How to find and clean the line

The error gives the line. If nothing on it looks wrong, the character is invisible, so retype the start of the line: delete the indentation and press Tab or the space bar. If the line has many errors, retype the whole line rather than hunting for each character, or paste the code into a plain text editor first and copy it again from there.

In Mokxi the warning or error points at the column, and the Retype button turns each pasted space, quote and dash into its ASCII twin and leaves text inside quotes and comments as it is.

Questions

What does "stray '\302' in program" mean?

Your sketch has a character from outside plain ASCII, outside quotes, and its first byte is 302 in octal. It is nearly always a non-breaking space copied from a web page; a degree sign or µ outside the quotes is the other common one.

Why do I get "stray '\302'" and "stray '\240'" together?

They are the two bytes of one non-breaking space, U+00A0. Older GCC reports each byte as its own error, so every such space gives two lines.

Why does the sketch build in Mokxi but not in the Arduino IDE?

Mokxi compiles with clang, which treats a non-breaking space as whitespace and warns instead of stopping. The Arduino IDE’s GCC stops. Press Retype the pasted quotes and dashes in the build tab before you copy the sketch.

What does "stray '\303' in program" mean?

The same thing for characters from U+00C0 to U+00FF, such as é, ü, ñ or the times sign ×. Use plain letters in names and * for multiplication.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.