3
    #include <stdio.h>

    int i;

    int main()

    {

        extern int i;

        if (i == 0)

            printf("scope rules\n");

    }

Output: scope rules

How extern variable works here?

Why there is no error like Compile time error due to multiple declaration

Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26
  • 2
    [Click me](http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/). – Maroun Feb 03 '14 at 13:53
  • please see @Jonathan Leffler is answer for a better explanation about extern variables http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how – yat0 Feb 03 '14 at 14:23

2 Answers2

9

extern doesn't actually create the variable though. It is like the forward declaration of a class, or the prototype of a function. Variable "i" at the starting creates a global integer named "i" which will exist in the current compilation unit, whereas "i" under the "int main" is a declaration that an integer named "i" exists somewhere in some compilation unit, and any uses of the name "i" refer to that variable.

Shubham Abrol
  • 613
  • 6
  • 10
5

Because you can declare that something exists as many times as you want to (provided the type is the same each time), but you can only define it once.

extern int i is a declaration that i exists, and is an int.

Where it exists is at the file level (the int i after the headers), with static storage duration. That means it's initialised to zero so you will always see the output "scope rules".

It's a subtle concept, the declaration/definition distinction, but it's one every C programmer should eventually learn well.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • @Eric, is that not covered by my "provided the type is the same each time" comment? I'm open to suggestions on how to make it clearer. Perhaps by clarifying the definition of "something" in the first sentence to state that name _and_ type are important. – paxdiablo Feb 03 '14 at 14:32
  • I do not think there is a way to clarify this. I have deleted my comment because the C standard refers, in several places, to an identifier in a way that means the identifier is just the string of characters that make it up; it is the same identifier whether it is at file scope or block scope. But the standard is not consistent. At C 2011 6.2.1 4, it says “Every other identifier has scope determined by the placement of its declaration…”. But clearly the `i` in this example does not have just one scope. It is a declaration that has scope; the identifier has independent existence. – Eric Postpischil Feb 03 '14 at 15:01
  • Then there is C 2011 6.2.1 5, which says “Unless explicitly stated otherwise, where this International Standard uses the term “identifier” to refer to some entity (as opposed to the syntactic construct), it refers to the entity in the relevant name space whose declaration is visible at the point the identifier occurs.” So maybe I should not have deleted my comment. Also, per 6.2.7 4, the declaration does not have to specify the same type each time; a declaration can provide additional information, and the type of the identifier becomes a composite of two declarations. – Eric Postpischil Feb 03 '14 at 15:08
  • So, this is all very complicated, oh, well. – Eric Postpischil Feb 03 '14 at 15:09