宏名称的有效字符是什么?

发布于 2024-07-10 13:33:03 字数 492 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

装迷糊 2024-07-17 13:33:03

宏名称只能包含字母数字字符和下划线,即 'az''AZ''0-9'>'_',并且第一个字符不能是数字。 某些预处理器还允许使用美元符号 '$',但您不应该使用它; 不幸的是我不能引用 C 标准,因为我没有它的副本。

来自 GCC 文档

预处理标记分​​为五种
广泛的类别:标识符,
预处理数字、字符串
文字、标点符号等。 一个
标识符
C 中的标识符:任意序列
字母、数字或下划线,其中
以字母或下划线开头。
C 的关键字没有意义
预处理器; 他们很普通
身份标识。 你可以定义一个宏
例如,其名称是关键字。
唯一可以的标识符
认为预处理关键字是
已定义。 请参阅定义。

对于其他语言来说也是如此
它使用 C 预处理器。 然而,
C++ 的一些关键字是
即使在预处理器中也很重要。
请参阅 C++ 命名运算符。

在 1999 C 标准中,标识符
可能包含不属于部分的字母
“基本源字符集”,
由实施者自行决定
(例如带重音的拉丁字母、希腊字母
字母或汉字)。 这
可以用扩展字符来完成
设置,或 '\u''\U' 转义
序列。 本条的实施
GCC 中的功能尚处于实验阶段; 这样的
字符仅在
'\u''\U' 形式且仅当
使用-fextended-identifiers

作为扩展,GCC 将 '$' 视为
信。 这是为了兼容
某些系统,例如 VMS,其中 '$'
常用于系统定义的
函数和对象名称。 '$' 不是
一封严格符合模式的信件,
或者如果您指定 -$ 选项。 看
调用。

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:

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.

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.

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.

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.

梦在深巷 2024-07-17 13:33:03

clang 允许使用很多“疯狂”的字符..尽管我一直在努力寻找任何很多押韵或理由 - 至于为什么 有些是允许的,而另一些则不允许。 例如......

#define 

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..

#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™️.

各空 2024-07-17 13:33:03

你是对的,就名称而言,相同的规则适用于宏和标识符:有效字符是 [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.

岁月静好 2024-07-17 13:33:03

为变量名称指定有效标识符的相同规则也适用于宏名称,但宏可能与关键字具有相同的名称。 标识符名称中的有效字符包括数字非数字,并且不得以数字开头。 非数字包括大写字母 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文