C语言之外部变量与作用域

     Each local variable in a function comes into existence only when the function is called, and disappears when the function is exited.This is why such variables are usually known as automatic variables.
     函数中的每个局部变量仅仅会在该函数被调用时存在,当该函数退出时,这些局部变量会消失。这也是为什么这些变量被看成是automatic自动变量。

     Because automatic variables come and go with function invocation, they do not retain their valus from one call to the next, and must be explicitly set upon each entry. If they are not set, they will contain garbage.
     因为自动变量会随着函数的调用产生和消失,他们的值会在进行下一个调用时消失,所以在每次进入函数时,必须明确地设置变量值。如果没有被设置,他们可能包含垃圾。

     The usual practice is to collect extern declarations of variables and functions in a separate file, historically called a headerthat is included by #include at the front of each source file. The suffix .h is conventional for header names.
     通常实际的操作是吧变量和函数的外部声明(extern)存放在一个独立的文件里,在历史上被叫作头文件(header),在每个源文件的开头用#include来包含,后缀.h会方便地表示头文件名。

     You should note that we are using the words definition and declaration carefully when we refer to external variables in this section. "Definition" refers to the place where the variables is created or assigned storage; "declaration" refers to places where the nature of the variable is stated but no storage is allocated.
     你应该注意,当我们针对外部变量(external)使用定义(definition)和声明(declaration)这两个字时应该非常注意。定义(definition)指的是这个地方变量被创建或者被分配内存;声明(declaration)指的是这个地方变量的性质被声明,但是没有分配内存。

     虽然extern外部变量能够使所有事情近在眼前,参数列表变短,当你需要的时候变量一直存在,这似乎简化了通信。但是即使你不用的时候外部变量也依然会在那。严重依赖外部变量会充满着危险,因为这导致了数据连接非常不明显————变量会在不可知和无意的情况下被改变,而且程序很难修改。还有一个原因这会毁坏函数的通用性。所以,滥用外部变量可能会写出劣质的程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值