Re: "extern" specifier ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: main function
    ... user--must not declare its prototype. ... int main; ... other than int is considered to be undefined behavior; some compilers ... than int, such as void, but you should avoid using such a definition, ...
    (comp.lang.c)
  • Scope/use of prototypes Qs
    ... int DATASIZE = MAXSIZE; ... void getdata; ... with a few different compilers ... ...
    (comp.lang.c)
  • protected by instance, not by type?
    ... compilers and are unsure whether or not the compiler is right. ... I allway thought the C++ type system only relies on static types. ... int privA; ... void protMethB ...
    (comp.lang.cpp)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH] get rid if __cpuinit and __cpuexit
    ... unsigned long action, void *hcpu) ... unsigned int cpu = hcpu; ... -static int __cpuinit ... __cpu_up(unsigned int cpu) ...
    (Linux-Kernel)