liveBook · Manning

Prototype

As explained in Modern C

A function prototype is a declaration that specifies a function's parameter types and return type.

A function prototype declares the parameter types and return type that determine how a C function may be called. For example, `bool leapyear(unsigned year);` tells the compiler that `leapyear` receives an `unsigned` argument and returns `bool`, without providing its definition. section 7.1

bool leapyear(unsigned year);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":14},{\"line\":0,\"ch\":22}]]"}
!@%STYLE%@!

Read the declaration from left to right: the return type is `bool`, the function is `leapyear`, and its parameter has type `unsigned`; the semicolon marks a declaration rather than a definition. section 7.1

TAKEAWAY: prototypes make calls type-aware.

  • A declaration may omit parameter names, and `extern` may be added; the compiler still needs the argument types and return type. section 7.1
  • Use `void` as the parameter list for a function that accepts no arguments, or as the return type for a function that returns no value. section 7.1
  • At a call such as `fbar(2)`, a visible prototype tells the compiler that `fbar` expects `double`; the `signed int` argument can therefore be converted to `2.0`, and the caller knows the result is `double`. section 7.1
  • Treat `main` as a special case: portable programs use one of its two official prototypes, return `EXIT_SUCCESS` or `EXIT_FAILURE`, and may omit an explicit return at the end. section 7.2

Declaration versus definition

The definition `bool leapyear(unsigned year) { ... }` supplies the function body. The separate declaration `bool leapyear(unsigned year);` supplies the calling interface. Both agree on the parameter type and return type, but only the definition contains executable behavior. section 7.1

How Modern C covers it

How deep it goes
Explains declaration syntax, void conventions, conversions, and special main forms without descending into linkage details.
Examples
Uses leapyear declarations, fbar argument conversion, main prototypes, and the exit declaration.
What you can do after
Evaluate function interfaces and anticipate compiler checking and conversions at calls.
How it connects
Connects function declarations, function definitions, function calls, parameter types, return types, void, main, and exit.

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. The section links point to the parts of the book it draws on.