开发者

Variable declaration and definition

开发者 https://www.devze.com 2023-01-22 07:39 出处:网络
int x; Is this a declaration or a definition? As I write the following code, #include <stdio.h> int main(void)
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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消