auto int a=5;
#include<stdio.h>
int main ()
{
printf("%d",a);
return 0;
}
I have read that the scope of automatic variables is within the specified block . In the above program, since the auto variable 'a' is declared outside main function, it should be assigned global scope and be accesible within the main . But, there seems to be an error .
Variables at top-level cannot be auto. They should be either declared static, extern (definition elsewhere) or global (no keyword for that).
This won't compile.
$ echo "auto int c;" > test.c
$ gcc -Wall -c test.c
test.c:1:10: error: file-scope declaration of ‘a’ specifies ‘auto’
In C, global scope auto variables are not allowed. Per definition they are function-local variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With