To be truly standards-compliant, must all functions in C (except for main) have a prototype, even if they are only used after their definition in the same translation unit?
To be truly standards-compliant, must all functions in C (except for main) have a
Share
It depends on what you mean by ‘truly standards compliant’. However, the short answer is ‘it is a good idea to ensure that all functions have a prototype in scope before being used’.
A more qualified answer notes that if the function accepts variable arguments (notably the
printf()family of functions), then a prototype must be in scope to be strictly standards compliant. This is true of C89 (from ANSI) and C90 (from ISO; the same as C89 except for the section numbering). Other than ‘varargs’ functions, though, functions which return anintdo not have to be declared, and functions that return something other than anintdo need a declaration that shows the return type but do not need the prototype for the argument list.Note, however, that if the function takes arguments that are subject to ‘normal promotions’ in the absence of prototypes (for example, a function that takes a
charorshort– both of which are converted toint; more seriously, perhaps, a function that takes afloatinstead of adouble), then a prototype is needed. The standard was lax about this to allow old C code to compile under standard conformant compilers; older code was not written to worry about ensuring that functions were declared before use – and by definition, older code did not use prototypes since they did not become available in C until there was a standard.C99 disallows ‘implicit int’…that means both oddball cases like ‘
static a;‘ (anintby default) and also implicit function declarations. These are mentioned (along with about 50 other major changes) in the foreword to ISO/IEC 9899:1999, which compares that standard to the previous versions:In ISO/IEC 9899:1990, §6.3.2.2 Function calls stated:
This paragraph is missing in the 1999 standard. I’ve not (yet) tracked the change in verbiage that allows
static a;in C90 and disallows it (requiringstatic int a;) in C99.Note that if a function is static, it may be defined before it is used, and need not be preceded by a declaration. GCC can be persuaded to witter if a non-static function is defined without a declaration preceding it (
-Wmissing-prototypes).