0

I am getting this error when I try to compile... I am new to coding in general, so it might be obvious and I'm not seeing it.

My code is:

#include <stdio.h>

int main ()
{
   float x​[7] = 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8;
   int i = 3;
   printf ("%.2f \n", x​[i + 1]);
   return 0;
}

It's a super simplistic code that is just supposed to output one number... However, every time I try to compile I get this error:

test.c:4:8: error: stray ‘\342’ in program
float x���[7] = 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8;
    ^
test.c:4:9: error: stray ‘\200’ in program
float x���[7] = 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8;
     ^
test.c:4:10: error: stray ‘\213’ in program
float x��[7] = 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8;
                   ^
test.c:6:21: error: stray ‘\342’ in program
printf ("%.2f \n", x���[i + 1]);
                 ^
test.c:6:22: error: stray ‘\200’ in program
printf ("%.2f \n", x���[i + 1]);
                  ^
test.c:6:23: error: stray ‘\213’ in program
printf ("%.2f \n", x��[i + 1]);

What might be causing this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike8490
  • 3
  • 2
  • 1
    `float x​[7] = 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8 ;` -> `float x​[7] = { 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8 };` – Jabberwocky Dec 14 '21 at 06:53
  • Also, does anyone know what the question mark diamond characters "�" mean? I assume it's that the code doesn't know what the actual characters are... much like how an iPhone will will print a box with a question mark in it if it receives an emoji from an android. – Mike8490 Dec 14 '21 at 06:56
  • 3
    The `stray ‘\342’` errors are not related to the errror I pointed out in the comment above. They are most likely due to the fact that you edited your code with an inappropriate editor. Which editor do you use? How do you compile? What is your platform? – Jabberwocky Dec 14 '21 at 06:56
  • 1
    Look at you code in an editor that will show white space characters. – Gerhard Dec 14 '21 at 06:57
  • I use VS code and I compile with gcc. My prof, who first gave the example, was on terminal or putty I believe... You think that has something to do with it? – Mike8490 Dec 14 '21 at 06:59
  • 2
    Did you originally try to copy and paste the code from somewhere? – Karl Knechtel Dec 14 '21 at 07:05
  • I copies the code from a slide which was used in class (we are going through the basics of c and the initializations of variables). But I put in {} before and after the numbers and it compiles just fine now! Thanks so much! – Mike8490 Dec 14 '21 at 07:08
  • Weird. I can't imagine why. Your code presumably still contains the undesired zero-width spaces. – Karl Knechtel Dec 14 '21 at 07:16
  • You have probably accidentally deleted the zero width space. 342 (octal) is a signature start of a UTF-8 byte sequence. 342 200 213 (octal) [is the UTF-8 sequence for ZERO WIDTH SPACE](https://stackoverflow.com/questions/19198332/compilation-error-stray-302-in-program-etc#comment117544674_19198332). – Peter Mortensen Feb 21 '23 at 07:58
  • 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:01
  • 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 Apr 19 '23 at 04:43
  • OK, the OP has left the building: *"Last seen more than 1 year ago"* – Peter Mortensen Apr 25 '23 at 19:17

2 Answers2

4

You have (presumably) tried to copy and paste your code from another source, and that source text contains one or more instances of an invisible character called a zero-width space. This is not valid in your source code. Your compiler is reporting these characters as the underlying bytes used to represent them: in UTF-8 encoding, the zero-width space is encoded with the bytes 0xe2, 0x80, 0x8b. These are, in turn, represented by the compiler as octal backslash escape sequences, giving the values from your error messages.

(Fixing this does not fix your program. You have another syntax error: you must use braces ({ and } symbols) to enclose your array data.)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
-1

You had missed { and } in

float x​[7] = 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8;

Corrected Code:

float x​[7] = { 23.0, -28.6, 30.2, 29.0, -8.0, -30.0, 27.8 };

Now it will give ans = -8.00

MagnusEffect
  • 3,363
  • 1
  • 16
  • 41