Function with unspecified number of arguments

Discussion in 'C Programming' started by Morris Dovey, Jan 30, 2008.

  1. Morris Dovey

    Morris Dovey Guest

    somenath wrote:

    > 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.

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto
     
    Morris Dovey, Jan 30, 2008
    #1
    1. Advertising

  2. Morris Dovey

    somenath Guest

    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
     
    somenath, Jan 30, 2008
    #2
    1. Advertising

  3. Morris Dovey

    Morris Dovey Guest

    Mark Bluemel wrote:
    >
    > Morris Dovey wrote:
    > > somenath wrote:
    > >
    > >> 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.
    > >

    > 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.

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto
     
    Morris Dovey, Jan 30, 2008
    #3
  4. Morris Dovey

    Mark Bluemel Guest

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


    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...
     
    Mark Bluemel, Jan 30, 2008
    #4
  5. Morris Dovey

    Guest

    On Jan 30, 4:34 pm, somenath <> 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 ?


    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 --
     
    , Jan 30, 2008
    #5
  6. Morris Dovey

    Mark Bluemel Guest

    Morris Dovey wrote:
    > somenath wrote:
    >
    >> 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.
    >

    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.
     
    Mark Bluemel, Jan 30, 2008
    #6
  7. somenath <> writes:

    > 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.

    > return 0;
    > }


    --
    Ben.
     
    Ben Bacarisse, Jan 30, 2008
    #7
  8. writes:
    > On Jan 30, 4:34 pm, somenath <> wrote:
    >> 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.)

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 30, 2008
    #8
  9. Morris Dovey

    Guest

    On Jan 30, 7:28 pm, Keith Thompson <> wrote:
    > writes:
    > > On Jan 30, 4:34 pm, somenath <> wrote:
    > >> 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.
     
    , Jan 30, 2008
    #9
  10. Morris Dovey

    Guest

    On Jan 30, 9:13 pm, wrote:
    > On Jan 30, 7:28 pm, Keith Thompson <> wrote:
    >
    > > writes:
    > > > On Jan 30, 4:34 pm, somenath <> wrote:
    > > >> 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 '()'.
     
    , Jan 30, 2008
    #10
  11. Morris Dovey

    Jack Klein Guest

    On Wed, 30 Jan 2008 06:34:18 -0800 (PST), somenath
    <> 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
     
    Jack Klein, Jan 31, 2008
    #11
  12. Morris Dovey

    DDD Guest

    On Jan 30, 10:34 pm, somenath <> 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 ?
    >
    > 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.
     
    DDD, Jan 31, 2008
    #12
  13. Morris Dovey

    somenath Guest

    On Jan 31, 9:21 am, Jack Klein <> wrote:
    > On Wed, 30 Jan 2008 06:34:18 -0800 (PST), somenath
    > <> 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.




    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 ?

    >
    > 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.
    >
     
    somenath, Jan 31, 2008
    #13
  14. somenath <> writes:

    > On Jan 31, 9:21 am, Jack Klein <> wrote:

    <snip>
    >> 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 ?


    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.

    --
    Ben.
     
    Ben Bacarisse, Jan 31, 2008
    #14
  15. On Jan 30, 2:34 pm, somenath <> 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.
     
    christian.bau, Jan 31, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jmborr
    Replies:
    1
    Views:
    455
    Stargaming
    Nov 3, 2007
  2. Markus Dehmann
    Replies:
    4
    Views:
    420
    Markus Dehmann
    Dec 7, 2007
  3. Replies:
    3
    Views:
    863
  4. Eric Sosman
    Replies:
    3
    Views:
    678
    Eric Sosman
    May 4, 2009
  5. oldyork90
    Replies:
    10
    Views:
    369
    Jorge
    Sep 27, 2008
Loading...

Share This Page