10

I'm using Code::Blocks on Ubuntu 10.10 (Maverick Meerkat). I have connected a Mac keyboard and set the keyboard settings to "Swiss German Mac". Now whenever I write an equals sign, followed by a space (something like width = 100) I get the error message: stray '\302' in program.

I know this error means that there is a non-standard character in the text file.

When I delete the space character, the program compiles just fine. So that means Code::Blocks adds some sort of special character. But I can't see why this happens. What is the reason?

What character does '\302' stand for?

[UPDATE]

I got a little further investigating the problem. I get this stray when I use the combo Shift + Space. Now that I know it doesn't happen that often any more. But it's still rather annoying especially when writing code... Is there a way to turn off this combo in X11?

[SOLVED]

Thanks to Useless's answer, I was able to solve the "issue". It's more of a feature actually. Shift + space created a spacenolinebreak by default. So by changing the xmodmap with

xmodmap -e "keycode  65 = space space space space space space"

this behavior was overridden and everything works fine now.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
underdoeg
  • 1,862
  • 4
  • 14
  • 26
  • Didn't you leave something out (stray \240)? The characters often form a unit (UTF-8 byte sequences of two or three bytes). \240 is a signature for NO-BREAK SPACE. The likely sequence is 302 240 (octal) → 0xC2 0xA0 (hexadecimal) → UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x)). – Peter Mortensen Mar 06 '21 at 23:16
  • 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+00A0, using `\x{00A0}`. – Peter Mortensen Mar 06 '21 at 23:16
  • Does this answer your question? [Compilation error: stray ‘\302’ in program, etc](https://stackoverflow.com/questions/19198332/compilation-error-stray-302-in-program-etc) – Karl Knechtel Jan 16 '23 at 14:33
  • Or it could actually be only \240 ([CE/CP-1250](https://en.wikipedia.org/wiki/Windows-1250) instead of UTF-8). – Peter Mortensen Apr 25 '23 at 15:33

9 Answers9

7

\302 stands for the octal representation of byte value the compiler encountered. It translates to 11000010 in binary, which makes me think it's the start of a two byte UTF-8 sequence. Then this sequence must be:

11000010 10??????

Which encodes the binary Unicode point 10??????, which can be anything from U+80 to U+BF.

Several characters starting from U+80 are special spaces and breaks which usually are not shown inside a text editor.

Probably it's not your editor, but Xorg, that emits these characters due to your keyboard settings. Try switching to a generic US keyboard and test if the problems persists.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tugrul Ates
  • 9,451
  • 2
  • 33
  • 59
  • Hi. This topic is rather old now. But you were right it happend, when i pressed ctrl + space. I can work around this now. But while programming it still happens a lot in lines like "int a = 10" or so. Do you know how I can disable this particular key combo in X11? – underdoeg May 22 '11 at 13:21
  • Unfortunately no. You may edit your question to tell how far you have come for others to notice your problem again. – Tugrul Ates Jun 24 '11 at 03:35
  • [\302 is 194](http://www.wolframalpha.com/input/?i=302+octal+in+decimal), so not a two byte sequence. – Yakov Galka Jun 29 '11 at 11:30
  • @ybungalobill bytes that have their msb set are part of a sequence in utf-8. – Tugrul Ates Jun 29 '11 at 14:55
  • AH, sorry. I misunderstood you. – Yakov Galka Jun 29 '11 at 16:10
  • Yes, a stray error, \240, was probably left out. Thus 302 240 (octal) → 0xC2 0xA0 (hexadecimal) → UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x)). – Peter Mortensen Mar 06 '21 at 23:12
  • `0xA0` is binary `10 10 0000`, so it fits your hypothesis. – Peter Mortensen Mar 06 '21 at 23:20
7

Since you're sure it's caused by hitting Shift + Space, you can check what X itself is doing by. First, run xev from the command line, hit Shift + Space and check the output. For example, I see:

$ xev
KeyPress event, serial 29, synthetic NO, window 0x2000001,
    root 0x3a, subw 0x0, time 4114211795, (-576,-249), root:(414,593),
    state 0x0, keycode 50 (keysym 0xffe1, Shift_L), same_screen YES,
    XLookupString gives 0 bytes:
    XmbLookupString gives 0 bytes:
    XFilterEvent returns: False

KeyPress event, serial 29, synthetic NO, window 0x2000001,
    root 0x3a, subw 0x0, time 4114213059, (-576,-249), root:(414,593),
    state 0x1, keycode 65 (keysym 0x20, space), same_screen YES,
    XLookupString gives 1 bytes: (20) " "
    XmbLookupString gives 1 bytes: (20) " "
    XFilterEvent returns: False
...

Then, run xmodmap -pk and look up the keycode (space should be 65 as above, but check your xev output).

If you see something like

65         0x0020 (space)

Then X isn't doing this. On the other hand, if I pick a character key which is modified by shift, I see something like this:

58         0x006d (m)      0x004d (M)

If you have two or more keysyms for your keycode, X is the culprit. In that case, something like xmodmap -e 'keycode 65 space' should work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Useless
  • 64,155
  • 6
  • 88
  • 132
1

I have seen this type of issue when copying and pasting from web pages or other electronic documents. The common culprits would be invalid quotes like ` instead of ', or something alike. Try to use the compiler error to guide you into where in the file the error might be.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • thanks. I know where in the file the error is, I get a nice stray error with line number and everything. What I try to do now is to simply deactivate this key combination: shift + space – underdoeg Jun 28 '11 at 10:51
  • Indeed. I ran into one, copying code from [this page](https://beta.docs.qmk.fm/using-qmk/advanced-keycodes/feature_macros). It was a [ZERO WIDTH SPACE](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128). A search/replace for `\x{200B}` in regular expression mode in a text editor eliminated it. – Peter Mortensen Mar 06 '21 at 23:23
  • They have now changed that page (also the corresponding non-broken link) so it no longer results in this error. – Peter Mortensen May 03 '23 at 19:03
0

I had the same issue by modified a US ASCII example file. So I convert it in UTF-8, and here is the GNU/Linux command:

iconv -c -t us-ascii -f utf-8 source_file -o dest_file

And then add my modifications… no more errors!

To verify the initial encoding, use

file -i source_file

I should add a non-ASCII character to allow iconv do the job!?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bcag2
  • 1,988
  • 1
  • 17
  • 31
0

That sounds like some kind of encoding issue. I haven't used Code::Blocks for years, so I am not sure if it allows you to pick different encodings.

How about opening your code file with gedit and saving it as UTF-8, and then try again? But it sounds rather strange that you get such an issue using space characters.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mario
  • 35,726
  • 5
  • 62
  • 78
  • opening and resaving doesn't work. The special character stays in the file. it must be some sort of special spacing. – underdoeg Jun 28 '11 at 10:51
0

'\302' is C notation for the octal number 3028, which equals C216 and 19410. So it's not ASCII.

Which character it maps to depends on the encoding. In Latin-1, it's the  character, for instance.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • It is more likely he left out the next stray error message (\240). That would be 302 240 (octal) → 0xC2 0xA0 (hexadecimal) → UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x)). – Peter Mortensen Mar 06 '21 at 23:16
0

I've seen this problem in my Linux box with a Finnish keyboard.

It also happens with Emacs, etc. I don't have a good solution for it, but I guess reports about the fact that it happens elsewhere are also useful...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tp1
  • 1,197
  • 10
  • 17
  • 1
    note that this only happens when typing characters like {[]} which are part of characters that need to pressed alt-gr key in finnish keyboard. Also the characters are pretty much invisible until you try to compile the code. – tp1 Jun 27 '11 at 17:02
  • 1
    No, for me it happens with shift and space, it's a Swiss keyboard. so I also have to press alt for {[]} but never had any problems... – underdoeg Jun 27 '11 at 18:43
0

If you open your file in Emacs and set-buffer-file-coding-system to something like "unix" or some ASCII variety, then when you try to save, it'll warn you that the buffer contains unrepresentable characters and points you to them so you can fix them.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

I decided to move the file from a MacBook to a Linux box and used email with my iCloud address. When I opened the transferred file, the errors had gone and the file compiled!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
matrixmike
  • 31
  • 3