void func()
In practice, an empty parameter means any argument is accepted.
void func(void)
accepts no argument.
But in Standard C99, I find such lines:
6.7.5.3 Function declarators (including prototypes)
14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.
according to the standard, func()
and func(void)
is the same?
TL;DR
In declarations,
the behaviour is quite different. The first one declares a function without any prototype - and it may take any number of arguments! Whereas the latter declares a function with a prototype, that has no parameters and accepts no arguments.
In definitions
and
The former declares and defines a function
func1
that has no parameters and no prototypeThe latter declares and defines a function
func2
with a prototype that has no parameters.These two behave distinctly in that whereas the C compiler must print a diagnostic message when calling a prototyped function with wrong number of arguments, it needn't do so when calling a function without prototype.
I.e, given the definitions above
However both calls are illegal in strictly-conforming programs as they're explicitly undefined behaviour as per 6.5.2.2p6.
Furthermore, the empty parentheses are considered an obsolescent feature:
and
In detail
There are 2 related, yet distinct concepts: parameters and arguments.
arguments are the values passed into the function.
parameters are the names/variables within the function that are set to the values of the arguments when the function entered
In the following excerpt:
n
andc
are parameters.42
andch
are arguments.The quoted excerpt only concerns the parameters of a function, but doesn't mention anything about the prototype or arguments to the function.
The declaration
void func1()
means that the functionfunc1
can be called with any number of arguments, i.e. no information about the number of arguments is specified (as a separate declaration, C99 specifies this as "function with no parameter specification), whereas the declarationvoid func2(void)
means that the functionfunc2
does not accept any arguments at all.The quote in your question means that within a function definition,
void func1()
andvoid func2(void)
both signal them that there are no parameters, i.e. variable names that are set to the values of the arguments when the function is entered. Thevoid func() {}
contrasts withvoid func();
the former declares thatfunc
indeed takes no parameters, whereas the latter is a declaration for a functionfunc
for which neither parameters nor their types are specified (a declaration without prototype).However, they yet differ definition-wise in that
The definition
void func1() {}
doesn't declare a prototype, whereasvoid func2(void) {}
does, because()
is not a parameter type list, whereas(void)
is a parameter type list (6.7.5.3.10):and further 6.9.1.7
The declarator of function definition for
func1
does not contain a parameter type list, and thus the function then doesn't have a prototype.void func1() { ... }
can still be called with any number of arguments, whereas it is a compile-time error to callvoid func2(void) { ... }
with any arguments (6.5.2.2):(emphasis mine)
This is a constraint, which according of the standard says that a conforming implementation must display at least one diagnostic message about this problem. But since
func1
doesn't have a prototype, a conforming implementation is not required to produce any diagnostics.However, if the number of arguments does not equal the number of parameters, the behaviour is undefined 6.5.2.2p6:
So in theory a conforming C99 compiler is also allowed to error or diagnose a warning in this case. StoryTeller provided evidence that clang might diagnose this; however, my GCC doesn't seem to do it (and this might also be required for it to be compatible with some old obscure code too):
When the above program is compiled with
gcc -std=c99 test.c -Wall -Werror
, the output is:That is, the arguments are not checked at all against the parameters of a function whose declaration in definition is not prototyped (
test
) whereas GCC considers it as a compile-time error to specify any arguments to a prototyped function (test2
); any conforming implementation must diagnose this as it is a constraint violation.The significant part of the quote is highlighted in bold below:
So, when the parameter list is empty for a function with its body, they are the same. But of it is just a declaration of a function.
The empty parameter list inside a function definition means that it does not include a prototype nor has any parameters.
C11 §6.9.1/7 Function definitions (emphasis in ongoing quotes is mine)
The question asks:
No. The essential difference between
void func()
andvoid func(void)
lies in their calls.C11 §6.5.2.2/2 Function calls (within constraints section):
Notice that parameters ≠ arguments. The function may contain no parameters, but it may have multiple arguments.
As function defined with empty parameters does not introduce a prototype, it's not checked against its calls, so in theory it may be supplied with whatever number of arguments.
However, it is technically an undefined behavior to call such function with at least one argument (see Antti Haapala's comments).
C11 §6.5.2.2/6 Function calls (within semantics section):
Hence, the difference is subtle:
void
, it won't compile when number of arguments don't match with parameters (along with their types), because of constaint violation (§6.5.2.2/2). Such situation requires diagnostic message from conforming compiler.Example:
Note that optimizing compiler may cut off the arguments in such case. For instance, this is how Clang compiles the above code (excluding
func1
's call) with-01
on x86-64 according to the SysV ABI calling conventions:No.
func(void)
says the function takes no arguments at all; whereasfunc()
says the function takes an unspecified number of arguments. Both are valid but thefunc()
style are obsolete and shouldn't be used.This is an artifact from pre-standard C. C99 marked this as obsolete.
6.11.6 Function declarators:
As of C11, it still remains as obsolescent and hasn't been removed from the standard.