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

N

noblesantosh

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

S.Tobias

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

noblesantosh

S.Tobias said:
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.)


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

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
 
D

Dik T. Winter

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

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

S.Tobias

Dik T. Winter said:
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(){}'.
 
S

S.Tobias

S.Tobias said:
(e-mail address removed) wrote:

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, ...);
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top