0

I'm writing a C program to print the date of Easter for a given year using the Gaussian algorithm. I'm really new to C. Here's the code:

#include <math.h>

int main () {
  int year = 1998;
  int a = year % 19;
  int b = year % 4;
  int c = year % 7;
  int k = floor (year/100);
  int p = floor ((13 + 8k)/25);
  int q = floor (k/4);
  int M = (15 − p + k − q) % 30;
  int N = (4 + k − q) % 7;
  int d = (19a + M) % 30;
  int e = (2b + 4c + 6d + N) % 7;
  if (d == 29 && e == 6) {
    printf("19 April");
  }
  else if (d == 28 && e = 6 && (11M + 11) % 30 < 19) {
    printf("18 April");
  }
  else if (22 + d + e < 32) {
    printf("%d March", (22 + d + e));
  }
  else {
    printf("%d April", d + e - 9);
  }
  return 0;
}

And the errors according to CodePad:

Line 23: error: invalid suffix "k" on integer constant
In function 'main':
Line 10: error: stray '\342' in program
Line 10: error: stray '\210' in program
Line 10: error: stray '\222' in program
Line 10: error: expected ')' before 'p'
Line 10: error: stray '\342' in program
Line 10: error: stray '\210' in program
Line 10: error: stray '\222' in program
Line 11: error: stray '\342' in program
Line 11: error: stray '\210' in program
Line 11: error: stray '\222' in program
Line 11: error: expected ')' before 'q'
Line 11: error: invalid suffix "a" on integer constant
Line 11: error: invalid suffix "b" on integer constant
Line 16: error: invalid suffix "c" on integer constant
Line 21: error: invalid suffix "d" on integer constant
Line 32: error: invalid suffix "M" on integer constant

As far as I can tell, there isn't any no "k" in line 23, so why is the compiler complaining?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bad Request
  • 3,990
  • 5
  • 33
  • 37
  • Well, for one thing, `else if (d == 28 && e = 6 && (11M + 11) % 30 < 19) {` should be `else if (d == 28 && e == 6 && (11M + 11) % 30 < 19) {`. That probably won't fix the error, though. – Maxpm Nov 26 '10 at 01:23
  • extreme [stray ... in program](http://codepad.org/AjMKDKz6) :-) – pmg Nov 26 '10 at 21:46
  • you might be interested in this [stack-exchange proposal](http://area51.stackexchange.com/proposals/11464/code-review?referrer=aWNm_PdciyFqjFW8CUacGw2 "Code Review"). – greatwolf Jan 13 '11 at 09:01
  • A direct analysis is: 342 210 222 (octal) → 0xE2 0x88 0x92 (hexadecimal) → UTF-8 sequence for Unicode code point U+2212 ([MINUS SIGN](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8704&number=128)). It can be searched for (and replaced) in any modern text editor or IDE using the regular expression `\x{2212}` (note: The notation is different in Visual Studio Code (and probably others): `\u2212` (instead of `\x{2212}`)). – Peter Mortensen May 01 '23 at 02:20
  • 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 May 01 '23 at 02:22
  • Though the stray error should have been ignored. The other error happened to be first. This ought to have been two questions, one for the stray error and one for the notation error. They are two completely different problems. – Peter Mortensen May 01 '23 at 02:35

4 Answers4

9

I think this 8k was probably intended to be 8*k:

int p = floor ((13 + 8k)/25);
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    And likewise many other "multiplications". The other errors are probably due to your compiler not supporting non-ASCII characters in source; it looks like you have some UTF-8 comments or strings that you did not include in the copy of the code you showed us. – R.. GitHub STOP HELPING ICE Nov 26 '10 at 01:26
  • The non-ASCII is in the lines starting "int M" and "int N". Also, in the line "else if (d == 28 && e = 6 && (11M + 11) % 30 < 19) {", that should be "e == 6". – Steve Jessop Nov 26 '10 at 01:28
  • 2
    No, you typed UTF-8 `U+2212 MINUS SIGN` instead of the standard `HYPHEN-MINUS` character for your subtractions. You'll need to fix that too. – R.. GitHub STOP HELPING ICE Nov 26 '10 at 01:29
  • Yep, the multiplications were a good catch, but it was the minus signs that would really have stumped me if you didn't point them out! Wish I could "accept" your comment. – Bad Request Nov 26 '10 at 01:43
1

When you multiply integers you need to use 8*k not 8k.

GWW
  • 43,129
  • 11
  • 115
  • 108
1

Besides of the Unicode characters used (you shouldn't edit the program with Winword or any other text processor), you do not need the floor function as you're using only integers all along. An integer variable can not hold fractional values so when you divide year by 100 you will only get the integral part of your quotient.

In the line

 if (d == 28 && e = 6 && (11M + 11) % 30 < 19)

beside the missing * you have a = that should be a ==.

Patrick Schlüter
  • 11,394
  • 1
  • 43
  • 48
0
int p = floor ((13 + 8k)/25);

What's 8k? Do you mean 8*k?

Similar problems at the below places also:

 int d = (19a + M) % 30; 

 int e = (2b + 4c + 6d + N) % 7;

 else if (d == 28 && e = 6 && (11M + 11) % 30 < 19) {   
Jay
  • 24,173
  • 25
  • 93
  • 141