tags:

views:

731

answers:

4

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?

+8  A: 

No, functions do not always need a prototype. The only requirement is that a function be "declared" before you use it. There are two ways to declare a function: to write a prototype, or to write the function itself (called a "definition.") A definition is always a declaration, but not all declarations are definitions.

In C99, you're correct. In C89/C90, you did not need to pre-declare a function; it would be implicitly declared as function taking undefined list of arguments and returning int simply by being used as a function.
Jonathan Leffler
This distinction between C99 and pre-C99 standards can be significant, as evidenced in this comp.lang.c FAQ question: http://c-faq.com/malloc/mallocnocast.html
nagul
+1  A: 

To the best of my knowledge (in ANSI C89/ISO C90), no. I am unsure about C99; however, I would expect the same.

Personal Note: I only write function prototypes when...

  1. I need to (when A() calls B() and B() calls A()), or
  2. I am exporting the function; otherwise, it feels superfluous.
Anthony Cuozzo
+2  A: 

Yes, every function must have a prototype, but that prototype may appear either in a separate declaration or as part of the function's definition. Function definitions written in C89 and up naturally have prototypes, but if you write things in classic K&R style, thus:

main (argc, argv)

  int argc;
  char **argv;

{
  ...
}

then the function definition has no prototype. If you write ANSI C (C89) style, thus:

main (int argc, char **argv) { ... }

then the function definition has a prototype.

Norman Ramsey
+8  A: 

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 an int do not have to be declared, and functions that return something other than an int do 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 char or short - both of which are converted to int; more seriously, perhaps, a function that takes a float instead of a double), 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;' (an int by default) and also implicit function declarations.

Note that if a function is file 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).

Jonathan Leffler