所以,让我们说,我有:
file1.c
int i;
static int j;
int main ()
{
for ( int k = 0; k < 10; k++ )
{
int foo = k;
}
}
Run Code Online (Sandbox Code Playgroud)
file2.c
{
// the following statements are before main.
extern int i; // this is acceptable, I know since i acts as a global variable in the other file
extern int j; // Will this be valid?
extern int foo; // Will this be valid as well?
}
Run Code Online (Sandbox Code Playgroud)
因此,我怀疑带有问号的陈述是否有效?
没有!static全局变量有文件范围(内部链接),所以你不能使用它们,因为它们有外部链接...这并不意味着你不能拥有一个具有外部链接的同名变量,但它不能是那个static.
正确的i.
不正确的j,至少它不能是在中定义的那个file1.c.
不正确foo,至少对于使用的局部变量file2.c没有外部链接(根本没有链接).局部变量仅在声明它的块被激活时才存在,因此在外部访问它是无意义的.