#if 0
#include <stdio.h>
 
extern int a;     // int var;  ->  declaration and defination
                  // extern int var;  -> declaration
                  
                  /*
                  Here, an integer type variable called var has been declared
                  (it hasn’t been defined yet, 
                  so no memory allocation for var so far). 
                  And we can do this declaration as many times as we want.
                  So far so good.
                  */
 
int main()
{ 
    printf("%d", a);
  
   return 0;
}

//output is error; //if a = 0 defined the memeory is allocated

#endif

#include <stdio.h>
extern int var;// if var = 0 then defined and the value will be 10;
int main()
{
    var = 10;
    printf("%d ", var);
    return 0;
}

//output = error

/*
m sure this post will be as interesting and informative to C virgins (i.e. beginners) as it will be to those who are well-versed in C. So let me start by saying that the extern keyword applies to C variables (data objects) and C functions. Basically, the extern keyword extends the visibility of the C variables and C functions. That’s probably the reason why it was named exough most people probably understand the difference between the “declaration” and the “definition” of a variable or function, for the sake of completeness, I would like to cl
Declaration of a variable or function simply declares that the variable or function exists somewhere in the program, but the memory is not allocated for them. The declaration of a variable or function serves an important role–it tells the program what its type is going to be. In case of function declarations, it also tells the program the arguments, their data types, the order of those arguments, and the return type of the function. So that’s all about the declaration.
Coming to the definition, when we define a variable or function, in addition to everything that a declaration does, it also allocates memory for that variable or function. Therefore, we can think of definition as a superset of the declaration (or declaration as a subset of definition).
Extern is a short name for external.
used when a particular files need to access a variable from another file.
*/

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: