Ubuntu: How to fix compilation errors that mention “stray ‘\342’” and “stray ‘\200’”?



Question:

I wrote this program:

#include<stdio.h>  int main()  {  printf(“Hello World\n”);  return 0;  }  

I saved it as first.c and tried to compile but receiving this problem I have installed even gcc compiler too.

zahid@zahid-HP-Compaq-nc6400-EH522AV:~/programs$ gcc first.c -o first1  first.c: In function ‘main’:  first.c:4:1: error: stray ‘\342’ in program  first.c:4:1: error: stray ‘\200’ in program  first.c:4:1: error: stray ‘\234’ in program  first.c:4:11: error: ‘Hello’ undeclared (first use in this function)  first.c:4:11: note: each undeclared identifier is reported only once for each function   it appears in  first.c:4:17: error: expected ‘)’ before ‘World’  first.c:4:17: error: stray ‘\’ in program  first.c:4:17: error: stray ‘\342’ in program  first.c:4:17: error: stray ‘\200’ in program  first.c:4:17: error: stray ‘\235’ in program  zahid@zahid-HP-Compaq-nc6400-EH522AV:~/programs$  

How can I fix this problem?


Solution:1

The problem is that you have Unicode quotation marks instead of ASCII quotation marks; probably your editor automatically changed them, or you copied the text from a site that does this automatically in its authoring software. Replace the quotes with normal ASCII quote (0x22, ") and it should work.

Fixed source:

#include <stdio.h>  int main()  {      printf("Hello World\n");      return 0;  }  


Solution:2

You're enclosing your string in the wrong kind of quotes.

You have:

“Hello World\n”  

You should instead have:

"Hello World\n"  

While the quotes you've used look similar, they are not recognized by a C compiler as enclosing a string. Thus, you get error messages about unrecognized Unicode characters, and you get error messages showing that the contents of the string are being interpreted as unquoted program code.

You will note that the quotes you've used have separate characters for beginning and ending a quotation (they are curved the way quotes are often typeset). Many word processors--as Jobin suggested--will automatically turn simple " quotes into such fancy quotation marks. If you're using a word processor to compose C programs, you should use a text editor instead.

Ubuntu comes with gedit installed by default (though there are many other text editors to choose from, too). Like many text editors, gedit provides syntax highlighting for many programming languages including C (so different text in your program will shown in different colors to signify its meaning), which is a handy feature and makes a text editor much more convenient and user-friendly for programming than a word processor.

This might be Ubuntu-specific, if you're used to a text editor on another platform that automatically reduces pasted quotations marks to the non-fancy version. But this question might end up getting closed and migrated to Stack Overflow.


Solution:3

You can use the sed command to fix these issues.

This will give you a quick preview of what will be replaced.

sed s/[”“]/'"'/g File.txt  

This will do the replacements and put the replacement in a new file called WithoutSmartQuotes.txt.

sed s/[”“]/'"'/g File.txt > WithoutSmartQuotes.txt  

This will overwrite the original file.

sed -i ".bk" s/[”“]/'"'/g File.txt  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »