c - C 中的可见性

标签 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/

相关文章:

c - 为什么 a+++++b 不起作用?

c - 如何将一个文本文件一点点分割成两个文件

c - 空指针安全吗?

c - 返回到循环中 if 语句的开头

c - 如何声明指向文件的动态指针数组

c++ - C/C++中的位操作

c - strstr() 在同一行中搜索两个不同的字符串

C:不兼容的类型?

c - C中的主要函数概念

c - C中的Mastermind评分算法