I was wondering what would happen if I left out the parameter declarations in a K&R function definition, so I tried the following:
#include <stdio.h>
void f(a)
// int a; <--- omitting `declaration-list` causes warning in gcc, but nothing in clang
{
printf("%d", 9);
}
int main()
{
f(9);
return 0;
}
When I compile this with gcc 11
, a diagnostic is issued in the form of a warning (i.e. warning: type of 'a' defaults to 'int'
). However, when I compile it with clang 14
, no diagnostic message is issued.
This confused me because in the Standard, omitting the declaration-list
(which contains the parameter declarations) is a constraint violation (as per C11 6.9.1(6)), which requires a conforming implementation to issue a diagnostic (as per C11 5.1.1.3). So, why didn't clang issue a warning? Have I misinterpreted something?
EDIT: Must a diagnostic be issued if a constraint violation can be "fixed" by the compiler applying some other provision in the Standard?
For example, the empty declaration list in the code above has always been a constraint violation (since C89). However, prior to C99, the Standard also had an implicit int
rule (see C90 6.5.2), which would have allowed the compiler to assume any undeclared parameters as being of type int
and thereby recover from the no-empty-declaration-list constraint violation.
So, would a pre-1999 ANSI-conformant compiler have still issued a diagnostic in this case? My first impression was that it would have since the constraint would have taken precedence over the implicit int
rule, but pg. 287 of Harbison and Steele says (emphasis added):
In the pre-Standard traditional form, the parameter names are listed in the declarator and the types are specified (in any order) in the declaration-list opt following the declarator. All parameters should be declared in the declaration-list, but prior to C99 omitted parameter declarations defaulted to type int.
The bit in bold makes it seem like empty declaration lists were allowed since the implicit int
rule would have "fixed" any no-empty-declaration-list constraint violations.