Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: identifier scope in old-style declarations

Reply
Thread Tools

Re: identifier scope in old-style declarations

 
 
Huibert Bol
Guest
Posts: n/a
 
      08-06-2008
Eric Sosman wrote:

> Thomas Pornin wrote:
>> Hello,
>>
>> with gcc-4.2.3, this does not compile:
>>
>> typedef int foo;
>> int f(foo)
>> int foo;
>> {
>> return foo;
>> }
>>
>> The error message is rather explicit ("old-style parameter declarations
>> in prototyped function definition"), which means that the compiler, when
>> encountering the "foo" on the second line, considered it to be a type
>> name, and not an argument name which would override the global type
>> definition within the function scope.
>>
>> I understand, from a practical point of view, that doing otherwise would
>> mess the compiler (it would contradict the almost-LALR(1) property of
>> the C grammer). However, I cannot find in the C standard where such an
>> override is forbidden. Right now, it seems to me that the construction
>> is ambiguous.
>>
>> Could someone shed some light on this ?

>
> Had to hunt for it a little, but I found it. 6.9.1p6: "[...] An
> identifier declared as a typedef name shall not be redeclared as a
> parameter. [...]"


But that's only the case if the declarator includes an _identifier list_, but
in the example above contains a _parameter type list_; so 6.9.1p5 applies:
"[...] No declaration list shall follow." which matches the error message
above.

- Huibert.
 
Reply With Quote
 
 
 
 
Huibert Bol
Guest
Posts: n/a
 
      08-06-2008
Eric Sosman wrote:

> For example, how would you describe the error in
>
> #include <stdio.h>
> int man(void) {
> return puts ("Hello, world!\n") == EOF;
> }
>
> ? (Yes, this is a trick question.)


It prints an additional newline

- Huibert.
 
Reply With Quote
 
 
 
 
Mark B [Diputsur]
Guest
Posts: n/a
 
      08-06-2008

"Huibert Bol" <(E-Mail Removed)> wrote in message
news:g7cqut$9qc$(E-Mail Removed)...
> Eric Sosman wrote:
>
>> For example, how would you describe the error in
>>
>> #include <stdio.h>
>> int man(void) {
>> return puts ("Hello, world!\n") == EOF;
>> }
>>
>> ? (Yes, this is a trick question.)

>
> It prints an additional newline


Doubtful it will print anything...
it's missing the function called at program startup.


 
Reply With Quote
 
Huibert Bol
Guest
Posts: n/a
 
      08-06-2008
Mark B. wrote:

> "Huibert Bol" <(E-Mail Removed)> wrote in message
> news:g7cqut$9qc$(E-Mail Removed)...
>> Eric Sosman wrote:
>>
>>> For example, how would you describe the error in
>>>
>>> #include <stdio.h>
>>> int man(void) {
>>> return puts ("Hello, world!\n") == EOF;
>>> }
>>>
>>> ? (Yes, this is a trick question.)

>>
>> It prints an additional newline

>
> Doubtful it will print anything...
> it's missing the function called at program startup.


Dûh, Eric never said it was a complete program!

- Huibert.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-06-2008
Eric Sosman wrote:

<snip>

> There's often some ambiguity in pinpointing "the" error in an
> erroneous source. Fundamentally, all that's really known is that
> the source is incorrect; diagnosing the nature of its incorrectness
> involves imagining a plausible correct source and describing the
> "edit distance" between them. Since there are many, many possible
> correct sources, there are many possible diagnoses.
>
> For example, how would you describe the error in
>
> #include <stdio.h>
> int man(void) {
> return puts ("Hello, world!\n") == EOF;
> }
>
> ? (Yes, this is a trick question.)


Well I suppose there would be no error until the last phase of
translation, which will fail because main is not defined, (and you have
not told us if the program is freestanding, in which case it would be
correct, provided the contents of stdio.h and the definition for puts
are supplied by the user.)

 
Reply With Quote
 
Mark B [Diputsur]
Guest
Posts: n/a
 
      08-06-2008

"Huibert Bol" <(E-Mail Removed)> wrote in message
news:g7crj1$9qc$(E-Mail Removed)...
> Mark B. wrote:
>
>> "Huibert Bol" <(E-Mail Removed)> wrote in message
>> news:g7cqut$9qc$(E-Mail Removed)...
>>> Eric Sosman wrote:
>>>
>>>> For example, how would you describe the error in
>>>>
>>>> #include <stdio.h>
>>>> int man(void) {
>>>> return puts ("Hello, world!\n") == EOF;
>>>> }
>>>>
>>>> ? (Yes, this is a trick question.)
>>>
>>> It prints an additional newline

>>
>> Doubtful it will print anything...
>> it's missing the function called at program startup.

>
> Dh, Eric never said it was a complete program!


Yet his claim that there is an error in the code suggests
that it should be... unless we have requirements on return
values from functions named 'man();' that I don't know
about...



 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-06-2008
santosh wrote:

> Eric Sosman wrote:
>
> <snip>
>
>> There's often some ambiguity in pinpointing "the" error in an
>> erroneous source. Fundamentally, all that's really known is that
>> the source is incorrect; diagnosing the nature of its incorrectness
>> involves imagining a plausible correct source and describing the
>> "edit distance" between them. Since there are many, many possible
>> correct sources, there are many possible diagnoses.
>>
>> For example, how would you describe the error in
>>
>> #include <stdio.h>
>> int man(void) {
>> return puts ("Hello, world!\n") == EOF;
>> }
>>
>> ? (Yes, this is a trick question.)

>
> Well I suppose there would be no error until the last phase of
> translation, which will fail because main is not defined, (and you
> have not told us if the program is freestanding, in which case it
> would be correct, provided the contents of stdio.h and the definition
> for puts are supplied by the user.)


Just to followup, many knowledgeable members of this group have
previously said that an implementation is not required to reject the
translation of any program that does not contain a selected #error
directive. In light of that I would amend my answer above and say that
no error need be emitted by a compiler for the translation unit given
above by Eric. A conforming implementation could insert it's own main
and invoke man from it.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-06-2008
Eric Sosman wrote:
>

.... snip ...
>
> For example, how would you describe the error in
>
> #include <stdio.h>
> int man(void) {
> return puts ("Hello, world!\n") == EOF;
> }
>
> ? (Yes, this is a trick question.)


What error? The result is linkable, but can't be a program. It
always returns 0, barring a write error. Lacking a header file,
linking will require an extrn statement or equivalent in the
caller.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-06-2008
Eric Sosman wrote:
> Eric Sosman wrote:
>> [...]
>> There's often some ambiguity in pinpointing "the" error in an
>> erroneous source. Fundamentally, all that's really known is that
>> the source is incorrect; diagnosing the nature of its incorrectness
>> involves imagining a plausible correct source and describing the
>> "edit distance" between them. Since there are many, many possible
>> correct sources, there are many possible diagnoses.
>>
>> For example, how would you describe the error in
>>
>> #include <stdio.h>
>> int man(void) {
>> return puts ("Hello, world!\n") == EOF;
>> }
>>
>> ? (Yes, this is a trick question.)

>
> Huibert Bol, Mark B [Diputsur], and santosh have between them
> described two errors in this code. To their credit, they did not
> get sidetracked by the blatant red herring, either! But they all
> overlooked the *real* error, which is that the code prints "Hello,
> world!\n\n" instead of "Hello, world!\a\n"


Why is that an error? There was no description of a requirement
for a single \n.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-06-2008
santosh <(E-Mail Removed)> writes:
[...]
>> Eric Sosman wrote:

[...]
>>> For example, how would you describe the error in
>>>
>>> #include <stdio.h>
>>> int man(void) {
>>> return puts ("Hello, world!\n") == EOF;
>>> }
>>>
>>> ? (Yes, this is a trick question.)

[...]
> Just to followup, many knowledgeable members of this group have
> previously said that an implementation is not required to reject the
> translation of any program that does not contain a selected #error
> directive.


Right.

> In light of that I would amend my answer above and say that
> no error need be emitted by a compiler for the translation unit given
> above by Eric.


Compilers don't emit errors; they emit diagnostic messages.

But I think you're conflating two different things: the cases in which
a compiler must reject a translation unit (only if it contains a
selected "#error"), and the cases in which it must issue a diagnostic
message (if there's a violation of a constraint or syntax rule).

The quoted code contains none of those things, and in fact it's
perfectly correct -- as far as the compiler can tell. If this were
real code, it would be *likely* that "man" rather than "main" was a
typo, but it happens to be a typo that changes a correct translation
unit to a different correct translation unit. A sufficiently clever
compiler could warn about it (or about anything), but there's no
requirement for it to do so.

> A conforming implementation could insert it's own main
> and invoke man from it.


An implementation is specifically forbidden to declare a prototype for
main (C99 5.1.2.2.1p1); that includes a prototype that's part of a
definition. Conceivably an implementation could provide a
non-prototyped definition, but that would be silly.

On the other hand, some tool in the environment (that's not considered
part of the C implementation) could always provide a definition for
main. Typically that tool is a text editor that you use to write your
own definition of main, but it could be something else.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD Error: "Expecting '.', identifier or quoted identifier withmultiple tables. S_K ASP .Net 0 08-06-2008 09:22 PM
the static identifier in the global scope parag_paul@hotmail.com C Programming 1 04-11-2008 08:57 AM
How can I fix the warning C4786 'identifier' : identifier wastruncated to 'number' characters in the debug information...? babakandme@yahoo.com C++ 2 12-11-2007 08:17 PM
identifier not found error, undeclared identifier aarthi28@gmail.com C++ 2 02-26-2007 02:11 AM
Trying to declare identifier locally and hide a file scope identifier Kobu C Programming 7 02-25-2005 05:19 AM



Advertisments