In the C language, function prototypes are declarations of functions that inform the compiler about the return type, name, and parameter types of the function before its actual implementation. Prototypes are typically placed in header files (.h). Their use allows:
Example of a prototype:
// math_utils.h int sum(int a, int b); // Function prototype
// main.c #include "math_utils.h" int main() { int result = sum(3, 4); // the compiler knows the signature of sum }
Without a prototype, the function would be treated as returning int and taking an indefinite number of arguments, which could lead to unexpected runtime errors.
Question: Can you call a function in C before its definition if it is not declared as a prototype?
Answer: In the C89 standard, it was allowed to call functions before their definition if the return value is int, and parameters were not checked (implicit int, implicit promotion). In modern standards, this leads to warnings or errors, and such an approach should not be used.
Example of an error:
int main() { foo(1, 2); // No prototype for foo } int foo(double x, double y) { ... }
The compiler will call the function, assuming the parameters as int, whereas the signature implies double — result: UB or incorrect values.
Story
In a large scientific project, one of the modules lacked prototypes for data processing functions. When passing
floatinstead ofint, errors were detected only after incorrect computations during operation, although compilation passed without errors.
Story
In a modular build automation utility, functions were defined only in
.cfiles, without declarations in headers. Two modules had functions with the same name and incompatible parameters — leading to a hard-to-trace linking error.
Story
In an embedded system project, a problem arose: the initialization function was called before its definition without a prototype. Due to the compiler's assumptions about parameter and return types, logic was severely disrupted, and the system malfunctioned only on certain builds with different memory organization.