int add()
is obsolete style and means accept any parameter (unlike C++ where it means (void)
). It's a remain from the old pre-standard "K&R" style where parameters were specified separately. C still allows this form (for now) for function declarators only.
6.11.6 Function declarators
The use of function declarators with empty parentheses (not prototype-format parameter
type declarators) is an obsolescent feature.
6.11.7 Function definitions
The use of function definitions with separate parameter identifier and declaration lists
(not prototype-format parameter type and identifier declarators) is an obsolescent feature.
When it comes to function definitions with an empty list, the following applies (C11 6.7.6.3/14):
Constraints
/--/
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.
So the code is a constraint violation, it is not valid C. You have a function definition but it apparently gets treated as if it had parameters. gcc specifically appears to be non-compliant here, since I get no diagnostic message even with max warnings and -pedantic.
As for why "it works", see What must a C compiler do when it finds an error? .