Re: Function with unspecified number of arguments



On Wed, 30 Jan 2008 06:34:18 -0800 (PST), somenath
<somenathpal@xxxxxxxxx> wrote in comp.lang.c:

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 ?

C does not allow the definition of functions with unspecified numbers
of arguments.

For example
void f()
{
}

The function body above defines a function that accepts NO ARGUMENTS
and returns nothing.

If you had not included the body, just this:

void f();

....then you would have provided a declaration, not a prototype, of a
function returning nothing and accepting an unspecified, but fixed,
argument list.

But you did not, and cannot in C, define such a function.

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 ?

As has been said, calling f() with any arguments at all produces
undefined behavior.

All you did was tell the compiler not to check that you called f()
correctly, that you would be responsible for doing so.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: faster access private or public?
    ... > void g ... Only if the compiler can somehow prove that this code is always called ... in a context where it causes undefined behavior. ... > in this part of the code while optimizing some other piece of code? ...
    (microsoft.public.vc.language)
  • Re: how to release memory?
    ... interest is that a pointer p is indeterminate. ... except that the compiler is free to optimize the statement away ... Is 'expr;' really invoking undefined behavior if expr's value ... standard doesn't ever say you cannot dereference a void*, ...
    (comp.lang.c)
  • Re: const_cast<>
    ... required to issue a diagnostic message for "void main". ... There is no point in asking why a particular compiler produces any ... particular results once you invoke undefined behavior. ...
    (comp.lang.cpp)
  • Re: Should function argument be changed in function body?
    ... function body. ... void foo1 ... value will not affect caller code and it saves stack size. ... Translating the C code into assembler may well lead to loading an often used argument into a register, retrieving it only from this register, nothing forces the compiler to "leave" the variable in the stack. ...
    (comp.lang.c)
  • Re: Function with unspecified number of arguments
    ... void f ... The function body above defines a function that accepts NO ARGUMENTS ... function returning nothing and accepting an unspecified, but fixed, ... undefined behavior. ...
    (comp.lang.c)