This question already has an answer here:
What is better: void foo()
or void foo(void)
?
With void it looks ugly and inconsistent, but I've been told that it is good. Is this true?
Edit: I know some old compilers do weird things, but if I'm using just GCC, is void foo()
Ok? Will foo(bar);
then be accepted?
void foo(void)
is better because it explicitly says: no parameters allowed.void foo()
means you could (under some compilers) send parameters, at least if this is the declaration of your function rather than its definition.That is the correct way to say "no parameters" in C, and it also works in C++.
But:
Means different things in C and C++! In C it means "could take any number of parameters of unknown types", and in C++ it means the same as
foo(void)
.Variable argument list functions are inherently un-typesafe and should be avoided where possible.
In C++, there is no difference in
main()
andmain(void)
.But in C,
main()
will be called with any number of parameters.Example:
main(void)
will be called without any parameters. If we try to pass then this end up leading to a compiler error.Example:
Besides syntactical differences, many people also prefer using
void function(void)
for pracitical reasons:If you're using the search function and want to find the implementation of the function, you can search for
function(void)
, and it will return the prototype as well as the implementation.If you omitted the second
void
, you have to search forfunction()
and will therefore also find all function calls, making it difficulter to find the actual implementation.C99 quotes
This answer aims to quote and explain the relevant parts of the C99 N1256 standard draft.
Definition of declarator
The term declarator will come up a lot, so let's understand it.
From the language grammar, we find that the following underline characters are declarators:
Declarators are part of both function declarations and definitions.
There are 2 types of declarators:
Parameter type list
Declarations look like:
Definitions look like:
It is called parameter type list because we must give the type of each parameter.
Identifier list
Definitions look like:
Declarations look like:
We cannot declare a function with a non-empty identifier list:
because 6.7.5.3 "Function declarators (including prototypes)" says:
It is called identifier list because we only give the identifiers
x
andy
onf(x, y)
, types come after.This is an older method, and shouldn't be used anymore. 6.11.6 Function declarators says:
and the Introduction explains what is an obsolescent feature:
f() vs f(void) for declarations
When you write just:
it is necessarily an identifier list declaration, because 6.7.5 "Declarators" says defines the grammar as:
so only the identifier-list version can be empty because it is optional (
_opt
).direct-declarator
is the only grammar node that defines the parenthesis(...)
part of the declarator.So how do we disambiguate and use the better parameter type list without parameters? 6.7.5.3 Function declarators (including prototypes) says:
So:
is the way.
This is a magic syntax explicitly allowed, since we cannot use a
void
type argument in any other way:What can happen if I use an f() declaration?
Maybe the code will compile just fine: 6.7.5.3 Function declarators (including prototypes):
So you can get away with:
Other times, UB can creep up (and if you are lucky the compiler will tell you), and you will have a hard time figuring out why:
See: Why does an empty declaration work for definitions with int arguments but not for float arguments?
f() and f(void) for definitions
vs
are similar, but not identical.
6.7.5.3 Function declarators (including prototypes) says:
which looks similar to the description of
f(void)
.But still... it seems that:
is conforming undefined behavior, while:
is non conforming as discussed at: Why does gcc allow arguments to be passed to a function defined to be with no arguments?
TODO understand exactly why. Has to do with being a prototype or not. Define prototype.
There are two ways for specifying parameters in C. One is using an identifier list, and the other is using a parameter type list. The identifier list can be omitted, but the type list can not. So, to say that one function takes no arguments in a function definition you do this with an (omitted) identifier list
And this with a parameter type list:
If in a parameter type list the only one parameter type is void (it must have no name then), then that means the function takes no arguments. But those two ways of defining a function have a difference regarding what they declare.
Identifier lists
The first defines that the function takes a specific number of arguments, but neither the count is communicated nor the types of what is needed - as with all function declarations that use identifier lists. So the caller has to know the types and the count precisely before-hand. So if the caller calls the function giving it some argument, the behavior is undefined. The stack could become corrupted for example, because the called function expects a different layout when it gains control.
Using identifier lists in function parameters is deprecated. It was used in old days and is still present in lots of production code. They can cause severe danger because of those argument promotions (if the promoted argument type do not match the parameter type of the function definition, behavior is undefined either!) and are much less safe, of course. So always use the
void
thingy for functions without parameters, in both only-declarations and definitions of functions.Parameter type list
The second one defines that the function takes zero arguments and also communicates that - like with all cases where the function is declared using a parameter type list, which is called a
prototype
. If the caller calls the function and gives it some argument, that is an error and the compiler spits out an appropriate error.The second way of declaring a function has plenty of benefits. One of course is that amount and types of parameters are checked. Another difference is that because the compiler knows the parameter types, it can apply implicit conversions of the arguments to the type of the parameters. If no parameter type list is present, that can't be done, and arguments are converted to promoted types (that is called the default argument promotion).
char
will becomeint
, for example, whilefloat
will becomedouble
.Composite type for functions
By the way, if a file contains both an omitted identifier list and a parameter type list, the parameter type list "wins". The type of the function at the end contains a prototype:
That is because both declarations do not say anything contradictory. The second, however, had something to say in addition. Which is that one argument is accepted. The same can be done in reverse
The first defines a function using an identifier list, while the second then provides a prototype for it, using a declaration containing a parameter type list.