Declaration and prototype difference

前端 未结 4 2146
盖世英雄少女心
盖世英雄少女心 2021-02-07 11:45

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

相关标签:
4条回答
  • 2021-02-07 12:25

    According to the C Standard (6.2.1 Scopes of identifiers)

    1. ...(A function prototype is a declaration of a function that declares the types of its parameters.)

    That is a prototype provides the information about the types of the function parameters.

    Consider for example

    void f();
    

    and

    void f( void );
    

    The first declaration is not a prototype because there is nothing known about the function parameters.

    The second declaration is a prototype because it provides a type list of the function parameters (it is a special kind of the type list specifies that the function has no parameters).

    0 讨论(0)
  • 2021-02-07 12:28

    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. [....]

    0 讨论(0)
  • 2021-02-07 12:37
    • A function declaration is any form of line declaring a function and ending with ;.

    • A prototype is a function declaration where all types of the parameters are specified.

    Example, prototype function declaration: void func (void);
    Example, non-prototype function declaration: void func ();.

    Non-prototype function declarations is an obsolescent feature (6.11.6) that may get removed from the C language. Therefore you should always use prototype format and never anything else.

    0 讨论(0)
  • 2021-02-07 12:49

    A declaration introduces a name:

    int a;    // "a" has type int
    

    A function declaration (that is not also a prototype) only introduces the name of a function and its return type:

    int f();   // a function call expression "f(x, y, ...)" has type int
    

    However, such a function declaration does not specify which arguments are valid for the function call; in other words, it does not specify the function signature. This is done by a function prototype (which is a kind of declaration):

    int g1(void);         // "g1" takes no arguments, "g()" has type int
    int g2(float, char);  // "g2" takes two arguments
    int g3(int, ...);     // "g3" takes at least one argument
    

    Whether a function prototype is visible at the time of a function call has important consequences on the call arguments: If no prototype is visible, all arguments undergo default argument promotions. By contrast, if a prototype is available, function arguments are converted to the types of the corresponding formal parameters (and the program is ill-formed if the number of arguments doesn't match or if any of the conversions would be ill-formed).

    To explore this further, observe that there are certain "impossible" combinations: If we had a declaration int g2(); and a definition int g2(float, char) { return 0; }, it would never be possible to call g2 just with the declaration, because the formal parameter types cannot result from default argument promotions. It is generally advisable to always use prototype declarations and never use non-prototype declarations.

    Finally, it is possible to call a prototyped function with more arguments than it has parameters, if the prototype ends in the ellipsis (...). The use of <stdarg.h> to get at those arguments requires that a function prototype is visible at the time of call for such a variable-argument function.

    0 讨论(0)
提交回复
热议问题