Take a look at the following code:
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.
#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.