为什么的#define INVALID_VALUE -999;使用时,给出一个语法错误? [英] Why does #define INVALID_VALUE -999; give a syntax error when used?

查看:416
本文介绍了为什么的#define INVALID_VALUE -999;使用时,给出一个语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较在C中定义的常量,我简化了我的程序如下:

 的#includestdio.h中
#包括stdlib.h中
#定义INVALID_VALUE -999;INT主要(无效)
{
    INT测试= 0;
    如果(测试== INVALID_VALUE)//将错误行..
        返回INVALID_VALUE;
    返回0;
}

当我使用gcc来编译,它给出了错误 错误:之前的预期')'';'令牌 <。 / p>

有什么理由,这不能做?


解决方案

从INVALID_VALUE定义中删除分号。

宏没有他们周围的语法的理解词汇替换(字符逐个字符)。您的宏INVALID_VALUE设置为 -999; ,所以你如果行宏扩展为:

 如果(测试==  -  999;)

这是无效的C语法。

I am trying to compare to a defined constants in C, and I have simplified my program to the following:

#include "stdio.h"
#include "stdlib.h"
#define INVALID_VALUE -999;

int main(void)
{
    int test=0;
    if(test==INVALID_VALUE) //The error line..
        return INVALID_VALUE;
    return 0;
}

And when I use gcc to compile, it gives out error "error: expected ‘)’ before ‘;’ token".

Is there any reason that this cannot be done?

解决方案

Remove the semicolon from your INVALID_VALUE definition.

Macros are replaced lexically (character-by-character) with no understanding of the syntax around them. Your macro INVALID_VALUE is set to -999;, so your if line expands the macro to:

if (test==-999;)

which is invalid C syntax.

这篇关于为什么的#define INVALID_VALUE -999;使用时,给出一个语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆