difference between void foo(void) and void foo() 
Author Message
 difference between void foo(void) and void foo()

The only copy of the C standard I have is WG14/N843 of Aug, 1998...

What is the difference between

void foo(void) {...}    - foo is a function with no parameters returning
void

and

void foo() {...}        - foo is a function with no parameter specification
returning void

As far as I can tell from the grammar presented in Section 6.7.5 of the
document I referenced above, it seems that a paramter list must at least
contain an identifier. Does this mean that void foo() {...} is
non-standard? It would seem so.

        david

--
fortran was the language of choice for the same reason that three-legged
races are popular.
        -- Ken Thompson, "Reflections on Trusting Trust"



Sun, 27 Apr 2003 03:00:00 GMT  
 difference between void foo(void) and void foo()

Quote:

> What is the difference between

> void foo(void) {...}       - foo is a function with no parameters returning
> void

> and

> void foo() {...}   - foo is a function with no parameter specification
> returning void

In a function definition, nothing. Either is declared to take no
parameters. If you call them with parameters, that is, IIRC, a
constraint violation.
If these were declarations, but _not_ definitions (i.e., if the {...}
were replaced by a ;), the second would not be declared to take any
parameters. You could define it elsewhere to take parameters and call it
with parameters; there is no way for the compiler to tell from this
declaration alone how many it takes. Note that calling such a function
with the wrong number of parameters is undefined behaviour, i.e., if you
do, truly anything may happen.
If the first were a declaration, but not a definition, nothing would
change in the number of parameters.
Note that, while the second is not illegal, it is rather bad practice.
Prototypes were made to be used for your security and clarification; use
them.

Richard



Sun, 27 Apr 2003 03:00:00 GMT  
 difference between void foo(void) and void foo()

Quote:

> Note that, while the second is not illegal, it is rather bad practice.
> Prototypes were made to be used for your security and clarification; use
> them.

Okay, but as a definition, would you still consider it bad practice to
leave out void? As you say, in a definition there is no difference, and
if it is a definition, then it is clear that the function takes no
arguments. Perhaps this is more a matter of style than of good or bad
practice (if such a distinction can be made here).

        david

--
FORTRAN was the language of choice for the same reason that three-legged
races are popular.
        -- Ken Thompson, "Reflections on Trusting Trust"



Sun, 27 Apr 2003 03:00:00 GMT  
 difference between void foo(void) and void foo()

Quote:
>What is the difference between

>void foo(void) {...}        - foo is a function with no parameters returning
>void

>and

>void foo() {...}    - foo is a function with no parameter specification
>returning void

We have discussed this issue at length about 10 days ago, in the context
of the main function (I mean, the parameter list, not the return type :-)

As definitions, they are identical, foo is defined as a function with no
parameters.  But a definition can also serve as declaration (if there is
no previous declaration for that function in scope).  And here we have a
difference: the first example declares foo as function with no parameters,
while the second one declares it as a function with a fixed, but
unspecified, number of parameters.  The first one enables consistency
checks between foo calls following the foo definition and the foo
definition.  The second one doesn't, i.e.

    void foo() {...}

    int main()
    {
        foo(1, 2, 3);
        return 0;
    }

does not require a diagnostic.

It's fine to omit void in the definition of main, because this function
is not normally (explicitly) called anywhere in the program, but it's
better to put it in the definition of other functions taking no
parameters, for the reason explained above.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 27 Apr 2003 03:00:00 GMT  
 difference between void foo(void) and void foo()

Quote:


> > Note that, while the second is not illegal, it is rather bad practice.
> > Prototypes were made to be used for your security and clarification; use
> > them.

> Okay, but as a definition, would you still consider it bad practice to
> leave out void?

Yes; a definition is always a declaration as well, so you could do
something like

int func1()
{
  return 4;

Quote:
}

int func2(a, b)
{
  return func1(a, b, 3)*2;

Quote:
}

and it would not require a diagnostic, but rather would invoke undefined
behaviour. _With_ the void, a diagnostic is required.

Besides, if you do include the void, you make it clear that you know
what you are doing.

Richard



Sun, 27 Apr 2003 03:00:00 GMT  
 difference between void foo(void) and void foo()

Quote:


> > What is the difference between

> > void foo(void) {...}  - foo is a function with no parameters returning
> > void

> > and

> > void foo() {...}      - foo is a function with no parameter specification
> > returning void

> In a function definition, nothing. Either is declared to take no
> parameters. If you call them with parameters, that is, IIRC, a
> constraint violation.
> If these were declarations, but _not_ definitions (i.e., if the {...}

Nope. This comes up every so often, with the same mistake repeated by
some regular posters every time.

void foo() {
    dosomething();

Quote:
}

foo(1,2);

is not a constraint violation (barring any other declarations of 'foo'),
since the former is not a prototype. In this respect, a definition
behaves just like a declaration - in fact - this definition *is* a
declaration.

--
Joe



Sun, 27 Apr 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. (foo == (foo_t *)NULL) vs. ((void *)foo == NULL)

2. what's the difference between void main(void) and int main(void)

3. from void (void*) to void (_cdecl*) (void*)

4. difference between void and <bold>void

5. calling a function with (void)foo()

6. sizeof(void*) versus sizeof(foo*)

7. void foo(const int a[2][3])

8. void foo() not a function?

9. Type of void foo(const int) ?

10. What's the difference: int main(void) VS void main(void)?

11. typedef int COMPARE(const void *, const void *);

12. void * & void

 

 
Powered by phpBB® Forum Software