变量声明VS定义 [英] Variable declaration vs definition

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

问题描述

我正在读的实习医生一些信息。
现在,笔者开始提的变量声明和定义。
通过声明,他提到何时案例:如果一个变量声明
为它的空间没有被分配。
现在,这给我带来了混乱,因为我觉得大部分的时间
当我用C使用变量,我其实都定义,并宣布他们的权利?
即,

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 the space for it is not allocated. Now this brought me to confusion, because I think MOST of the times when I use variables in C, I am actually both defining and declaring them right? i.e.,

int x; // definition + declaration(at least the space gets allocated for it)

我觉得那么的用C情况下,当你声明变量,但不
定义它是当你使用:

I think then that only cases in C when you declare the variable but not define it is when you use:

extern int x; // only declaration, no space allocated

为什么我得到它的权利?

did I get it right?

推荐答案

基本上,是的,你是对的。

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

由于C语言编写的标准,申报的指定间pretation和一组标识符的属性的和定义的的对象,会导致保留用于对象存储的。又一个的的标识符的定义是该标识符的声明

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.

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

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