静态变量声明(C) [英] Static Variable Declaration (C)

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

问题描述

以下两个static变量声明是否等效?

Are the following two static variable declarations equivalent?

1.

    static int var1;
    static int var2;
    static int var3;

2.

    static int var1, var2, var3;

更具体地说,在情况2中,所有变量是static还是仅仅是var1?

More specifically, in case 2, will all variables be static, or just var1?

推荐答案

是的,如果12相同,则声明.我们可以通过转到草稿C99标准来查看 6.7.5 声明符部分,其中说明(强调我的前进):

Yes the declarations in case 1 and 2 are identical. We can see this by going to the draft C99 standard section 6.7.5 Declarators which says (emphasis mine going forward):

每个声明器声明一个标识符,并在一个 与声明符相同形式的操作数出现在表达式中, 它指定具有范围,存储期限的功能或对象, 和声明说明符指示的类型.

Each declarator declares one identifier, and asserts that when an operand of the same form as the declarator appears in an expression, it designates a function or object with the scope, storage duration, and type indicated by the declaration specifiers.

我们可以从6.7 声明部分中看到语法如下:

We can see the grammar from section 6.7 Declarations is as follows:

declaration:
   declaration-specifiers init-declarator-listopt ;

声明说明符包括存储期限:

declaration-specifiers:
   storage-class-specifier declaration-specifiersopt

因此,存储期限说明符适用于 init-declarator-list 中具有以下语法的所有声明符:

so the storage duration specifier applies to all the declarators in the init-declarator-list which has the following grammar:

init-declarator-list:
   init-declarator
   init-declarator-list , init-declarator
init-declarator:
   declarator
   declarator = initializer

您可能想知道指针,它们的处理方式不同,我们可以从6.7.5的语法中了解声明符

You may wonder about pointers, they are handled handled differently and we can see this from the grammar in 6.7.5 for declarators:

declarator:
    pointeropt direct-declarator
[...]
pointer:
    * type-qualifier-listopt
    * type-qualifier-listopt pointer

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

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