K&R-style variadic functions
Regan Heath
regan at netmail.co.nz
Tue Jul 17 08:11:16 PDT 2012
On Tue, 17 Jul 2012 15:46:34 +0100, Jacob Carlborg <doob at me.com> wrote:
> On 2012-07-17 16:36, Regan Heath wrote:
>
>> I believe old-style no parameter function declarations MUST have "void"
>> i.e.
>>
>> int foo(void);
>
> That is still the case, regardless of "style"?
>
>> They cannot read:
>>
>> int foo();
>>
>> The latter MUST have parameters, we just can't tell what they are.
>
> Take a look at this:
>
> http://en.wikipedia.org/wiki/K%26R_C#KRC
>
> In that example none of the functions have any parameters declared and
> are not called with any arguments.
Ahh, I've been looking at the ANSI C spec and assuming that is what you're
basing things off, K&R C was pre-ANSI C and may have different rules. I
think you should probably aim to be ANSI C compliant and above, and ignore
K&R.
Looking at the ANSI C spec again, section 6.7.5.3, item 10 says:
"The special case of an unnamed parameter of type void as the only item in
the list specifies that the function has no parameters."
So, "void" indicates no parameters..
Item 14 is also applicable and says:
"An identifier list declares only the identifiers of the parameters of the
function. An empty list in a function declarator that is part of a
definition of that function specifies that the function has no parameters.
The empty list in a function declarator that is not part of a definition
of that function specifies that no information about the number or types
of the parameters is supplied." 124)
The latter part of that is applicable to declarations in header files (the
former is for definitions in c files); "The empty list in a function
declarator that is /not part of a definition of that function/ specifies
that /no information about the number or types of the parameters is
supplied/."
So, a function like:
int foo();
in a header "specifies that no information about the number or types of
the parameters is supplied".
However footnote 124) says see 6.1.6, and 6.1.6 says:
6.11.6 Function declarators
The use of function declarators with empty parentheses (not
prototype-format parameter type declarators) is an obsolescent feature.
So, coming full circle, it seems like I'm right after all .. I think.
"void" is required to indicate no parameters and () is obsolete in ANSI C.
R
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list