Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Newsgroups | comp.std.c |
| Subject | Re: Unimportant but amusing question |
| Date | 2013-01-28 10:46 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <kfnd2wprvr2.fsf@x-alumni2.alumni.caltech.edu> (permalink) |
| References | <kfnlibfsvyj.fsf@x-alumni2.alumni.caltech.edu> <tf1et9-0tq.ln1@jones.homeip.net> |
lawrence.jones@siemens.com writes:
> Tim Rentsch <txr@alumni.caltech.edu> wrote:
>> Consider the following program:
>>
>> int x = 1;
>>
>> int
>> main( int argc, char *argv[] ){
>> int x;
>> extern int x;
>> x = 0;
>> { extern int x; x = 2; }
>> return x;
>> }
>>
>> Which of these choices best describes the program:
>>
>> A. Constraint violation
>> B. Undefined behavior
>> C. Defined behavior
>> D. Strictly conforming
>> E. Proof that God exists
>>
>> Please explain your answer.
>>
>>
>> Hint: Read 6.2.2 p4 before answering.
>
> I'd say A. 6.7p3:
>
> If an identifier has no linkage, there shall be no more than
> one declaration of the identifier (in a declarator or type
> specifier) with the same scope and in the same name space....
>
> That trumps 6.2.2p4 for the two declarations of x in the outer-most
> block of main. Since they're in the same scope, no hiding is involved.
Certainly that's a plausible argument. However, 6.2.2 p4 can be
read as saying the linkage for this identifier is not no linkage but
rather external linkage ("if the prior declaration __specifies__ no
linkage [my emphasis], then the identifier has external linkage").
If 'x' in the two declarations are different identifiers then 6.7p3
doesn't apply; if these are the same identifier, then 6.2.2p4
overrides the linkage for the one identifier and makes it be
external linkage, in which case 6.7p3 again doesn't apply.
Don't get me wrong, I'm confident your interpretation is what was
intended. But just from reading the text that conclusion isn't
the only plausible one, or even necessarily the most likely one.
I know some versions (sorry, don't know which ones) of gcc accept
the above program under -std=c99 -pedantic-errors, and with no
complaint.
Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Unimportant but amusing question Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-26 09:19 -0800
Re: Unimportant but amusing question lawrence.jones@siemens.com - 2013-01-26 17:21 -0500
Re: Unimportant but amusing question Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-28 10:46 -0800
Re: Unimportant but amusing question Bart van Ingen Schenau <bart@ingen.ddns.info.invalid> - 2013-01-29 10:06 +0000
Re: Unimportant but amusing question "Derek M. Jones" <derek@_NOSPAM_knosof.co.uk> - 2013-01-29 12:45 +0000
csiph-web