AFAIR, "f()" accepts an undefined number of parameters, although I fail
to see the place where this is equivalent to a variable number of
arguments.
Can someone suggest me where I can read this?
Thanks!
--
Sensei <Sensei's e-mail is at Me-dot-com>
Error. Keyboard not attached. Press F1 to continue...
(Real BIOS Error)
"int main()" and "int main(void)" mean the same thing. See the gcc
documentation.
> AFAIR, "f()" accepts an undefined number of parameters, although I
> fail to see the place where this is equivalent to a variable number of
> arguments.
The two are not equivalent. C99 requires functions to be
declared before they are invoked. A function that accepts a
variable number of arguments must be declared with a prototype,
because a function declared without a prototype is not compatible
with one declared with an ellipsis terminator. See section
6.7.5.3 "Function declarators (including prototypes)":
Moreover, the parameter type lists, if both are present,
shall agree in the number of parameters and in use of the
ellipsis terminator; corresponding parameters shall have
compatible types. If one type has a parameter type list
and the other type is specified by a function declarator
that is not part of a function definition and that contains
an empty identifier list, the parameter list shall not have
an ellipsis terminator and the type of each parameter shall
be compatible with the type that results from the
application of the default argument promotions.
--
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;}}}
In the terminology of 6.9.1, a function definition with nothing
between the () has an "identifier list" (as opposed to a "parameter
type list," that is, a prototype), and the identifier list is empty.
Thus, the function is defined as having no parameters at all.
(My next paragraph has been the subject of debate, and some
smart people disagree with me. I think they're wrong, obviously, but
it's not a settled matter and you'll need to decide for yourself.)
Any definition (of anything) is also a declaration, and there's
a subtle point here: The empty identifier list *defines* a function
with no parameters at all, but the *declaration* says nothing about
the parameters. That is, the declaration that derives from the
definition is as if you had written `SomeType f();' -- all we know
is that the function takes a fixed number of parameters, but we don't
know how many nor what kind. The practical effect is that if you
*define* a function like `int f() { ...; return 42; }' and later
call it as `int x = f("Hello");', the compiler is not required to
diagnose the error (although some will). If you had defined the
function as `int f(void) { ...; return 42; }', the compiler would
definitely complain.
> AFAIR, "f()" accepts an undefined number of parameters, although I fail
> to see the place where this is equivalent to a variable number of
> arguments.
You won't find such a place, because they're not equivalent.
"Fixed but unspecified" (what you get with no "parameter type list,"
that is, no prototype) is not the same thing as "variable-length."
6.9.1p8 tells you that a function with a variable-length parameter
list must be defined with ",...", and 6.5.2.2p6 says that you must
use a ",..." prototype when calling such a function.
--
Eric Sosman
eso...@ieee-dot-org.invalid
Please don't quote peoples signatures, the bit typically after the
"-- ".
> "int main()" and "int main(void)" mean the same thing.
This is arguable wrong. See the many long threads in this group about
whether () on a function definition means the same as on a declaration.
It is also irrelevant to the main question.
> See the gcc
> documentation.
This is irrelevant to any part of the question. The gcc documentation is
not the standard. The gcc documentation does not define the language.
The OP was explicitly asking about the standard, hence mentioning the
part of the standard in question.
The actual question has been answered correctly by Ben Pfaff.
--
Flash Gordon
However, "extern int f();" and "extern int f(void);" are not the same thing.
--
Kenneth Brody
Did you have a question? If so, writing "??" at the top of your
response isn't enough to let us guess what you want to know.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <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"
> Sensei <sens...@apple.me.com> writes:
>
>> AFAIR, "f()" accepts an undefined number of parameters, although I
>> fail to see the place where this is equivalent to a variable number of
>> arguments.
>
> The two are not equivalent. C99 requires functions to be
> declared before they are invoked. A function that accepts a
> variable number of arguments must be declared with a prototype,
> because a function declared without a prototype is not compatible
> with one declared with an ellipsis terminator. See section
> 6.7.5.3 "Function declarators (including prototypes)":
>
> Moreover, the parameter type lists, if both are present,
> shall agree in the number of parameters and in use of the
> ellipsis terminator; corresponding parameters shall have
> compatible types. If one type has a parameter type list
> and the other type is specified by a function declarator
> that is not part of a function definition and that contains
> an empty identifier list, the parameter list shall not have
> an ellipsis terminator and the type of each parameter shall
> be compatible with the type that results from the
> application of the default argument promotions.
So, is the following call illegal, and in case, how are the parameters
handled as specified by the standard? Are they simply discarded?
int f ( ) { return 0; }
/* ... */
printf( "zero parameters = %d\n", f() );
printf( "some parameters = %d\n", f(1, 2.1234, "hello") );
Moreover, I know that main should either declared as main(void) or
main(int argc, char *argv[] ), but what does it mean (always as for the
standard) regarding its parameters?
Thanks for clarifying!
--
Sensei�<Sensei's e-mail is at Me-dot-com>
We know Linux is the best, it can do infinite loops in five seconds.
(Linus Torvalds)
That's fine, of course.
> printf( "some parameters = %d\n", f(1, 2.1234, "hello") );
That's undefined behavior, meaning that it's wrong but the
implementation isn't required to diagnose it.
> Moreover, I know that main should either declared as main(void) or
> main(int argc, char *argv[] ), but what does it mean (always as for
> the standard) regarding its parameters?
I'm not sure what you're asking. main has either no parameters or two
parameters; if it has two, they're of type int and char**,
respectively. (Ignoring other implementation-defined definitions,
freestanding implementations, and so forth.)