Are C-style macro names subject to the same naming rules as identifiers? After a compiler upgrade, it is now emitting this warning for a legacy application:
c样式的宏名称是否与标识符相同?在一次编译器升级之后,它现在为一个遗留应用程序发出这个警告:
warning #3649-D: white space is required between the macro name "CHAR_" and its replacement text
#define CHAR_& 38
This line of code is defining an ASCII value constant for an ampersand.
这一行代码为&符号定义了ASCII值常量。
#define DOL_SN 36
#define PERCENT 37
#define CHAR_& 38
#define RT_SING 39
#define LF_PAR 40
I assume that this definition (not actually referenced by any code, as far as I can tell) is buggy and should be changed to something like "CHAR_AMPERSAND"?
我假设这个定义(实际上没有被任何代码引用)是有bug的,应该改成“CHAR_AMPERSAND”吗?
4 个解决方案
#1
32
Macro names should only consist of alphanumeric characters and underscores, i.e. 'a-z'
, 'A-Z'
, '0-9'
, and '_'
, and the first character should not be a digit. Some preprocessors also permit the dollar sign character '$'
, but you shouldn't use it; unfortunately I can't quote the C standard since I don't have a copy of it.
宏名应该只包含字母数字字符和下划线,即。“a-z”、“a-z”、“0-9”和“_”,第一个字符不应该是数字。一些预处理器也允许美元符号“$”,但您不应该使用它;不幸的是,我不能引用C标准,因为我没有它的副本。
From the GCC documentation:
GCC的文档:
Preprocessing tokens fall into five broad classes: identifiers, preprocessing numbers, string literals, punctuators, and other. An identifier is the same as an identifier in C: any sequence of letters, digits, or underscores, which begins with a letter or underscore. Keywords of C have no significance to the preprocessor; they are ordinary identifiers. You can define a macro whose name is a keyword, for instance. The only identifier which can be considered a preprocessing keyword is
defined
. See Defined.预处理令牌可分为五个大类:标识符、预处理数字、字符串文本、标点符号和其他。标识符与C中的标识符相同:任何以字母或下划线开头的字母、数字或下划线序列。C的关键字对预处理器没有意义;他们是普通标识符。例如,您可以定义一个名称为关键字的宏。定义了唯一可视为预处理关键字的标识符。看到定义。
This is mostly true of other languages which use the C preprocessor. However, a few of the keywords of C++ are significant even in the preprocessor. See C++ Named Operators.
这主要适用于使用C预处理器的其他语言。然而,即使在预处理器中,c++的一些关键字也很重要。看到c++命名的运营商。
In the 1999 C standard, identifiers may contain letters which are not part of the “basic source character set”, at the implementation's discretion (such as accented Latin letters, Greek letters, or Chinese ideograms). This may be done with an extended character set, or the
'\u'
and'\U'
escape sequences. The implementation of this feature in GCC is experimental; such characters are only accepted in the'\u'
and'\U'
forms and only if-fextended-identifiers
is used.在1999年的C标准中,标识符可能包含不属于“基本源字符集”的字母,在实现的*决定中(例如重读的拉丁字母、希腊字母或中国的表意文字)。这可以通过扩展字符集或'\u'和'\u'转义序列来实现。该特性在GCC中的实现是实验性的;这些字符只在“\u”和“\u”形式中被接受,并且只有在使用- fextension标识符时才被接受。
As an extension, GCC treats
'$'
as a letter. This is for compatibility with some systems, such as VMS, where'$'
is commonly used in system-defined function and object names.'$'
is not a letter in strictly conforming mode, or if you specify the-$
option. See Invocation.作为扩展,GCC将“$”视为字母。这是为了与某些系统的兼容性,比如VMS,在系统定义的函数和对象名称中通常使用“$”。“$”不是严格遵循模式的字母,也不是你指定的-$选项。看到调用。
#2
8
clang
allows a lot of "crazy" characters.. although I have struggled to find
any
much rhyme or reason - as to why some are allowed, and others are not. For example..
clang允许很多“疯狂”的角色。尽管我一直在努力寻找任何韵律或理由——为什么有些是允许的,有些不是。例如. .
#define ???? ?: /// WORKS FINE
#define ■ @end /// WORKS FINE
#define ???? @interface /// WORKS FINE
#define P @protocol /// WORKS FINE
yet
然而,
#define ☎ TEL /// ERROR: Macro name must be an identifier.
#define ❌ NO /// ERROR: Macro name must be an identifier.
#define ⇧ UP /// ERROR: Macro name must be an identifier.
#define 〓 == /// ERROR: Macro name must be an identifier.
#define ???? APPLE /// ERROR: Macro name must be an identifier.
Who knows. I'd love to... but Google has thus failed me, so far. Any insight on the subject, would be appreciated™️.
谁知道呢。我很想……但到目前为止,谷歌已经让我失望了。任何见解,将感激™️。
#3
7
You're right, the same rules apply to macro and identifiers as far as the names are concerned: valid characters are [A-Za-z0-9_].
您是对的,同样的规则适用于宏和标识符的名称:有效字符是[A-Za-z0-9_]。
It's common usage to use CAPITALIZED names to differentiate macros from other identifiers - variables and function name.
通常使用大写名称来区分宏和其他标识符——变量和函数名。
#4
1
The same rules that specify valid identifiers for variable names apply to macro names with the exception that macros may have the same names as keywords. Valid characters in identifier names include digits
and non-digits
and must not start with a digit. non-digits
include the uppercase letters A-Z, the lowercase letters a-z, the underscore, and any implementation defined characters.
为变量名指定有效标识符的规则适用于宏名,但是宏可能与关键字具有相同的名称。标识符名称中的有效字符包括数字和非数字,不能以数字开头。非数字包括大写字母A-Z、小写字母A-Z、下划线和任何实现定义的字符。
#1
32
Macro names should only consist of alphanumeric characters and underscores, i.e. 'a-z'
, 'A-Z'
, '0-9'
, and '_'
, and the first character should not be a digit. Some preprocessors also permit the dollar sign character '$'
, but you shouldn't use it; unfortunately I can't quote the C standard since I don't have a copy of it.
宏名应该只包含字母数字字符和下划线,即。“a-z”、“a-z”、“0-9”和“_”,第一个字符不应该是数字。一些预处理器也允许美元符号“$”,但您不应该使用它;不幸的是,我不能引用C标准,因为我没有它的副本。
From the GCC documentation:
GCC的文档:
Preprocessing tokens fall into five broad classes: identifiers, preprocessing numbers, string literals, punctuators, and other. An identifier is the same as an identifier in C: any sequence of letters, digits, or underscores, which begins with a letter or underscore. Keywords of C have no significance to the preprocessor; they are ordinary identifiers. You can define a macro whose name is a keyword, for instance. The only identifier which can be considered a preprocessing keyword is
defined
. See Defined.预处理令牌可分为五个大类:标识符、预处理数字、字符串文本、标点符号和其他。标识符与C中的标识符相同:任何以字母或下划线开头的字母、数字或下划线序列。C的关键字对预处理器没有意义;他们是普通标识符。例如,您可以定义一个名称为关键字的宏。定义了唯一可视为预处理关键字的标识符。看到定义。
This is mostly true of other languages which use the C preprocessor. However, a few of the keywords of C++ are significant even in the preprocessor. See C++ Named Operators.
这主要适用于使用C预处理器的其他语言。然而,即使在预处理器中,c++的一些关键字也很重要。看到c++命名的运营商。
In the 1999 C standard, identifiers may contain letters which are not part of the “basic source character set”, at the implementation's discretion (such as accented Latin letters, Greek letters, or Chinese ideograms). This may be done with an extended character set, or the
'\u'
and'\U'
escape sequences. The implementation of this feature in GCC is experimental; such characters are only accepted in the'\u'
and'\U'
forms and only if-fextended-identifiers
is used.在1999年的C标准中,标识符可能包含不属于“基本源字符集”的字母,在实现的*决定中(例如重读的拉丁字母、希腊字母或中国的表意文字)。这可以通过扩展字符集或'\u'和'\u'转义序列来实现。该特性在GCC中的实现是实验性的;这些字符只在“\u”和“\u”形式中被接受,并且只有在使用- fextension标识符时才被接受。
As an extension, GCC treats
'$'
as a letter. This is for compatibility with some systems, such as VMS, where'$'
is commonly used in system-defined function and object names.'$'
is not a letter in strictly conforming mode, or if you specify the-$
option. See Invocation.作为扩展,GCC将“$”视为字母。这是为了与某些系统的兼容性,比如VMS,在系统定义的函数和对象名称中通常使用“$”。“$”不是严格遵循模式的字母,也不是你指定的-$选项。看到调用。
#2
8
clang
allows a lot of "crazy" characters.. although I have struggled to find
any
much rhyme or reason - as to why some are allowed, and others are not. For example..
clang允许很多“疯狂”的角色。尽管我一直在努力寻找任何韵律或理由——为什么有些是允许的,有些不是。例如. .
#define ???? ?: /// WORKS FINE
#define ■ @end /// WORKS FINE
#define ???? @interface /// WORKS FINE
#define P @protocol /// WORKS FINE
yet
然而,
#define ☎ TEL /// ERROR: Macro name must be an identifier.
#define ❌ NO /// ERROR: Macro name must be an identifier.
#define ⇧ UP /// ERROR: Macro name must be an identifier.
#define 〓 == /// ERROR: Macro name must be an identifier.
#define ???? APPLE /// ERROR: Macro name must be an identifier.
Who knows. I'd love to... but Google has thus failed me, so far. Any insight on the subject, would be appreciated™️.
谁知道呢。我很想……但到目前为止,谷歌已经让我失望了。任何见解,将感激™️。
#3
7
You're right, the same rules apply to macro and identifiers as far as the names are concerned: valid characters are [A-Za-z0-9_].
您是对的,同样的规则适用于宏和标识符的名称:有效字符是[A-Za-z0-9_]。
It's common usage to use CAPITALIZED names to differentiate macros from other identifiers - variables and function name.
通常使用大写名称来区分宏和其他标识符——变量和函数名。
#4
1
The same rules that specify valid identifiers for variable names apply to macro names with the exception that macros may have the same names as keywords. Valid characters in identifier names include digits
and non-digits
and must not start with a digit. non-digits
include the uppercase letters A-Z, the lowercase letters a-z, the underscore, and any implementation defined characters.
为变量名指定有效标识符的规则适用于宏名,但是宏可能与关键字具有相同的名称。标识符名称中的有效字符包括数字和非数字,不能以数字开头。非数字包括大写字母A-Z、小写字母A-Z、下划线和任何实现定义的字符。