Re: "extern" specifier ?
- From: James Kanze <james.kanze@xxxxxxxxx>
- Date: Thu, 3 Dec 2009 03:36:11 -0800 (PST)
On Dec 2, 6:25 pm, "Igor Tandetnik" <itandet...@xxxxxxxx> wrote:
Robby <Ro...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
================================F1.c
#include <stdio.h>
int count;
void f1();
int main(void)
{
int i;
f1();
for(i=0; i<count; i++) printf("%d ", i);
return 0;
}
==============================
============================F2.c
#include <stdlib.h>
int count;
void f1(void)
{ count = rand(); }
===============================
According to the book, the above example is not supposed to
work as it explains on quote:
"If you declare count a second time, many linkers will
report a duplicate symbol error, which means that count is
defined twice and the linker doesn't know which to use."
Now, I tried this in VC++ as a simple .c program as shown
above and I get no errors or warnings and VC++ promts a
build succeeded in the output box ???
Looks like a bug to me. The program is in fact invalid, and
the linker should have complained.
It's undefined behavior. The compiler is not required to
complain. (This is the case for all errors which involve more
than one file, I believe.)
Historically, most early C compilers (late 1970's/early 1980's)
implemented global definitions as above more or less as if they
were named common blocks in Fortran---just overlay all of the
definitions with the same name. The C standard decided to
require the extern, in order to allow more rigorous
implementations, but many C compilers continue to support the
older semantics.
--
James Kanze
.
- References:
- "extern" specifier ?
- From: Robby
- Re: "extern" specifier ?
- From: Igor Tandetnik
- "extern" specifier ?
- Prev by Date: Re: Type cast problem with VC++ 2005 Express Edition
- Next by Date: Re: "extern" specifier ?
- Previous by thread: Re: "extern" specifier ?
- Next by thread: Re: "extern" specifier ?
- Index(es):
Relevant Pages
|