变量声明和定义 [英] Variable declaration and definition

查看:111
本文介绍了变量声明和定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  INT X;

这是声明还是定义?<​​/ P>

在我写以下code,

 的#include&LT;&stdio.h中GT;INT主要(无效)
{
    INT X;
    的printf(%p,&安培; X);
    返回0;
}

它打印一些地址。因此,作为内存分配, INT X; 不能只是​​一个声明。因此,它是一个定义?


解决方案

从的 C标准(n1256)


6.7声明的结果
...结果
5 A声明指定间pretation和一组标识符的属性。 A 定义的标识符是该标识符的声明:
搜索结果
- 一个对象,使存储到保留该对象;
结果
- 函数,包括函数体; 101)结果
- 一个枚举常量或typedef名称,是的(唯一)的声明
标识符。

在这种情况下, INT X; 是一个定义(或定义声明)。

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

这篇关于变量声明和定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆