what is the difference, void func(void) and void fucn()

Discussion in 'C Programming' started by noblesantosh@yahoo.com, Jul 22, 2005.

  1. Guest

    Hi all,
    What is the difference between following two function definations?
    <1>
    void func(void)
    {
    /* some code */
    }

    <2>
    void func()
    {
    /* some code */
    }

    Why would somebody use 2nd one?

    --santosh.
    , Jul 22, 2005
    #1
    1. Advertising

  2. S.Tobias Guest

    wrote:
    > Hi all,
    > What is the difference between following two function definations?
    > <1>
    > void func(void)
    > {
    > /* some code */
    > }
    >
    > <2>
    > void func()
    > {
    > /* some code */
    > }


    Both define a function returning void, and accepting zero arguments.

    They define `func' with different types. The first type is a type
    with a prototype, the second type is without a prototype.

    If in a function call expression the expression designating the
    function has a type without a prototype, and any arguments
    are passed, compiler is not required to diagnose an error,
    but the behaviour is undefined.

    void f();
    f(x); //UB, no diagnostic

    void f(void);
    f(x); //diagnostic

    (It's a bit longer story when a function hasn't a prototype, and
    has arguments - then the number of arguments and types after
    promotion count.)

    > Why would somebody use 2nd one?


    For me it's usually laziness. Maybe there are other reasons,
    I don't know.
    (Note: there may be important reasons to declare a function
    _type_ without a prototype.)

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jul 22, 2005
    #2
    1. Advertising

  3. Guest

    Re: what is the difference, void func(void) and void fucn()

    S.Tobias wrote:
    > wrote:
    > > Hi all,
    > > What is the difference between following two function definations?
    > > <1>
    > > void func(void)
    > > {
    > > /* some code */
    > > }
    > >
    > > <2>
    > > void func()
    > > {
    > > /* some code */
    > > }

    >
    > Both define a function returning void, and accepting zero arguments.
    >
    > They define `func' with different types. The first type is a type
    > with a prototype, the second type is without a prototype.
    >
    > If in a function call expression the expression designating the
    > function has a type without a prototype, and any arguments
    > are passed, compiler is not required to diagnose an error,
    > but the behaviour is undefined.
    >
    > void f();
    > f(x); //UB, no diagnostic
    >
    > void f(void);
    > f(x); //diagnostic
    >
    > (It's a bit longer story when a function hasn't a prototype, and
    > has arguments - then the number of arguments and types after
    > promotion count.)
    >
    > > Why would somebody use 2nd one?

    >
    > For me it's usually laziness. Maybe there are other reasons,
    > I don't know.
    > (Note: there may be important reasons to declare a function
    > _type_ without a prototype.)
    >
    > --
    > Stan Tobias
    > mailx `echo LID | sed s/[[:upper:]]//g`


    Thanks stan.
    and how the following function differs with above two functions

    3>
    void fucn(int a, ...)
    {
    /*some code*/
    }
    Can't we use 2nd as variable number of arguments like 3rd.
    Can you or anybody further explore it?
    --santosh
    , Jul 22, 2005
    #3
  4. In article <> "S.Tobias" <> writes:
    ....
    > If in a function call expression the expression designating the
    > function has a type without a prototype, and any arguments
    > are passed, compiler is not required to diagnose an error,
    > but the behaviour is undefined.
    >
    > void f();
    > f(x); //UB, no diagnostic


    This is only undefined behaviour when the actual function f has not a
    single parameter. "void f();" declares a function f that returns void
    with an unspecified number of parameters of unspecified type. So
    f(x) is undefined behaviour if the actual function does *not* have a
    single parameter.

    > > Why would somebody use 2nd one?


    [ about void f(); ]
    >
    > For me it's usually laziness. Maybe there are other reasons,


    In pre-standard C (i.e. C without prototypes) that was the standard
    way to declare functions.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Jul 22, 2005
    #4
  5. S.Tobias Guest

    Dik T. Winter <> wrote:
    > In article <> "S.Tobias" <> writes:
    > ...


    > > void f();
    > > f(x); //UB, no diagnostic

    >
    > This is only undefined behaviour when the actual function f has not a
    > single parameter. "void f();" declares a function f that returns void
    > with an unspecified number of parameters of unspecified type. So
    > f(x) is undefined behaviour if the actual function does *not* have a
    > single parameter.


    Yes, thanks. I was thinking only about the simple case that
    the OP asked about, and should have written: `void f(){}'.

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jul 22, 2005
    #5
  6. S.Tobias Guest

    Re: what is the difference, void func(void) and void fucn()

    wrote:
    > S.Tobias wrote:
    >> wrote:


    >> > What is the difference between following two function definations?
    >> > <1>
    >> > void func(void)
    >> > {
    >> > /* some code */
    >> > }
    >> >
    >> > <2>
    >> > void func()
    >> > {
    >> > /* some code */
    >> > }

    >>
    >> Both define a function returning void, and accepting zero arguments.
    >>
    >> They define `func' with different types. The first type is a type
    >> with a prototype, the second type is without a prototype.
    >>

    [snip]
    > and how the following function differs with above two functions
    >
    > 3>
    > void fucn(int a, ...)
    > {
    > /*some code*/
    > }
    > Can't we use 2nd as variable number of arguments like 3rd.


    No, the third declares a type wich is not compatible with the second
    case.

    typedef void f_t();

    `f_t' is compatible with:

    void f();
    void f(void);
    void f(int);
    void f(int, int);

    but is not compatible with:

    int f();
    void f(int, ...);

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jul 22, 2005
    #6
    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. Johnny
    Replies:
    3
    Views:
    435
    Robert Kern
    Aug 23, 2005
  2. Hari Sekhon
    Replies:
    0
    Views:
    478
    Hari Sekhon
    Jun 20, 2006
  3. Vinko Vrsalovic

    int func() v/s int func(void)

    Vinko Vrsalovic, Jan 21, 2005, in forum: C Programming
    Replies:
    14
    Views:
    1,276
    Villy Kruse
    Jan 24, 2005
  4. Alex Vinokur
    Replies:
    6
    Views:
    338
    Tor Rustad
    Nov 18, 2006
  5. Replies:
    1
    Views:
    384
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page