0

I don't know why this syntax error appears in this program.

#include <stdio.h>
#include <stdbool.h>

#define N 30

int main(void) {
    char sieve[N], *sp;
    int number;
    for(sp = sieve; sp < sieve + N;)
        *sp++ = true;

    for(number = 3;; number += 2) {
        sp = sieve + (number – 3)/2;
        if(sp >= sieve + N)
            break;
        while(sp += number, sp < sieve + N)
            *sp = false;
    }

    printf("2\n");
    for(number = 3, sp = sieve; sp < sieve + N; number += 2, sp++) {
        if(*sp)
            printf("%d\n", number);
    }

    return 0;
}

It appears in line 13 : sp = sieve + (number - 3)/2;

error: stray '\342' in program
error: stray '\200' in program
error: stray '\223' in program
error: expected '>' before numeric constant.

I tried by another compiler too and also I tried by this way also:

sp = sieve + ((number - 3)/2);

But nothing changes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashish Rawat
  • 3,363
  • 5
  • 29
  • 35
  • 3
    Check your editor's settings. Are you editing the file in an unusual encoding? – Graham Borland Mar 22 '13 at 12:44
  • 4
    Delete the line and type it again. It probably contains invalid characters for this encoding. – Alexander Mar 22 '13 at 12:44
  • 1
    Are you editing the source with a plain text editor or a word processor? It looks like it's inserting formatting characters or localization into the source? – K Scott Piel Mar 22 '13 at 12:45
  • Those are probably unicode characters, C expects ASCII only. – jim mcnamara Mar 22 '13 at 12:45
  • I'm using sublime text 2. – Ashish Rawat Mar 22 '13 at 12:46
  • Did you paste the code into Sublime Text from a word processor or web page? – Graham Borland Mar 22 '13 at 12:47
  • No, I write it by myself. I also tried in code::blocks – Ashish Rawat Mar 22 '13 at 12:51
  • 1
    Apart from the compiler errors, you really _must_ rewrite this code. You have managed to obfuscate very simple code into an unreadable mess, because of all the pointless comma operator spam. I see no reason what-so-ever why this whole code can't be written using plain for loops with 3 statements and no commas. Also why would you increase a character variable with the value `true`? Your code doesn't make any sense. – Lundin Mar 22 '13 at 12:56
  • 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:05

1 Answers1

8

The character for the minus sign on line 13 isn't a minus sign at all, but a hyphen (I think that's what it's called).

 sp = sieve + (number – 3)/2;

should be

 sp = sieve + (number - 3)/2;

When I changed that character the code compiles.

jpw
  • 44,361
  • 6
  • 66
  • 86
  • 4
    Yup, the em-dash. That's a good sign he copied and pasted his code from wikipedia :0 – sehe Mar 22 '13 at 12:54
  • Someone ought to contact Wikipedia - let them know code should be formatted differently. –  Mar 22 '13 at 12:59
  • @sehe If this is taken from Wikipedia, then for the love of obfuscation find the code and purge it from existence. Not so much for the symbol issue, but rather so that nobody starts thinking that code like this is canon for how to write C programs. – Lundin Mar 22 '13 at 13:02
  • @Lundin I haven't looked at the code. It's just that code formatting on wiki's/blogs frequently corrupts dashes, quotes etc. – sehe Mar 22 '13 at 13:02
  • No, [en dash](https://en.wiktionary.org/wiki/en_dash#Noun) (not [em dash](https://en.wiktionary.org/wiki/em_dash#Noun)). (Unicode code point [U+2013](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). The UTF-8 byte sequence for EN DASH is 342 200 223 (octal, as in the question). 0xE2 0x80 0x93 (hexadecimal). It would be 224 instead of 223 for EM DASH (U+2014). – Peter Mortensen Apr 18 '23 at 12:45