我有一个用C++编写的应用程序的源代码,我想用以下方式进行注释:
#ifdef 0
...
#endif
我得到了这个错误:
错误:宏名称必须是标识符
为什么会发生这种情况?
我有一个用C++编写的应用程序的源代码,我想用以下方式进行注释:
#ifdef 0
...
#endif
我得到了这个错误:
错误:宏名称必须是标识符
为什么会发生这种情况?
#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
但是你需要有信心,这些符号不会被除你之外的其他代码无意中设置。换句话说,不要使用像NOTUSED
或DONOTCOMPILE
这样的东西,因为其他人也可能使用。为了安全起见,应该优先选择#if
选项。
使用以下方法来评估表达式(常量0会被评估为false)。
#if 0
...
#endif
如果您没有遵循宏规则,也可能出现此错误。
例如:
#define 1K 1024 // Macro rules must be identifiers error occurs
原因:宏应该以字母开头,而不是数字。
更改为
#define ONE_KILOBYTE 1024 // This resolves
#ifdef 0
...
#endif
#ifdef 期望使用宏而不是常量或表达式
#if 0
...
#endif
#if !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif
#ifdef !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif
#ifdef 期望的是宏定义而不是宏表达式
#define <stdio.h>
...代替...
#include <stdio.>