1. 规则
1.1 原文
Rule 5.2 Identifiers declared in the same scope and name space shall be distinct
Category Required
Analysis Decidable, Single Translation Unit
Applies to C90, C99
1.2 分类
规则4.2:在同一作用域和名称空间中声明的标识符应该是不同的
Required必须类规范。
2. 关键描述
如果两个标识符都是外部标识符,则此规则不适用,因为这种情况由规则5.1涵盖。
如果其中一个标识符是宏标识符,则不适用此规则,因为这种情况由规则5.4和规则5.5涵盖。
distinct的定义取决于正在使用的C语言的实现和版本:
•在C90中,最低要求是前31个字符是有效的;
•在C99中,最低要求是前63个字符都是有效的
作为单个字符计数的通用字符或扩展源字符。
如果两个标识符仅在非重要字符上不同,则行为未定义。如果考虑可移植性,则使用标准中指定的最小限制来应用此规则将是谨慎的。长标识符可能会损害代码的可读性。虽然许多自动代码生成系统会生成较长的标识符,但将标识符长度保持在远低于此限制的水平是有充分理由的
3. 代码实例
例1,在下面的示例中,所讨论的实现在没有外部链接的标识符中支持31个重要的区分大小写的字符。
标识符engine_expirst_gas_temperature_local符合此规则。尽管它与标识符engine_exhaust_gas_temperature_raw没有区别,但它处于不同的作用域。但是,它不符合规则5.3
/* 1234567890123456789012345678901********* Characters */
extern int32_t engine_exhaust_gas_temperature_raw;
static int32_t engine_exhaust_gas_temperature_scaled; /* Non-compliant */
void f ( void )
{
/* 1234567890123456789012345678901********* Characters */
int32_t engine_exhaust_gas_temperature_local; /* Compliant */
}
/* 1234567890123456789012345678901********* Characters */
static int32_t engine_exhaust_gas_temp_raw;
static int32_t engine_exhaust_gas_temp_scaled; /* Compliant */