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

Edu*_*rdo 36 c++ macros c-preprocessor

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

#ifdef 0
...
#endif
Run Code Online (Sandbox Code Playgroud)

我得到了这个错误

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

为什么会这样?

pax*_*blo 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
Run Code Online (Sandbox Code Playgroud)

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

#if 0
: : :
#endif
Run Code Online (Sandbox Code Playgroud)

您还可以使用:

#ifdef NO_CHANCE_THAT_THIS_SYMBOL_WILL_EVER_EXIST
: : :
#endif
Run Code Online (Sandbox Code Playgroud)

但你需要确信这些符号不会被你自己以外的代码无意中设置.换句话说,不要使用类似NOTUSEDDONOTCOMPILE其他人也可能使用的东西.为安全起见,#if应该首选该选项.


tva*_*son 13

使用以下命令计算表达式(常量0的计算结果为false).

#if 0
 ...
#endif
Run Code Online (Sandbox Code Playgroud)


Foo*_*ooo 5

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

喜欢

#define 1K 1024 // Macro rules must be identifiers error occurs
Run Code Online (Sandbox Code Playgroud)

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

改成

#define ONE_KILOBYTE 1024 // This resolves 
Run Code Online (Sandbox Code Playgroud)