[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

C syntax ambiguity

unknown

2/9/2011 5:50:00 PM

Hello

Imagine the following code occurring at Function Scope:

{
f();
}

Now there might be two valid programmer intentions here.

Either he is declaring f to be a function returning int and taking an
unspecified number of arguments.

Or, he is calling a function f with no arguments and discarding it's
return value.

Most compilers seem to prefer the second interpretation. However, as it
would be possible to call the function unambiguously by dereferencing:
(*f)();
wouldn't it be preferable for the first interpretation to be made?

[Rgds]
11 Answers

Tom St Denis

2/9/2011 6:05:00 PM

0

On Feb 9, 12:49 pm, Gand Alf <nos...@nospam.com> wrote:
> Hello
>
> Imagine the following code occurring at Function Scope:
>
> {
>   f();
>
> }
>
> Now there might be two valid programmer intentions here.
>
> Either he is declaring f to be a function returning int and taking an
> unspecified number of arguments.

Is that interpretation even valid in C90 or 99? At the very least
he'd have to write

f(void);

If he meant to declare a function and not call it.

When I see a lonely f(); I see a function call.

Tom

Ben Pfaff

2/9/2011 6:52:00 PM

0

Gand Alf <nospam@nospam.com> writes:

> {
> f();
> }
>
> Now there might be two valid programmer intentions here.
>
> Either he is declaring f to be a function returning int and taking an
> unspecified number of arguments.

Nope. In C89, a declaration has to have a storage class
specifier, a type specifier, or a type qualifier. Otherwise it
doesn't fit the syntax of a declaration and so it can't be
interpreted as one.
--
Ben Pfaff
http://be...

Keith Thompson

2/9/2011 7:05:00 PM

0

Gand Alf <nospam@nospam.com> writes:
> Imagine the following code occurring at Function Scope:
>
> {
> f();
> }
>
> Now there might be two valid programmer intentions here.
>
> Either he is declaring f to be a function returning int and taking an
> unspecified number of arguments.
>
> Or, he is calling a function f with no arguments and discarding it's
> return value.
>
> Most compilers seem to prefer the second interpretation. However, as it
> would be possible to call the function unambiguously by dereferencing:
> (*f)();
> wouldn't it be preferable for the first interpretation to be made?

In C99, f(); is unambiguously a function call, since implicit int was
removed from the language.

In C90, at least gcc consistently treats it as a function call, and
complains if there's a declaration following it. I've tried and failed
to find wording in the C90 standard that says it can't be a function
declarator. Anyone else?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

2/9/2011 7:21:00 PM

0

Ben Pfaff <blp@cs.stanford.edu> writes:
> Gand Alf <nospam@nospam.com> writes:
>> {
>> f();
>> }
>>
>> Now there might be two valid programmer intentions here.
>>
>> Either he is declaring f to be a function returning int and taking an
>> unspecified number of arguments.
>
> Nope. In C89, a declaration has to have a storage class
> specifier, a type specifier, or a type qualifier. Otherwise it
> doesn't fit the syntax of a declaration and so it can't be
> interpreted as one.

That makes sense, but where is that rule stated? I can't find it
in the C90 standard.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Kenneth Brody

2/9/2011 8:02:00 PM

0

On 2/9/2011 12:49 PM, Gand Alf wrote:
> Hello
>
> Imagine the following code occurring at Function Scope:
>
> {
> f();
> }
>
> Now there might be two valid programmer intentions here.
>
> Either he is declaring f to be a function returning int and taking an
> unspecified number of arguments.
>
> Or, he is calling a function f with no arguments and discarding it's
> return value.
>
> Most compilers seem to prefer the second interpretation. However, as it
> would be possible to call the function unambiguously by dereferencing:
> (*f)();
> wouldn't it be preferable for the first interpretation to be made?

One could also unambiguously declare the function using "int f();".

Which do you think is more likely the intended use? The function-scope
declaration, or a call to the function?

I would have to agree that I would be very surprised if this were meant to
declare the function, rather than call it. In fact, I would be very
surprised if my compiler treated it as such. (You say "most compilers", but
I would venture that it's quite possibly "all compilers".)

By the same argument, some compilers (and, I believe, the C99 standard
itself) allow declarations to occur within a code block, not just at the
top. If that were the case, how would one call a function which takes no
parameters, without having to jump through hoops with "(f)()"?

void foo()
{
bar();
printf("Hello, world.\n");
baz();
}

--
Kenneth Brody

Ben Bacarisse

2/9/2011 9:19:00 PM

0

Keith Thompson <kst-u@mib.org> writes:

> Ben Pfaff <blp@cs.stanford.edu> writes:
>> Gand Alf <nospam@nospam.com> writes:
>>> {
>>> f();
>>> }
>>>
>>> Now there might be two valid programmer intentions here.
>>>
>>> Either he is declaring f to be a function returning int and taking an
>>> unspecified number of arguments.
>>
>> Nope. In C89, a declaration has to have a storage class
>> specifier, a type specifier, or a type qualifier. Otherwise it
>> doesn't fit the syntax of a declaration and so it can't be
>> interpreted as one.
>
> That makes sense, but where is that rule stated? I can't find it
> in the C90 standard.

I don't have an authoritative copy, but the text file widely available
online has this syntax:

declaration:
declaration-specifiers init-declarator-list<opt> ;

declaration-specifiers:
storage-class-specifier declaration-specifiers<opt>
type-specifier declaration-specifiers<opt>
type-qualifier declaration-specifiers<opt>

which is exactly what Ben Pfaff said. None of storage-class-specifier,
type-specifier or type-qualifier can be empty. Is this the syntax you
have in your C90 copy?

--
Ben.

Ben Bacarisse

2/9/2011 9:25:00 PM

0

Keith Thompson <kst-u@mib.org> writes:

> Gand Alf <nospam@nospam.com> writes:
>> Imagine the following code occurring at Function Scope:
>>
>> {
>> f();
>> }
<snip>
> In C90, at least gcc consistently treats it as a function call, and
> complains if there's a declaration following it. I've tried and failed
> to find wording in the C90 standard that says it can't be a function
> declarator. Anyone else?

Not wording as such but it's in the syntax. See my other post.

For what it's worth, the fact that a declaration must start with a
keyword or a typedef name was there in K&R C and is still there in C99.
I don't have a certified copy of C90, but it seems as odd thing to have
altered (and the altered back).

--
Ben.

blp

2/9/2011 11:02:00 PM

0

Keith Thompson <kst-u@mib.org> writes:

> Ben Pfaff <blp@cs.stanford.edu> writes:
>> Gand Alf <nospam@nospam.com> writes:
>>> {
>>> f();
>>> }
>>>
>>> Now there might be two valid programmer intentions here.
>>>
>>> Either he is declaring f to be a function returning int and taking an
>>> unspecified number of arguments.
>>
>> Nope. In C89, a declaration has to have a storage class
>> specifier, a type specifier, or a type qualifier. Otherwise it
>> doesn't fit the syntax of a declaration and so it can't be
>> interpreted as one.
>
> That makes sense, but where is that rule stated? I can't find it
> in the C90 standard.

It's stated as part of the context-free grammar for "declaration"
in the standard.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

Keith Thompson

2/10/2011 3:28:00 AM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Keith Thompson <kst-u@mib.org> writes:
>> Ben Pfaff <blp@cs.stanford.edu> writes:
>>> Gand Alf <nospam@nospam.com> writes:
>>>> {
>>>> f();
>>>> }
>>>>
>>>> Now there might be two valid programmer intentions here.
>>>>
>>>> Either he is declaring f to be a function returning int and taking an
>>>> unspecified number of arguments.
>>>
>>> Nope. In C89, a declaration has to have a storage class
>>> specifier, a type specifier, or a type qualifier. Otherwise it
>>> doesn't fit the syntax of a declaration and so it can't be
>>> interpreted as one.
>>
>> That makes sense, but where is that rule stated? I can't find it
>> in the C90 standard.
>
> I don't have an authoritative copy, but the text file widely available
> online has this syntax:
>
> declaration:
> declaration-specifiers init-declarator-list<opt> ;
>
> declaration-specifiers:
> storage-class-specifier declaration-specifiers<opt>
> type-specifier declaration-specifiers<opt>
> type-qualifier declaration-specifiers<opt>
>
> which is exactly what Ben Pfaff said. None of storage-class-specifier,
> type-specifier or type-qualifier can be empty. Is this the syntax you
> have in your C90 copy?

Yes, that's it; I just managed to miss it.

Incidentally, C99 has:

declaration-specifiers:
storage-class-specifier declaration-specifiers<opt>
type-specifier declaration-specifiers<opt>
type-qualifier declaration-specifiers<opt>
function-specifier declaration-specifiers<opt>

The only function-specifier is "inline".

Which means that this:

inline;

doesn't violate the grammar for a declaration -- but it does violate the
constraint in 6.7p2:

A declaration shall declare at least a declarator (other than
the parameters of a function or the members of a structure or
union), a tag, or the members of an enumeration.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ben Bacarisse

2/10/2011 3:57:00 AM

0

Keith Thompson <kst-u@mib.org> writes:
<snip>
> Incidentally, C99 has:
>
> declaration-specifiers:
> storage-class-specifier declaration-specifiers<opt>
> type-specifier declaration-specifiers<opt>
> type-qualifier declaration-specifiers<opt>
> function-specifier declaration-specifiers<opt>
>
> The only function-specifier is "inline".
>
> Which means that this:
>
> inline;
>
> doesn't violate the grammar for a declaration -- but it does violate the
> constraint in 6.7p2:
>
> A declaration shall declare at least a declarator (other than
> the parameters of a function or the members of a structure or
> union), a tag, or the members of an enumeration.

I'm not sure why you singled out "inline" or C99. The syntax for a
declaration is

declaration-specifiers init-declarator-list<opt> ;

so all of

int;
const;
extern;

satisfy the syntax but violate the constraint.

--
Ben.