1

My professor gave us some code in a Microsoft Word document and said it should be able to compile, but I'm getting all sorts of errors and I don't know whats wrong because I have no experience whatsoever with C.

It's an assembly language class and we're supposed to write assembly code to match what the C code is doing. He told us to run the program in C to get a feel of things.

#include <stdio.h>
#define  SIZE  40

main()
{
    int v[SIZE];
    register int gap, i, j, temp;

    /*  Initialize array to random positive integers mod 256  */
    for (i = 0; i < SIZE; i++)
        v[i] = rand() & 0xFF;

    /*  Display the unsorted array  */
    for (i = 0; i < SIZE; i++)
        printf(“v[%-d] = %-d\n”, i, v[i]);

    /*  Sort the array using a shell sort  */
    for (gap = SIZE / 2; gap > 0; gap /= 2) {
        for (i = gap; i < SIZE; i++) {
            for (j = i - gap; j >= 0 && v[j] > v[j + gap]; j -= gap) {
                /*  Exchange out of order items  */
                temp = v[j];
                v[j] = v[j + gap];
                v[j + gap] = temp;
            }
        }
    }

    /*  Display the sorted array  */
    for (i = 0; i < SIZE; i++)
        printf(“v[%-d] = %-d\n”, i, v[i]);
    }

The errors I get are strays in lines 15 and 31, so each line that has a printf in it.

As3.c: In function ’main’:
As3.c:15: error: stray ’\223’ in program
As3.c:15: error: expected expression before ’%’ token
As3.c:15: error: expected expression before ’%’ token
As3.c:15: error: stray ’\’ in program
As3.c:15: error: stray ’\224’ in program
As3.c:31:error: stray ’\223’ in program
As3.c:31:error: expected expression before ’%’ token
As3.c:31:error: expected expression before ’%’ token
As3.c:31:error: stray ’\’ in program
As3.c:31:error: stray ’\224’ in program

How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1773059
  • 23
  • 1
  • 3
  • 2
    Those stray characters strongly hint at a bad or corrupted copy/paste. – Mysticial Oct 25 '12 at 04:12
  • 2
    Did you copy-paste this from MS Word or similar? The "magic quotes" are breaking it for one thing. – John Carter Oct 25 '12 at 04:13
  • yes he gave us the code in a ms word file, i'll try rewriting the code again in notepad. – user1773059 Oct 25 '12 at 04:17
  • 6
    What a n00b. (Your professor, that is.) – Jonathon Reinhart Oct 25 '12 at 04:18
  • Is this GCC? A much better title would be "stray character in program" and tag it with "gcc". – Jonathon Reinhart Oct 25 '12 at 04:19
  • 2
    You need to tell your professor that **MS WORD IS INAPPROPRIATE FOR CODE**. Yes, in bold caps. It is *that* bad. Not only is using an application like MS word totally inappropriate for code (non-monospace by default, no autoindent, no syntax highlighting) but it also *breaks* code as you can see since it uses typographical quotation marks etc. instead of the plain ASCII ones programming languages use. – ThiefMaster Oct 25 '12 at 10:25
  • This is a ***very*** common error when copying code. In this case, it is revealed it is from a [Microsoft Word](https://en.wikipedia.org/wiki/Microsoft_Word) document. Other sources are from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, and copying 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)). The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Jun 16 '23 at 11:57
  • It would be better if the close reason was changed to a duplicate, of *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Jun 16 '23 at 11:59
  • In this case, 223 and 224 are probably the single CE/[CP-1250](https://en.wikipedia.org/wiki/Windows-1250) ones, corresponding to Unicode code point U+201C ([LEFT DOUBLE QUOTATION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)) and Unicode code point U+201D ([RIGHT DOUBLE QUOTATION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). As posted here, they can ***positively*** (guesswork isn't required) ***be searched*** for (and replaced) using the regular expression `\x{201C}|\x{201D}`. – Peter Mortensen Jun 16 '23 at 12:54
  • cont' - Note: The notation is different in Visual Studio Code (and probably others): `\u201C|\u201D` (instead of `\x{201C}|\x{201D}`) – Peter Mortensen Jun 16 '23 at 12:57
  • LEFT DOUBLE QUOTATION MARK and RIGHT DOUBLE QUOTATION MARK are the only ones. There aren't any other of the common stray characters (observed from this kind of source). – Peter Mortensen Jun 16 '23 at 12:59

1 Answers1

9

It looks like some of the characters got "smart"-ified, probably by Microsoft Word or a similar program. You'll need to do a find-and-replace to change and to " (and perhaps likewise for other characters, though and are the only characters complained about in the specific compile-errors you've posted).

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 5
    And anyone who saves code in Microsoft Word should be shot, on-the-spot. – Jonathon Reinhart Oct 25 '12 at 04:17
  • Thank you, simply changing the quotes made the program run. I can't believe how simple it was. – user1773059 Oct 25 '12 at 04:21
  • Any proper "publishing program" should try to make opening and closing quotes look different. So, this is more like user error. Could be intentional too, to teach students something about source code, compiler behavior and copy-pasting. I think I'll totally do this if I'll ever teach stuff like this. – hyde Oct 25 '12 at 04:23
  • @user1773059 you can consider accepting the answer if it has solved your problem. You can do it by clicking the tick sign shown with the answer. – krammer Oct 25 '12 at 04:28
  • Had a similar error and there were no double quotes but the asterisk `*` was different. – heretoinfinity Feb 06 '22 at 15:29