Compilation error: stray ‘\302’ in program etc

You have invalid characters in your source. If you don't have any valid non-ASCII characters in your source, maybe in a double quoted string literal, you can simply convert your file back to ASCII with:

tr -cd '\11\12\15\40-\176' < old.c > new.c

The method with iconv will stop at wrong characters which makes no sense. The above command line is working with the example file.


You have an invalid character on that line. This is what I saw:

enter image description here


Sure, convert the file to ASCII and blast all Unicode characters away. It will probably work... But...

  1. You won't know what you fixed.
  2. It will also destroy any Unicode comments. Example: //: A²+B²=C²
  3. It could potentially damage obvious logic and the code will still be broken, but the solution less obvious. For example: A string with "Smart-Quotes" (“ & ”) or a pointer with a full-width asterisk (*). Now “SOME_THING” looks like a #define (SOME_THING) and *SomeType is the wrong type (SomeType).

Two more surgical approaches to fixing the problem:

  1. Switch fonts to see the character. (It might be invisible in your current font)

  2. Regular expression search all Unicode characters not part of non-extended ASCII.

    In Notepad++ I can search up to FFFF, which hasn't failed me yet.

    [\x{80}-\x{FFFF}]

    80 is hex for 128, the first extended ASCII character.

    After hitting "find next" and highlighting what appears to be empty space, you can close your search dialog and press Ctrl + C to copy to clipboard.

    Then paste the character into a Unicode search tool. I usually use an online one. http://unicode.scarfboy.com/

Example:

I had a bullet point (•) in my code somehow. The Unicode value is 2022 (hex), but when read as ASCII by the compiler you get \342 \200 \242 (3 octal values). It's not as simple as converting each octal values to hex and smashing them together. So "E2 80 A2" is not the hexadecimal Unicode point in your code.


I got the same with a character that visibly appeared as an asterisk, but it was a UTF-8 sequence instead:

Encoder * st;

When compiled, it returned:

g.c:2:1: error: stray ‘\342’ in program
g.c:2:1: error: stray ‘\210’ in program
g.c:2:1: error: stray ‘\227’ in program

342 210 227 turns out to be UTF-8 for ASTERISK OPERATOR (Unicode code point U+2217).

Deleting the '*' and typing it again fixed the problem.