Re: Use of 'extern' keyword



On 28 Jul 2005 03:02:33 -0700, "siliconwafer" <spdandavate@xxxxxxxxx>
wrote in comp.lang.c:

> Hi all,
> I wanted to know that is use of extern keyword mandatory in case of
> global variables and functions used in other source files?
> i.e consider a following piece of code from MSDN explaining extern
> storage class:
> /******************************************************************
> SOURCE FILE ONE
> *******************************************************************/
>
> extern int i; /* Reference to i, defined below */
> void next( void ); /* Function prototype */
>
> void main()

This is typical Microsoft arrogant, ignorant nonsense. Since they
don't claim conformance to the C standard later than 1995, what you
have when you start a program with "void main()" is totally undefined
behavior, therefore no longer actually a C program at all. At least
the C language standard says nothing at all about what it should or
will do.

But that does not really have anything to do with the issue you are
asking about.

> {
> i++;
> printf( "%d\n", i ); /* i equals 4 */
> next();
> }
>
> int i = 3; /* Definition of i */
>
> void next( void )
> {
> i++;
> printf( "%d\n", i ); /* i equals 5 */
> other();
> }
>
> /******************************************************************
> SOURCE FILE TWO
> *******************************************************************/
>
> extern int i; /* Reference to i in */
> /* first source file */
> void other( void )
> {
> i++;
> printf( "%d\n", i ); /* i equals 6 */
> }
>
>
> Here,if I drop the extern keyword from source file 2 and compile and
> link the 2 source files I get the same result as with 'extern' keyword.
> i.e extern keyword is optional.same is the case with functions.
> If so in which cases is the use of 'extern' mandatory?
> regards,
> -Siliconwafer

I went into detail about how the C standard defines this in my
response to Antonio Contreras's reply to your original post.

To summarize, if you remove the 'extern' keyword from the declaration
of 'i' in the second source file, you create an implicit external
definition of 'i' in that translation unit. The first source file has
an explicit external definition of 'i'. If you combine these into one
program, that program has two external definitions of 'i', and that
produces undefined behavior, which literally means that the C standard
washes its hands and doesn't say what should or will happen.

For reasons not worth going into here, on some compilers the program
will build and work the same if you remove the 'extern' keyword in the
second file. On other compilers you will get an error from the linker
and no executable file. I have even seen compilers that will create
different objects for each source file. Changes you make to 'i' in
one file will not change the value of 'i' in the other file.

So to answer your question about when you need the 'extern' keyword,
the C standard says you need it on every file scope declaration of an
object in all the source files of a program, with at most one
exception.

Regardless of how your particular version of your compiler treats this
particular example, if you want your program to build without error
and run with the same results on all C compilers, you need to have the
'extern' in the second file.

There are always dangers in assuming the requirements of the language
from the results of one compiler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: Static, Extern and Global
    ... If a function is defined with the 'static' keyword in front of its ... the name is not visible outside of the original source file. ... There is no 'Extern', or 'static' as you spelled it above. ... Everything defined at file scope in C or namespace scope in C++ has ...
    (alt.comp.lang.learn.c-cpp)
  • Re: keyword extern
    ... The header should also be included in the definition source file. ... >> This is the only way to check the match between the declaration and the ... term you are looking for here is "file scope" which is well defined by ... One never needs to use the extern keyword with a function definition, ...
    (comp.lang.c)
  • Re: "extern" inside a block
    ... > with an initializer, specifying external ... I put extern ... after preprocessing that source file has this at the top: ... But how does this work when extern declaration ...
    (comp.lang.c)
  • Re: Variable already defined in .obj - cant figure this one out
    ... What's the trick? ... Each source file includes your header only ... it extern in at least one of the files. ...
    (microsoft.public.vc.mfc)
  • Re: "extern" inside a block
    ... In multi source file programs, ... > I know the behaviour when extern declaration follows a non-extern ... Linkage of Identifiers ... scope in which a prior declaration of that identifier is visible [and ...
    (comp.lang.c)