- 1. 規則
- 1.1 原文
- 1.2 分類
- 2. 關鍵描述
- 3. 代碼實例
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 */