简体   繁体   English

C标准符合性-标识符

[英]C Standard Conformance - Identifiers

I'm looking for some clarifications on parts of the C Standard (C99 and/or C11), mainly on the use of identifiers . 我正在寻找一些有关C标准(C99和/或C11)的说明,主要是关于标识符的使用。
The context is the implementation of a complete C99 standard library, which I want it to be fully compliant with the standard. 上下文是完整C99标准库的实现,我希望它完全符合该标准。

The basic question is: to what extend am I allowed by the C standard to declare identifiers/symbols not listed in the standard? 基本问题是: C标准允许我在多大程度上声明未在标准中列出的标识符/符号?

As an example, let's consider the isfinite macro from math.h . 例如,让我们考虑math.hisfinite宏。
A possible implementation might be: 可能的实现方式可能是:

#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 );

Here I need to declare additional identifiers that are obviously not part of the C standard. 在这里,我需要声明显然不属于 C标准的其他标识符

In note 3 of section 4 of the C99 standard ( Conformance ), we can read: 在C99标准( 一致性第4部分的注释3中,我们可以阅读:

This implies that a conforming implementation reserves no identifiers other than those explicitly reserved in this International Standard. 这意味着除本国际标准中明确保留的标识符外,符合标准的实现不保留任何标识符。

I'm not sure to understand it. 我不确定是否了解。
Does it mean I'm not allowed to declare additional identifiers? 这是否意味着我不允许声明其他标识符?

Assuming that it's not the case, and that I am allowed to declare other identifiers for my own implementation, what naming rule should I follow , considering those identifiers are not meant to be used other than in a macro expansion, like the above example? 假设情况并非如此,并且允许我为自己的实现声明其他标识符,那么我应该遵循什么命名规则 ,考虑到这些标识符除了上面的示例中的宏扩展之外,不打算使用?

In section 7.1.3 of the C99 standard ( Reserved identifiers ), we can read: 在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. 始终保留所有以下划线开头的标识符,以用作普通名称和标记名称中具有文件范围的标识符。

Ok, I shall not declare an identifier with leading double underscore, nor a leading single underscore followed by an uppercase letter. 好的,我不会声明带有双引号和双引号的标识符,也不能带单引号和大写字母。
But what about the second rule, still considering my above example? 但是第二条规则又如何考虑我上面的示例呢?

The point of the reserved identifiers rule is that those identifiers are reserved for the implementation. 保留标识符规则的重点是为实施保留了这些标识符。 Now that you are writing (part of) the implementation rather than an "end user program", you should name your identifiers in the reserved name space, exactly so that your identifiers don't accidentally collide with the identifiers of the (eventual) end user. 现在,您正在编写实现的一部分(而不是“最终用户程序”),您应该在保留的名称空间中命名标识符,以确保标识符不会意外地与(最终)端的标识符发生冲突。用户。

As an example of this rule in action, if you're on Linux, use "readelf -s" to check the symbol lists of, say, glibc or libgcc. 作为运行中该规则的示例,如果您使用的是Linux,请使用“ readelf -s”检查glibc或libgcc的符号列表。 You'll find plenty of symbols with leading double underscores, or leading underscore followed by a capital letter. 您会发现大量带有双下划线的符号,或带下划线的大写字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM