3

Here is my code

int main()​
{
    float avg, age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
    avg = average(age); /* Only name of array is passed as argument. */
    printf("Average age=%.2f", avg);
    return 0;
}

Compilation error in int main():

error: stray '\342' in program
error: stray '\200' in program
error: stray '\213' in program
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nadim Tareq
  • 481
  • 6
  • 7
  • Show us the error message. – Sourav Ghosh Nov 22 '17 at 12:32
  • And the compilation statement, too. – Sourav Ghosh Nov 22 '17 at 12:32
  • 2
    Are you copying code from MS Word? – Jonathon Reinhart Nov 22 '17 at 12:33
  • No copy from another – Nadim Tareq Nov 22 '17 at 12:34
  • 3
    It seems to be a UTF-8 byte sequence for a BOM character, a zero-width space at the begin of the file to let Notepad on Windows detect it is a UTF-8 text file. With another editor like Notepad++ you can save it as UTF-8 without BOM. Or a couple of backspaces deleting the first char of the text. Maybe it would help to set the projects encoding to UTF-8 too; the compiler for instance. – Joop Eggen Nov 22 '17 at 12:39
  • 1
    @JoopEggen the BOM isn't a zero-width space, afaik. –  Nov 22 '17 at 12:43
  • I believe you should set Codeblocks Settings -> Editor -> Other settings tab -> Use encoding when opening files: UTF-8. And then check "use this encoding as fallback encoding". – Lundin Nov 22 '17 at 12:55
  • @FelixPalmen Wikipedia cited: _If the BOM (Byte Order Mark) character appears in the middle of a data stream, Unicode says it should be interpreted as a "zero-width non-breaking space."_ My description was intended to hint that the character is not seen, a ghost. – Joop Eggen Nov 22 '17 at 12:56
  • And please don't dump us with screen shots of compiler messages. Their text suffices. – Jens Gustedt Nov 22 '17 at 13:19
  • 1
    @JoopEggen That's the wrong way around. The BOM is a different sequence and this just tells it should be seen as whitespace when appearing somewhere in the middle. The character codes in the error message are not the BOM. –  Nov 22 '17 at 14:29
  • 1
    @FelixPalmen Apologies, I have checked the values, and they are not of a BOM. So there must be special chars. Could happen via an included macro too. – Joop Eggen Nov 22 '17 at 14:37
  • @JoopEggen it's an *actual* "zero width space" here. But yes, a BOM would lead to a very similar error. –  Nov 22 '17 at 14:39
  • 1
    This is a duplicate. The canonical is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Mar 05 '21 at 05:32
  • Though the canonical is currently lacking *comprehensive* answers. – Peter Mortensen Jan 16 '23 at 13:53
  • 1
    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:35
  • 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. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 18 '23 at 13:04

4 Answers4

8

You have "crap characters" in your source file.

\342 \200 \213 is octal for 0xE2 0x80 0x8B which is a zero width space in UTF-8 (Unicode U+200B), something no C compiler can make sense of (and something you can't see, zero-width after all, when UTF-8 is displayed correctly).

-> Use a text or code editor (even Windows' Notepad should do, if not saving as UTF-8, but any other editor would be better) and/or an integrated development environment to write your code. Don't ever use word processors or the like, that might introduce unwanted characters.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • How do you remember the exact byte sequence of that zero-width-space? – iBug Nov 22 '17 at 12:40
  • 1
    @iBug convert octal to hex and google for the hex? With such a sequence of `>0x7f` characters, it's pretty obvious it's an UTF-8 sequence. –  Nov 22 '17 at 12:42
  • 1
    *"(even windows' "notepad" should do)"* Unfortunately, if you save file as UTF-8 it will include BOM too without telling you. Windows Notepad is so crap that it should not be used at all. – user694733 Nov 22 '17 at 12:54
  • @user694733 well, the problem *here* wasn't a BOM, but good to know ... this would of course lead to a *similar* problem. –  Nov 22 '17 at 14:30
  • It is usually not caused by using word processors, but by copy-pasting code from web pages, PDF files, or chat (e.g. Skype Chat). – Peter Mortensen Mar 05 '21 at 05:35
1

This seem to be a problem with bad characters in your code. Characters that are non-printable like ╚ or something. Open your code for example in Notepad++, select menu option to view all characters, menu ViewShow invisible charactersShow all characters and remove any bad characters from your code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wookie88
  • 33,079
  • 4
  • 27
  • 32
  • This will not help with zero width space (U+200B) that seems [to be the case here](https://stackoverflow.com/questions/47434802/error-stray-342-stray-200-stray-213-in-c-compiling/47434894#47434894). However, some code editors can do Unicode code point search and replace (e.g., [Notepad++](https://en.wikipedia.org/wiki/Notepad%2B%2B)). – Peter Mortensen Mar 05 '21 at 05:38
  • To be more precise, in this case, use `\x{200B}` in a regular expression search. – Peter Mortensen Mar 06 '21 at 19:24
1

The code looks fine.

According to my personal experience, stray '\xxx' is often caused by using invalid characters in your code (except in string literals), which happens more frequently if your language is not English.

For example, this code generates such an error:

int main(){return 0;}

I can't find what's wrong, so my only suggestion is make sure your IME is switched to English, then type the code again.

iBug
  • 35,554
  • 7
  • 89
  • 134
1

Use the following website. It will show you bad characters and then remove them:

View non-printable Unicode characters

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahmoud Mabrok
  • 1,362
  • 16
  • 24