On 28 Jul 2005 03:02:33 -0700, "siliconwafer" <sp*********@yahoo.com>
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/~a...FAQ-acllc.html