liveBook · Manning

Prototype

As explained in Modern C, Third Edition

A function prototype is a declaration that specifies a function's name, return type, and parameter types without providing its body.

`bool leapyear(unsigned year);` declares a function without defining its body. Its prototype specifies a return type, `bool`, and a parameter type, `unsigned`. The parameter name may be omitted, as in `extern bool leapyear(unsigned);`; `extern` is optional here. A definition supplies the body as well as the prototype. section 7.1

In a declaration or definition, parentheses enclose parameter declarations; in a call, they enclose arguments. A function taking no parameters uses `(void)`. A function returning no value uses `void` as its return type. These are different positions with different meanings. section 7.1

extern double fbar(double);

...
double fbar2 = fbar(2)/2;

The declaration lets the caller convert the integer argument `2` to `double` before calling `fbar`. Because the return type is also `double`, `fbar(2)/2` uses floating-point division. section 7.1

TAKEAWAY: A prototype determines how a function can be called. Standard-library declarations provide the same information: for example, `<string.h>` declares string operations such as `strlen` and `strcmp`. Their parameter types distinguish operations on strings from operations on character arrays more generally. section 7.4 section 6.1.5

`main` is a special case: its prototype is constrained by the C standard, though the programmer defines it. For portable programs, use `int main(void)` or `int main(int argc, char* argv[argc+1])`. The exit status returned from `main` is passed to the runtime system; `EXIT_SUCCESS` and `EXIT_FAILURE` are the portable status choices. By contrast, `[[noreturn]] void exit(int status);` declares a function that does not return to its caller. section 7.2

How Modern C, Third Edition covers it

How deep it goes
Explains declaration syntax, optional parameter names, void conventions, and call-site conversions; defers details of `extern`.
Examples
Uses `leapyear` declarations, a `fbar(2)/2` call, and portable `main` prototypes.
What it assumes
Assumes prior exposure to functions and their calls while explaining what declaration parentheses and type lists mean.
What you can do after
Apply it to declare callable functions and reason about argument conversion, return types, and portable `main` signatures.
How it connects
Connects function declarations and definitions to argument conversion, return types, standard-library functions, and program termination.

Also explained in

Compare how 2 books explain prototype →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.