0

I have a simple c++ code like this:

#include <iostream>
#include <string>
using namespace std;
int main() {
    int n , a , b ;
    cin >> n >> a >> b ;
    //This two lines are exactly same!!!!
    cout << n - max(a + 1, n - b) + 1 << endl ;
    //cout << n - max(a + 1, n - b) + 1 << endl ;
}

In this code I have two line that are exactly same but when I compile the exact above code I get my result with any input for example(5,2,3) but when I uncomment the second cout and comment the first one with all previous condition the code doesn't compile(GNU 4.8.2) and get this error:

sample.cpp:8:5: error: stray ‘\342’ in program
     cout << n - max(a + 1, n - b) + 1 << endl ;
     ^
sample.cpp:8:5: error: stray ‘\200’ in program
sample.cpp:8:5: error: stray ‘\211’ in program
sample.cpp:8:5: error: stray ‘\342’ in program
sample.cpp:8:5: error: stray ‘\200’ in program
sample.cpp:8:5: error: stray ‘\211’ in program
sample.cpp:8:5: error: stray ‘\342’ in program
sample.cpp:8:5: error: stray ‘\200’ in program
sample.cpp:8:5: error: stray ‘\211’ in program
sample.cpp:8:5: error: stray ‘\342’ in program
sample.cpp:8:5: error: stray ‘\200’ in program

I try with Microsoft Visual C++ 2010 compiler and I got the same result but with different error:

program.cpp
program.cpp(9) : error C2065: 'n¢?%' : undeclared identifier
program.cpp(9) : error C2065: 'a¢?%' : undeclared identifier
program.cpp(9) : error C2065: '¢?%1' : undeclared identifier
program.cpp(9) : error C2065: '¢?%n¢?%' : undeclared identifier
program.cpp(9) : error C2065: '¢?%b' : undeclared identifier
program.cpp(9) : error C2146: syntax error : missing ';' before identifier '¢?%'
program.cpp(9) : error C3861: '¢?%max': identifier not found
program.cpp(9) : error C2065: '¢?%' : undeclared identifier
program.cpp(9) : error C2065: '¢?%1' : undeclared identifier

I can't understand both cout are exactly same but first one work properly and second one doesn't why is this a compiler bug??

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel.V
  • 2,322
  • 7
  • 28
  • 58
  • consider using a different text editor that won't insert weird characters.. I heartily recommend vim – M.M Oct 04 '14 at 12:13
  • @M.M: It is more likely caused by copying the code from a web page, a [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) document, 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 26 '23 at 00:16
  • The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 26 '23 at 00:17

2 Answers2

8

The two lines are not the same - the second has multibyte UTF-8 character in it:

$ cat line1 | xxd -g 1
0000000: 20 20 20 20 63 6f 75 74 20 3c 3c 20 6e 20 2d 20      cout << n - 
0000010: 6d 61 78 28 61 20 2b 20 31 2c 20 6e 20 2d 20 62  max(a + 1, n - b
0000020: 29 20 2b 20 31 20 3c 3c 20 65 6e 64 6c 20 3b 20  ) + 1 << endl ; 
0000030: 0a                                               .

$ cat line2 | xxd -g 1
0000000: 20 20 20 20 63 6f 75 74 20 3c 3c 20 6e e2 80 89      cout << n...
0000010: 2d e2 80 89 6d 61 78 28 61 e2 80 89 2b e2 80 89  -...max(a...+...
0000020: 31 2c e2 80 89 6e e2 80 89 2d e2 80 89 62 29 e2  1,...n...-...b).
0000030: 80 89 2b e2 80 89 31 20 3c 3c 20 65 6e 64 6c 20  ..+...1 << endl 
0000040: 3b 0a                                            ;.

Some of the whitespaces on the second line are of the U+2009 'THIN SPACE' variety (e2 80 89 in UTF-8).

The compiler does not like these whitespaces and makes its displeasure known (with what I think is a reasonably clear error message).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • @Daniel.V: `xxd` is a handy tool for situations like this. – NPE Oct 04 '14 at 11:37
  • Anyway, the error-message makes it obvious. It in both cases complains about bytes which should not be there. – Deduplicator Oct 04 '14 at 11:39
  • When it is known, it can be searched for (and replaced) in a sufficiently modern text editor with the regular expression `\x{2009}`. That comes in handy for space (zero width or not), e.g. `\x{00A0}` for [NO-BREAK SPACE](https://www.charset.org/utf-8). – Peter Mortensen Apr 26 '23 at 00:31
  • Note: The notation is different in Visual Studio Code (and probably others): `\u2009` (instead of `\x{2009}`) – Peter Mortensen Apr 27 '23 at 13:16
0

Choose the encoding, for example, in Notepad++ like UTF-8 without BOM. I think it will be OK.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131