Variable declaration and definition

前端 未结 2 786
醉酒成梦
醉酒成梦 2021-01-06 18:56
int x;

Is this a declaration or a definition?

As I write the following code,

#include 

int main(void)
{
            


        
相关标签:
2条回答
  • 2021-01-06 19:24

    int x; is a definition. extern int x; is just a declaration. extern int x = 3; is also a definition. HTH

    0 讨论(0)
  • 2021-01-06 19:39

    From the C standard (n1256):

    6.7 Declarations
    ...
    5 A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

    — for an object, causes storage to be reserved for that object;
    — for a function, includes the function body;101)
    — for an enumeration constant or typedef name, is the (only) declaration of the identifier.

    In this case, int x; is a definition (or a defining declaration).

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