title: Redeclaration Error in C++ when using extern
body: <p>So, AFAIK you can declare a name in C as many times as you want, but you cannot redefine a name more than once. Also according to what I think, a declaration is when a name is introduced. It is when, say, a compiler would add that name to the symbol table. A definition is when memory is allocated for a name. Now, here the name <strong>p</strong> is being declared again. It is not being defined again. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  float goo(float x)
  {
      int p = 4;
      extern int p;
      cout &lt;&lt; p &lt;&lt; endl;
      return floor(x) + ceil(x) / 2;
  }
  int p = 88;
  </code></pre>
  
  <p>But, I get the following error:</p>
  
  <pre><code>iter.cpp: In function ‘float goo(float)’:
  iter.cpp:53:16: error: redeclaration of ‘int p’
       extern int p;
                  ^
  iter.cpp:52:9: note: previous declaration ‘int p’
       int p = 4;
  </code></pre>
  
  <p>According to me, <code>int p = 4;</code> should allocate memory for p on the call stack i.e. introduce a new local variable. Then, extern int p should declare p again. Now p should refer to the global variable p and this p should be used in all subsequent statements in the function goo.</p>