The following code:
#include <stdio.h>
static int i[];
int main()
{
printf("%d\n",i[0]);
}
compiles fine but does not link. I would have thought that this would
not compile, since in the standard:
You thought wrong. Who told you that a compiler is *ever* required
not to compile? Up until C99, there was never a requirement that an
implementation refuse to compile a translation unit, regardless of
what it contained.
Since C99, a compiler is prohibited from successfully translating a
program containing a #error preprocessor directive, unless this
directive itself is part of a group skipped by conditional
compilation.
Other than that, a translator may translate and generate an executable
from anything you hand it.
"If the declaration of an identifier for an object is a tentative
definition and has internal
linkage, the declared type shall not be an incomplete type."
Your program violates a "shall" in the standard that happens to be in
a "semantics" section, and not in a "constraints" section. That means
that your program generates undefined behavior and no requirements at
all are placed on the implementation. No diagnostic is required.
If your program violated a "shall" in a constraints section of the
standard, the compiler would be required to issue a diagnostic. But
that does not mean it could not also produce an executable, and the
behavior would still be undefined.
I believe that i fits all the above conditions, and is an incomplete
type. Should this program not compile (hence a conformance issue with
gcc), or am I wrong/missing something, etc
gcc, by default, is not a C compiler, but a compiler for a language
often referred to as "gnu C". That's not really a terrible fault, as
most compilers do not translate in their best standard conforming mode
by default.
Check your compiler documentation or post in a gcc newsgroup to find
out what options you should invoke your compiler with to get it to
issue a diagnostic for this situation, it is probably possible.
But get over the notion that you can write code that "should not
compiler". Other than the #error directive in C99 or later
conformance, a compiler is never forbidden from producing an
executable.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html