宏名称的有效字符是什么?
C 风格宏名称是否遵循与标识符相同的命名规则? 编译器升级后,它现在针对遗留应用程序发出此警告:
warning #3649-D: white space is required between the macro name "CHAR_" and its replacement text
#define CHAR_& 38
此行代码正在为 & 符号定义 ASCII 值常量。
#define DOL_SN 36
#define PERCENT 37
#define CHAR_& 38
#define RT_SING 39
#define LF_PAR 40
我认为这个定义(据我所知,实际上没有被任何代码引用)是有缺陷的,应该更改为“CHAR_AMPERSAND”之类的东西?
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:
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.
#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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
宏名称只能包含字母数字字符和下划线,即
'az'
、'AZ'
、'0-9'
和>'_'
,并且第一个字符不能是数字。 某些预处理器还允许使用美元符号'$'
,但您不应该使用它; 不幸的是我不能引用 C 标准,因为我没有它的副本。来自 GCC 文档:
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.From the GCC documentation:
clang
允许使用很多“疯狂”的字符..尽管我一直在努力寻找任何很多押韵或理由 - 至于为什么 有些是允许的,而另一些则不允许。 例如......clang
allows a lot of "crazy" characters.. although I have struggled to findanymuch rhyme or reason - as to why some are allowed, and others are not. For example..yet
Who knows. I'd love to... but Google has thus failed me, so far. Any insight on the subject, would be appreciated™️.
你是对的,就名称而言,相同的规则适用于宏和标识符:有效字符是 [A-Za-z0-9_]。
使用大写名称来区分宏与其他标识符(变量和函数名称)是常见的用法。
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_].
It's common usage to use CAPITALIZED names to differentiate macros from other identifiers - variables and function name.
为变量名称指定有效标识符的相同规则也适用于宏名称,但宏可能与关键字具有相同的名称。 标识符名称中的有效字符包括
数字
和非数字
,并且不得以数字开头。非数字
包括大写字母 AZ、小写字母 az、下划线和任何实现定义的字符。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
andnon-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.