storage class ambiguity

Robert Berlinger naftoli at aecom.UUCP
Wed Nov 2 07:45:40 AEST 1983


There seems to be a slight ambiguity in the description of
external variables in the C reference manual. (The C programming
language, Kernighan & Ritchie, Prentice Hall 1978). To my
knowledge the proper way to declare a global variable is to
declare "int foo;" in one source file and declare "extern int
foo;" in all the rest. However, according to the reference manual
this seems unnecessary and you should be able to declare "int
foo;" in all source files.

The reference page 205, paragraph 10.2 states:

> 10.2 External data definitions
>     An external data definition has the form
>
>		 data-definition:
>			 declaration
>
> The storage class of such data may be extern (which is the
> default) or static, but not auto or register.

According to this, since data outside of a procedure is by default
extern, you need not ever explicitly declare it as such. This is
true in the case of most UN*X compilers that I've seen.  
Example:

		file 1: int foo = 8;
		file 2: int foo;

No errors; foo will be initialized to 8.

		file 1: int foo;
		file 2: int foo = 8;

No errors; foo will be initialized to 8.

		file 1: int foo = 8;
		file 2: int foo = 7;

Multiply defined error from loader.
		
		file 1: extern int foo;
		file 2: extern int foo;

Undefined _foo message from loader.

		file 1: extern int foo = 8;

Compiler msg - cannot initialize external.

The last two errors are the most important here. If extern is
purported to be the default, then why do you get an error if you
state it explicitly? Also, I've heard that IBM/370 compilers do
not resolve the references and will create two seperate global
cells.  If this is true (that not all compilers resolve several
global declarations) then the reference manual should not say
that extern is the default.  Rather, a null storage class outside
of the program definition should mean declaration, and an extern
storage class should mean definition.

Robert Berlinger
Systems Support
Albert Einstein Coll. of Med.

{philabs,esquire,cucard}!aecom!naftoli



More information about the Comp.lang.c mailing list