sandyloggs
sandyloggs

Reputation: 3

why the output of this program comes only by using extern ? Why not without it?

#include<stdio.h>  
int main ()
{

   printf("%d\n",z);
   return 0;

}  
int z=25;

why is output to this code is showing an error ?

Upvotes: 0

Views: 14

Answers (2)

user5950614
user5950614

Reputation:

Try this :

#include<stdio.h>  
int z=25;

int main ()
{

   printf("%d\n",z);
   return 0;

}  

Upvotes: 0

Derlin
Derlin

Reputation: 9871

The order in which you declare your functions/variables count in C. In your code, when the compiler parses your code, it encounters the symbol z, which has not yet been declared.

So, you need to put your int z = ... before the first time you use z, hence before main.

The extern keyword tells the compiler that the variable has been declared in another file, so it will be resolved during linking, i.e. when all the files are assembled into a program. So for the compilation of this file, the unresolved symbol z can be ignored => no compilation error.

Upvotes: 1

Related Questions