C语言 “整数常量上的后缀“k”无效”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4281943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:12:31  来源:igfitidea点击:

"invalid suffix "k" on integer constant" error

c

提问by Bad Request

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:

我正在编写一个 C 程序来使用高斯算法打印给定年份的复活节日期。我对 C 真的很陌生。这是代码:

#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 '2' in program
Line 10: error: stray '0' in program
Line 10: error: stray '2' in program
Line 10: error: expected ')' before 'p'
Line 10: error: stray '2' in program
Line 10: error: stray '0' in program
Line 10: error: stray '2' in program
Line 11: error: stray '2' in program
Line 11: error: stray '0' in program
Line 11: error: stray '2' 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's no "k" in line 23, so why is the compiler complaining?

据我所知,第 23 行没有“k”,那么为什么编译器会抱怨呢?

回答by R.. GitHub STOP HELPING ICE

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

我认为这8k可能是为了8*k

int p = floor ((13 + 8k)/25);

回答by GWW

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

当您将整数相乘时,您需要使用 8*k 而不是 8k。

回答by Patrick Schlüter

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

除了使用的 Unicode 字符(您不应使用 Winword 或任何其他文本处理器编辑程序)之外,您不需要该floor函数,因为您一直只使用整数。整数变量不能保存小数值,因此当您除以year100 时,您只会得到商的整数部分。

In the line

在行

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

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

除了缺少的之外,*您还有一个=应该是==.

回答by Jay

int p = floor ((13 + 8k)/25);

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

什么是8k?你的意思是 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) {