Search code examples
clanguage-lawyerdeclarationfunction-prototypes

Declaration and prototype difference


What is the difference between declaration and prototype in C? In which situations they are called declarations and in which prototypes?


Solution

  • TL;DR; All prototypes are declarations, but not all declarations are prototypes.

    Declaration is the generic terminology used in the standards, prototype is more specific.

    Quoting C11, chapter §6.7

    A declaration specifies the interpretation and attributes of a set of identifiers. [...]

    and from §6.7.6,

    Each declarator declares one identifier, and asserts that when an operand of the same form as the declarator appears in an expression, it designates a function or object with the scope, storage duration, and type indicated by the declaration specifiers.

    On the other hand, from chapter §6.2.1

    [....] A function prototype is a declaration of a function that declares the types of its parameters.

    So, one liner, prototype is more complete form (including types of parameter) of declaration.


    About "identifier": chapter §6.4.2.1,

    An identifier is a sequence of nondigit characters (including the underscore _, the lowercase and uppercase Latin letters, and other characters) and digits, which designates one or more entities as described in 6.2.1. [...]

    and in chapter §6.2.1,

    An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter. [....]