gpt4 book ai didi

C 标准一致性 - 标识符

转载 作者:行者123 更新时间:2023-12-01 16:26:28 28 4
gpt4 key购买 nike

我正在寻找有关 C 标准(C99 和/或 C11)部分内容的一些说明,主要是关于标识符的使用。
上下文是一个完整的C99标准库的实现,我希望它完全符合标准。

基本问题是:C 标准允许我在多大程度上声明标准中未列出的标识符/符号?

作为示例,让我们考虑 math.h 中的 isfinite 宏。
一个可能的实现可能是:

#define isinf( _x_ )                                                       \
( \
( sizeof( _x_ ) == sizeof( float ) ) ? _c99_math_isinf_f( _x_ ) : \
( sizeof( _x_ ) == sizeof( double ) ) ? _c99_math_isinf_d( _x_ ) : \
_c99_math_isinf_l( _x_ ) \
)

int _c99_math_isinf_f( float x );
int _c99_math_isinf_d( double x );
int _c99_math_isinf_l( long double x );

在这里,我需要声明附加标识符,这些标识符显然不属于C标准的一部分

在C99标准的第4节的注释3(一致性)中,我们可以读到:

This implies that a conforming implementation reserves no identifiers other than those explicitly reserved in this International Standard.

我不太明白。
这是否意味着我不能声明其他标识符?

假设情况并非如此,并且我可以为自己的实现声明其他标识符,我应该遵循什么命名规则,考虑到这些标识符不应在除宏扩展,就像上面的例子吗?

在 C99 标准的第 7.1.3 节(保留标识符)中,我们可以读到:

  1. All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  2. All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name.

好吧,我不会声明一个带有前导双下划线的标识符,也不会声明一个前导单下划线后跟一个大写字母的标识符。
但是,仍然考虑我上面的例子,第二条规则又如何呢?

最佳答案

保留标识符规则的要点是这些标识符是为实现而保留的。既然您正在编写(部分)实现而不是“最终用户程序”,您应该在保留的 namespace 中命名您的标识符,这样您的标识符就不会意外地与(最终)结束的标识符发生冲突用户。

作为此规则实际应用的示例,如果您使用的是 Linux,请使用“readelf -s”检查 glibc 或 libgcc 等符号列表。您会发现大量带有前导双下划线或前导下划线后跟大写字母的符号。

关于C 标准一致性 - 标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23876275/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com