简体   繁体   English

为什么海湾合作委员会不这样?

[英]Why doesn't GCC like this?

I am trying to learn C and finding myself getting stuck a lot, no complains :) 我正在努力学习C并发现自己陷入困境,没有抱怨:)

Anyway, I wrote a program and GCC does not like it. 无论如何,我写了一个程序,GCC不喜欢它。 The following code is NOT the program, but demonstrate the problem: 以下代码不是程序,但演示了问题:

#define MAXLINE = 1000

int main()
{
   int tmp = MAXLINE;
   char line[MAXLINE];

   return 0;
}

When it is compiled, I get the following error: 编译时,我收到以下错误:

test.c:7: error: expected expression before '=' token test.c:7:错误:'='标记之前的预期表达式

If I replace symbolic constant MAXLINE with int 1000, everything works. 如果我用int 1000替换符号常量MAXLINE,一切正常。

What is going on? 到底是怎么回事?

When the preprocessor replaces your definition of MAXLINE , your code is changed to 当预处理器替换MAXLINE的定义时,您的代码将更改为

int main()
{
   int tmp = = 1000;
   char line[= 1000];
   return 0;
}

The C preprocessor is very dumb! C预处理器非常笨! Do not put anything extra in your #defines (no equals, no semicolons, no nothing) 不要在#defines中添加任何额外内容(不等于,没有分号,没有任何内容)

定义不需要等号:)

#define maxline 1000

不应该只定义=

#define MAXLINE 1000

The #define statement doesn't need the equals sign. #define语句不需要等号。

It should read: 它应该是:

#define MAXLINE 1000

使用#define而不是'=':

#define MAXLINE 1000
#define MAXLINE 1000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM