错误:宏名称必须是使用#ifdef 0的标识符

时间:2009-01-09 01:27:31

标签: c++ macros c-preprocessor

我有用C ++编写的应用程序的源代码,我只想用以下内容评论:

#ifdef 0
...
#endif

我收到此错误

  

错误:宏名称必须是标识符

为什么会这样?

5 个答案:

答案 0 :(得分:60)

#ifdef指令用于检查是否定义了预处理程序符号。标准(C11 6.4.2 Identifiers)强制标识符不能以数字开头:

identifier:
    identifier-nondigit
    identifier identifier-nondigit
    identifier digit
identifier-nondigit:
    nondigit
    universal-character-name
    other implementation-defined characters>
nondigit: one of
    _ a b c d e f g h i j k l m
    n o p q r s t u v w x y z
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
digit: one of
    0 1 2 3 4 5 6 7 8 9

使用预处理器阻止代码的正确形式是:

#if 0
: : :
#endif

您也可以使用:

#ifdef NO_CHANCE_THAT_THIS_SYMBOL_WILL_EVER_EXIST
: : :
#endif

但是您需要确信符号会被您自己以外的代码无意中设置。换句话说,请勿使用其他人也可能使用的NOTUSEDDONOTCOMPILE之类的内容。为安全起见,应首选#if选项。

答案 1 :(得分:13)

使用以下方法评估表达式(常量0的计算结果为false)。

#if 0
 ...
#endif

答案 2 :(得分:5)

如果您不遵守marco规则,也会发生此错误

#define 1K 1024 // Macro rules must be identifiers error occurs

原因:宏应以字母开头,而不是数字

更改为

#define ONE_KILOBYTE 1024 // This resolves 

答案 3 :(得分:2)

#ifdef 0
...
#endif

#ifdef期待一个宏而不是表达式 当使用常量或表达式时

#if 0
...
#endif

#if !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

如果使用#ifdef则会报告此错误

#ifdef !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

#ifdef期望宏而不是宏表达

答案 4 :(得分:1)

请注意,如果您不小心输入了以下内容,也可以点击此错误:

#define <stdio.h>

......而不是......

#include <stdio.>
相关问题