Re: Function with unspecified number of arguments



On Jan 30, 2:34 pm, somenath <somenath...@xxxxxxxxx> wrote:
Hi All,

I have one question regarding unspecified number of argument of
function.
I would like to know why functions are allowed to define with
unspecified number of arguments ?

For example
void f()
{

}

int main(void)
{
    f(3,4);
    f(3,4,3);
    return 0;

}

Inside void f() what ever argument is passed from main is useless . So
where function with unspecified number of  arguments are useful ?

First, your example is incorrect. A function declaration

int f ();

means that f is a function with an unknown, fixed number of
parameters. In other words, f must have a fixed number of parameters,
you just don't tell the compiler how many. Of the two calls f (3, 4)
and f (3, 4, 3), at least one invokes undefined behaviour. However,
you wrote a function definition:

inf f () { ... }

and _that_ is definitely a function with zero arguments. Why is it
allowed? For historical reasons. Or hysterical raisins, whatever you
prefer. The first C compilers allowed it, and an amazing number of
programmers doesn't appreciate the possibility that the compiler could
tell you about using the wrong parameters.
.



Relevant Pages