Variable declaration vs definition

前端 未结 3 1649
深忆病人
深忆病人 2020-12-04 19:55

I was reading some info on externs. Now the author started mentioning variable declaration and definition. By declaration he referred to case when: if a variable is declared

3条回答
  •  既然无缘
    2020-12-04 20:46

    Basically, yes you are right.

    extern int x;  // declares x, without defining it
    
    extern int x = 42;  // not frequent, declares AND defines it
    
    int x;  // at block scope, declares and defines x
    
    int x = 42;  // at file scope, declares and defines x
    
    int x;  // at file scope, declares and "tentatively" defines x
    

    As written in C Standard, a declaration specifies the interpretation and attributes of a set of identifiers and a definition for an object, causes storage to be reserved for that object. Also a definition of an identifier is a declaration for that identifier.

提交回复
热议问题