Variable declaration and definition

梦想的初衷 提交于 2019-12-19 04:15:14

问题


int x;

Is this a declaration or a definition?

As I write the following code,

#include <stdio.h>

int main(void)
{
    int x;
    printf("%p",&x);
    return 0;
}

it prints some address. So as memory is allocated, int x; can't be just a declaration. So is it a definition?


回答1:


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).




回答2:


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



来源:https://stackoverflow.com/questions/4012724/variable-declaration-and-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!