变量的可见性 [英] visibility of variable

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

问题描述

你好,

要限制单个文件中变量的范围,该文件是具有多个C文件的大型项目的一部分,我们使用静态变量

吧?然后将任何变量限制为功能范围应该是

声明为auto还是其他什么?

hello,
To limit scope of a variable in a single file that is part of a
large project that have several C files we use static variable
right?then to limit any variable to function scope it should be
declared as auto or anything else?

推荐答案

>要限制单个文件中变量的范围,该文件是
> To limit scope of a variable in a single file that is part of a
具有多个C文件的大型项目的一部分,我们使用静态变量
对吗?


正确。

然后将任何变量限制为功能范围它应该是
声明为auto还是其他什么?
large project that have several C files we use static variable
right?
Right.
then to limit any variable to function scope it should be
declared as auto or anything else?




auto是局部变量的默认值,你不需要做任何事情

特殊如果你想让一个变量是函数的局部变量,只需声明

它在函数内部。



auto is the default for local variables, you don''t need to do anything
special if you want a variable to be local to a function, just declare
it inside the function.


ra ******* @ gmail.com 写道:
ra*******@gmail.com writes:
限制作为大型项目有多个C文件我们使用静态变量
对吗?然后将任何变量限制为功能范围它应该是
声明为auto还是其他什么?
To limit scope of a variable in a single file that is part of a
large project that have several C files we use static variable
right?then to limit any variable to function scope it should be
declared as auto or anything else?




看来你混淆范围和联系。在函数外声明的任何变量

都有文件范围,无论是否使用`static''声明它是否为
。但是,`static''给出了

变量内部链接,这意味着它不会与其他翻译单元中的文件范围标识符链接




函数内部声明的变量具有块范围,而不是函数范围。 (只有标签具有功能范围。)当它们声明没有存储类说明符或者除了extern之外的任何

存储类说明符时,它们没有

联动。

-

如果它没有给出一个新的C标准就不会/>
static一词的新含义。"

- C99上的Peter Seebach



It seems that you are confusing scope and linkage. Any variable
declared outside a function has file scope, regardless of whether
it is declared with `static'' or not. However, `static'' gives the
variable internal linkage, which means that it will not be linked
against file-scope identifiers in other translation units.

Variables declared inside a function have block scope, not
function scope. (Only labels have function scope.) When they
are declared without a storage class specifier, or with any
storage class specifier other than `extern'', they have no
linkage.
--
"It wouldn''t be a new C standard if it didn''t give a
new meaning to the word `static''."
--Peter Seebach on C99


ra ******* @ gmail.com 写于26/09/05:
ra*******@gmail.com wrote on 26/09/05 :
要限制单个文件中变量的范围,该文件是具有多个C文件的大型项目的一部分,我们使用静态变量
对吗?然后将任何变量限制为函数范围它应该是
声明为auto或其他什么?
To limit scope of a variable in a single file that is part of a
large project that have several C files we use static variable
right?then to limit any variable to function scope it should be
declared as auto or anything else?




没有''功能范围''。有块范围。块中定义的变量

具有块范围。如果它具有''静态''

限定符,则会变得持久。


但是不鼓励这些方法,特别是对于大型项目。

更好地处理''contexts'',其中数据在结构中定义,

以及代码用于处理数据的位置。


这是面向对象编程的第一步,这是一个很好的

Thing。


下一步是''ADT'' (摘要数据类型)。谷歌是你的朋友。


-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

C库: http:// www .dinkumware.com / refxc.html


今天世界上有10种类型的人;

了解二元的人,那些不要的。



There is no ''function scope''. There is ''block scope''. A variable
defined in a block has the block scope. If it has the ''static''
qualifier, it becomes persistent.

But these methods are not encouraged, specially for large project.
Better to work with ''contexts'', where data are defined in a structure,
and where the code is designed to process the data.

It''s the first step to Object Oriented Programming, that is a Good
Thing.

Next step is ''ADT'' (Abstract Data Types). Google is your friend.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."


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

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