From 9198465745658212016
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10f5bc,6f6e97a6fdbbf44c
X-Google-Attributes: gid10f5bc,public
X-Google-Thread: 109fba,6f6e97a6fdbbf44c
X-Google-Attributes: gid109fba,public
X-Google-Thread: 102b75,6f6e97a6fdbbf44c
X-Google-Attributes: gid102b75,public
X-Google-Thread: f78e5,6f6e97a6fdbbf44c
X-Google-Attributes: gidf78e5,public
X-Google-Thread: 1014db,6f6e97a6fdbbf44c
X-Google-Attributes: gid1014db,public
X-Google-Thread: 110292,6f6e97a6fdbbf44c
X-Google-Attributes: gid110292,public
From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
Subject: Re: And you thought that the Pentium bug was bad!  (Get a load of this!)
Date: 1995/06/20
Message-ID: <3s5pke$iet@newshost.lanl.gov>#1/1
X-Deja-AN: 104726347
distribution: world
references: <3s2las$1br@hustle.rahul.net> <3s4h92$hpf@hacgate2.hac.com>
followup-to: comp.lang.c
organization: Los Alamos National Laboratory
newsgroups: comp.std.c++,comp.lang.c,comp.lang.c++,comp.sys.intel,comp.arch,comp.software.testing

Followup set to comp.lang.c: comp.std.c omitted from the huge list of groups
because it has already been discussed there.

In article <3s4h92$hpf@hacgate2.hac.com>, collins@thor.tu.hac.com (Ron
Collins) writes: 
<snip>
|> : double d = 0.9;
|> :  
|> : int
|> : main ()
|> : {
|> :     double d = 0.8;
|> :     {
|> :         extern double d;
|> :  
|> :         printf ("%f\n", d);
|> :         return 0;
|> :     }
|> : }
|> : -------------------------------------------------------------------------
|> 
|> : This program *should* print 0.900000, and with most compilers, it does.
|> 
|> Maybe I'm missing the point here... but it seems to me that the program
|> *should* print 0.8 -- since the "d" defined outside the block is
initialized 
|> to 0.8.  I don't have any books with me, but is "extern" *required* to
|> scope outside of the function? (I thought it just had to scope outside
|> the current block).

The code should print 0.9 because the d relevant to the printf is the
declaration that says `extern double d;'. When the compiler meets a
definition that specifies extern, it asks if a previous declaration is
visible. (In this case, the answer is yes: the `double d = 0.8;' is visible at
that moment.) If a declaration is visible, it next asks if that declaration
has internal or external linkage. (In this case, the answer is no: that
declaration is block scope declaration that does not specify extern: so it is
a declaration of an object with no linkage). If it has external or internal
linkage, the new declaration specifies the same linkage; otherwise it
specifies external linkage. (So this d has external linkage). The file scope
`double d = 0.9;' does not specify static: at file scope absence of static is
the same as specifying extern. Following the logic mentioned above, this d
also has external linkage.

Now every declaration of a variable with no linkage specifies a unique
object: different from all others during its lifetime. All external
declarations of the same name specify the same object. All internal
declarations (none here) of teh same name _in a given translation unit_
(roughly source file) specify the same object. So, in this case the d being
printed is the same as the d being initialized to 0.9; not the  d being
initialized to 0.8.

(The details of the rules for interpretation of extern has been changed by
the Technical Corrigendum: I described the new rules. For this code, it
does not matter anyway.)

Cheers
Tanmoy
-- 
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



