Function with unspecified number of arguments

M

Morris Dovey

somenath said:
I would like to know why functions are allowed to define with
unspecified number of arguments ?

So that, for example, you won't need separate versions of
printf() to handle 1, 2, 3,... variables.
 
S

somenath

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 ?

Regards,
Somenath
 
M

Morris Dovey

Mark said:
But in modern C we don't use
int printf();

We use
int printf(const char *format, ...);

i.e. we use variable argument lists rather than unspecified arguments.

I misunderstood. The OP said "unspecified number" and I hooked
onto that rather than "unspecified arguments". Sorry.
 
M

Mark Bluemel

somenath said:
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 ?

Compatibility with historic C syntax, dating back to when I was a
boy :)
>So where function with unspecified number of arguments are useful ?

They aren't.

Functions with variable numbers of arguments are useful however...
 
V

vippstar

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 ?

they are useful for function pointers.
Example
-- snip.c --
int f1(int x) { return x; }
int f2(int x, int y) { return y; }
int main(void) {
int (*ptr)();
ptr = f1;
ptr(1);
ptr = f2;
ptr(1, 2);
return 0;
}
-- snip.c --
 
M

Mark Bluemel

Morris said:
So that, for example, you won't need separate versions of
printf() to handle 1, 2, 3,... variables.
But in modern C we don't use
int printf();

We use
int printf(const char *format, ...);

i.e. we use variable argument lists rather than unspecified arguments.
 
B

Ben Bacarisse

somenath said:
I would like to know why functions are allowed to define with
unspecified number of arguments ?

As has been said it is largely historical.
For example
void f()
{
}

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

This is not guaranteed to work. It is undefined behaviour.
Unspecified arguments does not mean a variable number thereof (6.5.2.2
p6: "If the number of arguments does not equal the number of
parameters, the behavior is undefined.").

So:

extern int g();

int main(void)
{
return g(42);
}

int g(int x) { return 0; }

is OK, but 'return g();' in main is not.
 
K

Keith Thompson

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()
[...]

they are useful for function pointers.
Example
-- snip.c --
int f1(int x) { return x; }
int f2(int x, int y) { return y; }
int main(void) {
int (*ptr)();
ptr = f1;
ptr(1);
ptr = f2;
ptr(1, 2);
return 0;
}
-- snip.c --

Hmm. As far as I can see, the only advantage in this case is that you
don't need an explicit conversion when you assign f1 or f2 to ptr.
But when you make a call via the pointer, you have to know the type of
the function actually being called anyway, because you're passing the
right number and type(s) of arguments.

As far as I know, old-style function declarations were kept in the
language only for backward compatibility. Your program could be
written without them, at the cost of a bit more verbosity. Some of
the disadvantages of your approach are that a call like ptr(1, 2, 3)
won't be diagnosed by the compiler, and you can't pass arguments of
type float or of an integer type narrower than int.

Are there any cases where old-style function declarations really give
you something useful (other than backward compatibility)? (I'll note
in passing that That Other Language seems to manage without them.)
 
V

vippstar

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()
[...]

they are useful for function pointers.
Example
-- snip.c --
int f1(int x) { return x; }
int f2(int x, int y) { return y; }
int main(void) {
int (*ptr)();
ptr = f1;
ptr(1);
ptr = f2;
ptr(1, 2);
return 0;
}
-- snip.c --
Are there any cases where old-style function declarations really give
you something useful (other than backward compatibility)? (I'll note
in passing that That Other Language seems to manage without them.)
There are, remember that post about two function pointers that take
each other as an argument and call them?
Well, in that topic I thought it's not possible, but I didn't know
about '()' in function pointers.
I cannot look up the post, maybe someone else can do it.
 
V

vippstar

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() [...]
they are useful for function pointers.
Example
-- snip.c --
int f1(int x) { return x; }
int f2(int x, int y) { return y; }
int main(void) {
int (*ptr)();
ptr = f1;
ptr(1);
ptr = f2;
ptr(1, 2);
return 0;
}
-- snip.c --
Are there any cases where old-style function declarations really give
you something useful (other than backward compatibility)? (I'll note
in passing that That Other Language seems to manage without them.)

There are, remember that post about two function pointers that take
each other as an argument and call them?
Well, in that topic I thought it's not possible, but I didn't know
about '()' in function pointers.
I cannot look up the post, maybe someone else can do it.

Found it, here is via google:
<http://groups.google.com/group/comp.lang.c/browse_thread/thread/
f93c79699ead2798/bfd2a6651287e716>
Notice mr Walter Robersons example.
int fun1(int (*auxfun)()) { return (auxfun == NULL) ? 5 : 17 * auxfun(NULL); }
int fun2(int (*auxfun)()) { return (auxfun == NULL) ? 42 : 9 - auxfun(NULL); }
int fun3(void) { return fun1(fun2) + fun2(fun1); }
Which is not possible without '()'.
 
J

Jack Klein

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
 
D

DDD

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 ?

Regards,
Somenath

// The parameters in f() is empty, if you want to compiler to check
the passed parameters, you should write a function with void
parameter: f(void)
// void f()
void f(void)
{

}

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

}
try it again.
 
S

somenath

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


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.



Thanks for the replies . But I am confused by your statement
"accepting an unspecified, but fixed,
argument list."
Are you indicating type of the arguments are unspecified but the
number of argument is specified If it is is fixed is it none ?
 
B

Ben Bacarisse

somenath said:
On Jan 31, 9:21 am, Jack Klein <[email protected]> wrote:

Thanks for the replies . But I am confused by your statement
"accepting an unspecified, but fixed,
argument list."
Are you indicating type of the arguments are unspecified but the
number of argument is specified If it is is fixed is it none ?

The term "fixed" is causing you problems so lets avoid it. The
declaration Jack Klein gave states that we don't know how many
arguments f needs. The compiler can not check the call (nore can it
do any type conversion other than what are called "the default
argument promotions") so you are on your own. This does not mean that
call can't be wrong, just that the compiler can't check it!

In particular, the declaration

void f();

does not mean that the function f can be called with a variable number
of arguments (like printf can). All it means is that the compiler
does not know (at this point) how many arguments are needed. To find
out, one must look at the *definition* of f. This may be in another
file. I may not even be available (if f is in a closed source
library) but only the actual definition can tell us how many arguments
f needs. When you find it, you will see something like:

void f() {...} /* f takes no arguments (old form) */
void f(void) {...} /* f takes no arguments (newer form) */
void f(int a) {...} /* f takes a single int */

If the call you make has the wrong number of arguments (as
specified in the *definition*) then the behaviour is undefined.

Note that I left out one option for f. If f is defined like this:

void f(int a, ...) {...}

then the behaviour is undefined. What this means is that one can only
call a function with a variable argument list if a prototype
declaration for it is in scope. This is why Jack Klein said "fixed".
The declaration 'void f();' does not say *how* many arguments must be
passed but it *does* prevent there being a variable number of them.
We don't know how many, but it is some "fixed" number.

All of this is rather esoteric since (except in the oddest of
situations) one would specify the number and type of a function's
arguments by writing a full function prototype.
 
C

christian.bau

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.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top