0

I'm aware that the compiler adds extern to functions by default. But does it do that with variables? I can do int foo as many times as I want without compiler errors, what is the point of extern int foo then? Would int foo be global as well?

  • `extern` does not mean "global", it means that the function or variable was defined in another compilation module, and can be used by the current module if the two are linked together. – Marco Bonelli Oct 12 '20 at 11:15
  • Did you read the `6.9.2 External object definitions` in the C11 standard? – 12431234123412341234123 Oct 12 '20 at 11:15
  • I thought you had to buy a copy of the C standards be it C89/C99/C11 and wasn't available online? – ving arr Oct 12 '20 at 11:18
  • 1
    @vingarr https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents/83763#83763, more specifically here's [C11](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – Marco Bonelli Oct 12 '20 at 11:19
  • Note that with GCC 10 and later, the compiler uses `-fno-common` by default (instead of the previous `-fcommon`). That means that if you have global variables scattered around in header without the `extern` prefix, your software won't link with GCC 10 under default compilation flags. The standard doesn't allow it — it does recognize that it is a common extension. Make your life simpler — don't write variables definitions into header files without the `extern` prefix. – Jonathan Leffler Oct 12 '20 at 12:17
  • You can find an online, HTML version of a late draft of C11 at http://port70.net/~nsz/c/c11/n1570.html — I link to it a lot. – Jonathan Leffler Oct 12 '20 at 12:18

1 Answers1

1

extern int foo; means there is a variable of type int called foo somewhere, maybe in a other compilation unit. You need the extern keyword when multiple translation units should be able to directly access the same variable.

In this case every translation unit that accesses the variable foo needs at least one extern int foo; somewhere, possibly inside a header which is included in every translation unit which needs to access foo.

In one, and only one, translation unit there needs to be a int foo=0; (You can replace 0 with whatever you like), without extern. The variable foo "belongs" to this translation unit.

If you can, avoid global variables. Access them through functions.

  • But if I can or should use int foo only once, why does the compiler not care if I do it multiple times? – ving arr Oct 12 '20 at 11:27
  • @vingarr The _compiler_ doesn't care, but the _linker_ does. Try it – Jabberwocky Oct 12 '20 at 11:28
  • Or an explicit `extern` declaration with an initializer (`extern int foo = 0;`) can serve in place of an implicitly extern declaration. – John Bollinger Oct 12 '20 at 11:28
  • @vingarr, the distinction is between *definitions* and mere *declarations*. There must be exactly one definition of each external object, but there may be multiple declarations. – John Bollinger Oct 12 '20 at 11:30