[讨论]关于C语言数据存储类型关键字static的问题
(2012-04-09 23:44:47)
标签:
关键字
存储
c语言
杂谈
[讨论]关于C语言数据存储类型关键字static的问题在C语言数据存储类型关键字中有一个static,static有三种链接类型(linkage
type),即static with no linkage, static with internal linkage 以及
static with external linkage。我在一本书上,即C Primer Plus,看到对static with
no linkage的描述,如下:
Static, no linkage— A variable declared in a block with the static
storage class modifier belongs to the "static, no
linkage" storage class. It has static storage
duration, block scope, and no linkage. It is initialized just once,
at compile time. If not initialized explicitly, its bytes are set
to 0.
译文:静态存储类型,无链接类型——在一个程序段中声明的变量,具有静态存储类型,其所属的存储类型为“静态存储类型,无链接类型”。它具有静态存储持续期,程序段作用域,无链接。它只可初始化一次,在编译时进行。如果没有经过明确初始化则具有该存储类型的变量所有字节都设置为0。
按照这样的对“static, no linkage”的定义,则具有该存储类型的变量就只具有“block
linkage”,即它只在一个程序段中起作用,言下之意就是在程序的其他地方不起作用。
而稍后我又在该书的练习题中发现这样的一个问题:
Which storage classes create variables that persist for the
duration of the containing program?
译文:何种存储类型能够产生出变量,该变量能够在其所处的程序范围内持续存在?
给出的参考答案是:
The static, no linkage storage class, the static, internal linkage
storage class, and the static, external linkage storage
class.
译文:静态,无链接存储类型、静态,内部链接存储类型以及静态,外部链接类型
按照这样的参考答案http://www.tnbwang.com,也就是说static no
linkage类型的变量不只可以在其所处的程序段范围内存在还可以持续在整个程序范围内存在了?这和前面提出的static, no
linkage类型的定义不矛盾吗?而且我做了一个测试程序(如下)也证明定义是正确的而这个练习问题的参考答案不正确。
#include
int main(void){
int a = 5;
int b = 4;
if(a > b){
static int c = a;//
分享:
喜欢
0
赠金笔
加载中,请稍候......
评论加载中,请稍候...
发评论
登录名: 密码: 找回密码 注册记住登录状态
昵 称:
评论并转载此博文
发评论
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。