Function definition without parameter list

J

John Taylor

Hello,

I have another doubt about C99 language syntax, this time with function
definitions. I don't understand why it is not possible to define a
function "x" in the following way:

int x {return 0;}

Let me explain. A <function-definition> can be defined as
<declaration-specifiers> <declarator> <compount-statement>. In my case,
<declaration-specifiers> is "int", <declarator> is "x" and the
<compound-statement> is "{return 0;}" I read the constrains in section
6.9.1 but I couldn't manage to found the point in which the standard
forbids similar code.

Of course for the piece of code to be compiled I also must have added
the corresponding function declaration, that would look such as an
integer declaration:

int x;

All of this is very weird to me.

Thanks for any explaination.
 
J

James Kuyper

John said:
Hello,

I have another doubt about C99 language syntax, this time with function
definitions. I don't understand why it is not possible to define a
function "x" in the following way:

int x {return 0;}

Let me explain. A <function-definition> can be defined as
<declaration-specifiers> <declarator> <compount-statement>. In my case,
<declaration-specifiers> is "int", <declarator> is "x" and the
<compound-statement> is "{return 0;}" I read the constrains in section
6.9.1 but I couldn't manage to found the point in which the standard
forbids similar code.

The key point is that the syntax doesn't cover all of the rules. There
are also constraints. The relevant constraint is the one in 6.9.1p2:

"The identifier declared in a function definition (which is the name of
the function) shall have a function type, as specified by the declarator
portion of the function definition.141)"

Section 6.7.5.3p5 identifies the two different ways in which a
declaration can declare a function:

" D( parameter-type-list )
or
D( identifier-listopt )"

Your declaration does not match either form, so it does not declare a
declarator with a function type, and therefore is not allowed as a
function declaration. Since the declaration doesn't qualifying as a
function declaration, the presence of the compound-statement makes it a
syntax error.
 
B

Ben Bacarisse

John Taylor said:
I have another doubt about C99 language syntax, this time with
function definitions. I don't understand why it is not possible to
define a function "x" in the following way:

int x {return 0;}

Let me explain. A <function-definition> can be defined as
<declaration-specifiers> <declarator> <compount-statement>. In my
case, <declaration-specifiers> is "int", <declarator> is "x" and the
<compound-statement> is "{return 0;}" I read the constrains in section
6.9.1 but I couldn't manage to found the point in which the standard
forbids similar code.

It is the very first constraint:

"The identifier declared in a function definition (which is the name
of the function) shall have a function type, as specified by the
declarator portion of the function definition."

Your identifier ('x') has an object type ('int'). Declarators for
function types have always have parentheses.

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top