You may use several compatible function declarations if among them there is no more than one function definition.
In C there is defined the notion of compatible types.
From the C11 Standard (6.2.7 Compatible type and composite type)
1 Two types have compatible type if their types are the same.
Additional rules for determining whether two types are compatible are
described in 6.7.2 for type specifiers, in 6.7.3 for type qualifiers,
and in 6.7.6 for declarators.
and (6.7 Declarations)
4 All declarations in the same scope that refer to the same object or
function shall specify compatible types.
and at last (6.7.6.3 Function declarators (including prototypes))
- ... If one type has a parameter type list and the other type
is specified by a function declarator that is not part of a function
definition and that contains an empty identifier list, the parameter
list shall not have an ellipsis terminator and the type of each
parameter shall be compatible with the type that results from the
application of the default argument promotions.
In the both these function declarations
int hi(int);
int hi();
the return types are compatible (they are the same). And the parameter of the first function declaration has the type int
that corresponds to the type of the integer promotions.
From the C11 Standard 6.5.2.2 Function calls)
6 If the expression that denotes the called function has a type that
does not include a prototype, the integer promotions are performed on
each argument, and arguments that have type float are promoted to
double. These are called the default argument promotions.
If you would write for example
int hi(char);
int hi();
then there are declared two different functions because the type char
, the type of the parameter of the first function, does not correspond to a type after default argument promotions.
Another example of declaring two compatible functions and two different functions.
These two declarations declare the same function
int hi( double );
int hi();
and these two declarations declare different (non-compatible) functions
int hi( float );
int hi();
In C23 Standard there are done the following changes relative to function declarations
— mandated function declarations whose parameter list is empty be
treated the same as a parameter list which only contain a single void;
So according to the C23 Standard these function declarations
int hi(int);
int hi();
are equivalent to
int hi(int);
int hi( void );
So they declare two different functions and the compiler should issue a message.
int hi();
declaration will be like C++ and equivalent toint hi(void);
, which will indeed contradictint hi(int);
. Until then, this analysis is sound. – Digitalis