Note that there are some explanatory texts on larger screens.

plurals
  1. PO"extern" inside a function?
    primarykey
    data
    text
    <p>Well, reading "a bit old" book ("The C programming language", second edition, by Dennis Ritchie), I came a cross the following:</p> <blockquote> <p>An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it</p> </blockquote> <p>and I was like - <em>what?!</em> </p> <p>"The variable must also be declared in each function that wants to access it". Then, I was shocked one more time:</p> <pre><code>int max; /* ... */ int main() { extern int max; /* ... */ } </code></pre> <p>And one more - <em>what?!</em></p> <hr> <p>As far as I know (obviously, it's not much and far from enough), <code>extern</code> makes sense only when you define a global variable somewhere and you want to access it through another file (not to define it again).</p> <p>So:</p> <ul> <li>What's the point of this <code>extern int max</code> <em>inside</em> the <code>main</code> or any other function?</li> <li>Does the standard <em>really</em> says, that this is a <em>must</em> (that I need to declare, for this example, this <em>max</em> in each function, that will use it?)</li> <li>Is this the same for C++ (that's why I placed the C++ tag)? This is the first time I see something like this.</li> </ul> <p>Note: this is not the same as <a href="https://stackoverflow.com/questions/9686198/what-is-the-use-of-declaring-a-static-variable-as-extern-inside-a-function">What is the use of declaring a static variable as extern inside a function?</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload