Declaration vs definition in C

前端 未结 4 962
余生分开走
余生分开走 2020-12-20 00:06

Consider the code:

int main(void)
{
    int a;
}

As far as I know, int a; is a definition, as it causes storage to be reserved

4条回答
  •  误落风尘
    2020-12-20 00:17

    A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:

    extern int bar;
    extern int g(int, int);
    

    A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

    int bar;
    int g(int lhs, int rhs) {return lhs*rhs;}
    

提交回复
热议问题