<分区>
如果我有一个函数:
void G(int f){
printf("Hello");
}
(我知道这个函数有参数没有意义,但我有它只是为了问题)
即使“int f”没有定义也没有在函数中使用,它在函数内部是否可见?
标签 c
<分区>
如果我有一个函数:
void G(int f){
printf("Hello");
}
(我知道这个函数有参数没有意义,但我有它只是为了问题)
即使“int f”没有定义也没有在函数中使用,它在函数内部是否可见?
最佳答案
even though it has no definition
不太对。它实际上确实有一个定义,它就在函数声明中。您是否使用是完全不同的事情。
尽管如此,您使用的术语“可见性”对初学者来说还可以,但语言规范(以及像我这样使用它的语言律师)更喜欢术语范围。
变量的范围包含在(对于 C11)6.2.1 标识符的范围
中。该部分的节 /4
指出(我的重点):
If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block.
这意味着该变量一直“可见”到函数 block 的末尾(假设您没有通过在内部作用域中引入另一个同名变量来隐藏它)。
关于c - C 中的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740208/