1. 规则
1.1 原文
Rule 5.3 An identifier declared in an inner scope shall not hide an identifier
declared in an outer scope
Category Required
Analysis Decidable, Single Translation Unit
Applies to C90, C99
1.2 分类
规则4.2:在同一作用域和名称空间中声明的标识符应该是不同的
Required必须类规范。
2. 关键描述
在内部作用域中声明的标识符应与在外部作用域中声明的任何标识符不同。
distinct的定义取决于它的实现和C语言的版本使用:
•在C90中,最低要求是前31个字符是有效的;
•在C99中,最低要求是前63个字符都是有效的
作为单个字符计数的通用字符或扩展源字符。
如果一个标识符是在内部作用域中声明的,但与外部作用域中已经存在的标识符没有区别,那么最内部的声明将“隐藏”外部的标识符。这可能会导致开发人员的困惑。
注意:在一个名称空间中声明的标识符不会隐藏在另一个名称空间中声明的标识符。
术语外部作用域和内部作用域的定义如下:
具有文件作用域的标识符可以被认为具有最外层作用域;
具有块作用域的标识符具有更内部的作用域;
•连续的嵌套块,引入更多的内部作用域。
3. 代码实例
例1,
void function1( void )
{
int16_t i; /* Declare an object "i" */
{
int16_t i; /* Non-compliant - hides previous "i " */
i = 3; /* Could be confusing as to which "i" this refers */
}
}
struct astruct {
int16_t m;
};
extern void function_test( struct astruct *p );
int16_t xyz = 0; /* Declare an object "xyz" */
/* Non-compliant - outer "xyz" is now hidden by parameter name */
void function2( struct astruct xyz )
{
function_test(&xyz);
}
uint16_t speed;
void function3(void)
{
typedef float32_t speed; /* Non-compliant - type hides object */
}