initialization - Declaring and assigning variables with file scope in C -


say declare following variable:

int num; num = 0;  int main(void) {         /* ... */         exit(exit_success); } 

the compiler complain num being undeclared , default type int. not happen when in 1 step:

int num = 0; 

or if move assignment main():

int main(void) {         num = 0;         /* ... */         exit(exit_success); }     

i once read explanation behavior cannot find anymore. update me again.

i'm compiling

gcc -std=c11 -o2 -g -pedantic -wall -c -fmessage-length=0 -v 

num = 0; statement can exist inside function. cannot exist in global scope.

if put statement outside function, it's wrong , not allowed. think like, if have statement outside functions, in global scope, when , how statement can executed? so, that's wrong.

a special case, initialization while defining allowed in form of int num = 0;


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -