-2

Every time when I do a comparison with a negative number, GCC shows me this:

error: stray '\342' in program
   if ( -4 < ÔÇô3 ) return 1;
   ^
compilation terminated due to -Wfatal-errors.

Code:

int main() {
  if ( -4 < –3 ) return 1;
  return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rsk82
  • 28,217
  • 50
  • 150
  • 240
  • 1
    Use a proper text editor to write your code. – Brian Bi Jun 07 '14 at 20:07
  • 4
    I think it's pretty obvious which character is causing the problem. That's an altered hyphen. Dumb [website](http://rishida.net/tools/conversion/) doesn't have a way to share results that I can see, but http://i.imgur.com/Q6QtHCm.png. Compare that to a [normal hyphen](http://i.imgur.com/7tdVnWd.png). – chris Jun 07 '14 at 20:07
  • Didn't you leave out the next two stray characters reported (they form one unit in this case)? Many of these start with 342 (octal): U+200B (342 200 213, [ZERO WIDTH SPACE](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)), U+2013 (342 200 223, [EN DASH](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)), U+2014 (342 200 224, [EM DASH](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)), and U+2212 (342 210 222, [MINUS SIGN](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8704&number=128)). – Peter Mortensen Mar 06 '21 at 20:48
  • OK, for the code posted here, closest to the source, it is 342 200 223. 342 200 223 (octal) → 0xE2 0x80 0x93 (hexadecimal) → UTF-8 sequence for Unicode code point U+2013 ([EN DASH](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8704&number=128)). Most text editors (e.g. [Geany](https://en.wikipedia.org/wiki/Geany) (Linux and Windows) and [Notepad++](https://en.wikipedia.org/wiki/Notepad%2B%2B)) with a [regular expression](https://en.wikipedia.org/wiki/Regular_expression) mode will be able to do search/replace for Unicode code point U+2013, using `\x{2013}`. – Peter Mortensen Mar 06 '21 at 20:59
  • Note: The notation is different in Visual Studio Code (and probably others): `\u2013` (instead of `\x{2013}`) – Peter Mortensen Apr 27 '23 at 18:36
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. – Peter Mortensen Apr 27 '23 at 18:38

1 Answers1

5

The dash before the number three is a Unicode character that looks like a dash. Delete it, and insert a new dash and you'll be fine.

If you used copy-paste, then beware, as pasted code can contain Unicode characters and that may lead to compilation errors like this.

If you typed it manually into an editor, use a different editor or alter the settings of the editor not to convert standard hyphens into other characters. E.g., Microsoft Word does this conversion, but I hope that you did not write code in Word.


The explanation of the error message:

You saved the file in UTF-8 charset. The altered hyphen you used is saved as three consecutive bytes: 0xE2 0x80 0x93 (hexadecimal values)

g++ parses the file byte by byte. It encounters 0xE2 first, which it considers invalid and outputs error: stray '\342' in program. The 342 is the octal representation of 0xE2. And \342 is valid C++ code, it is the escaped version of 0xE2 character. See C++ escape sequences for details, \nnn means the character represented by the octal nnn number.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Csq
  • 5,775
  • 6
  • 26
  • 39
  • 1
    You'll only be fine if you're not using an editor which automatically converts `-` to `–`. I think that is not a safe assumption: if it were, the OP wouldn't have asked this question. –  Jun 07 '14 at 20:09
  • @hvd Thanks for the comment, I've updated the answer. I suspect some copy-paste behind the scenes. – Csq Jun 07 '14 at 20:12