• Coding
  • Understanding the "extern" keyword in C

Take a look at the following code:
#include <stdio.h>

int nbr=0;

int main ()
{
    int i=10;

    /* extern int nbr; */

    while (i--)
        printf("%d\n", nbr++);

    return 0;
}
Line 9 declares variable nbr as an extern int. What confuses me is that whether this line is commented or not, the result will be the same (printing numbers from 0 to 9).

I tried to do some research online but the only results I seem to get was the differences between global and extern from a C++ point of view. Don't you love it when Google mistakes C for C++ every single time?!

If anyone is familiar with C I have a few assumptions, for which I would love to get confirmation/refutal:

- I am assuming that omitting the declaration with extern is implied if it's missing. Moder compilers are smart enough to realize this, don't need the explicit declaration anymore, and they still support the extern keyword for backward compatibility purposes.

- As a matter of fact, it's still better style to explicitly declare the variable as extern; for clarity/readability purposes.

- I tried the above code(s) with two compilers: gcc and Sun C++ compiler. I am assuming this behavior is supported by most compilers? Is it defined in a C standard?

I hope I'm clear enough.
I got my answers.

TL;DR:
The keyword can be easily omitted when the definition of the external variable occurrs in the same file as its use. When dealing with multiple files, the keyword warns the compiler about a variable defined elsewhere.

Also, I learned the difference between variable declaration and definition:
Wikipedia wroteTo understand how external variables relate to the extern keyword, it is necessary to know the difference between defining and declaring a variable. When a variable is defined, the compiler allocates memory for that variable and possibly also initializes its contents to some value. When a variable is declared, the compiler requires that the variable be defined elsewhere. The declaration informs the compiler that a variable by that name and type exists, but the compiler need not allocate memory for it since it is allocated elsewhere.

The extern keyword means "declare without defining".
Am used to extern being used outside function bodies. Does that make any difference. Hope it's all still fresh in your mind.
@arithma:
Oh yes definitely. From what I understand there are two reasons why you would need to use extern:

1- You define an external (global) variable, above main, and you want to declare it in the function accessing it (main or other). If definition happened in the same file, the "extern" declaration can be implied. Generally speaking, you won't see this, although compilers won't complain about it. This is the case in the example I gave.

2- You define the external variable in another file (most likely a header file). To avoid compiler errors, you will declare (without defining) your variable at the beginning of each file that accesses it. "Declare" is just a way of telling your compiler "Don't worry, the variable is defined elsewhere". "Define" consists of actually allocating memory. From what I read, this has the minor inconvenience of tying the two files (the header with definition, and the function file with declaration) together, and it's up to the developer to avoid mistakes. In large code bases, this can be problematic, so people often use their "extern" declaration in a separate header file as well.

I hope I'm clear, and most importantly, I hope the above is correct.