auto int a=5;
#include<stdio.h>
int main ()
{
printf("%d",a);
return 0;
}
我读到自动变量的作用域在指定的块内。在上面的程序中,因为自动变量'a‘是在main函数外部声明的,所以它应该被分配到全局作用域,并且可以在main函数中访问。但是,似乎有一个错误。
发布于 2014-01-06 21:56:00
顶级变量不能为auto
。它们应该声明为static
、extern
(在别处定义)或全局(没有关键字)。
这不会编译。
$ echo "auto int c;" > test.c
$ gcc -Wall -c test.c
test.c:1:10: error: file-scope declaration of ‘a’ specifies ‘auto’
发布于 2014-01-06 21:56:14
在C中,全局作用域auto
变量是不允许的。根据定义,它们是函数局部变量。
https://stackoverflow.com/questions/20951440
复制相似问题