我们可以在 C 的不同函数中声明相同的局部静态变量吗?

标签 c function static

我们可以在 C 的不同函数中声明相同的局部静态变量吗?

例如:

in function1:
void function1()
{
   static int a;
   a++;
   //dosomething here
}

in function2:
void function2()
{
   static int a;
   a++;
   //dosomething here
}

in function3:
void function3()
{
   static int a;
   a++;
   //dosomething here
}

在这种情况下,如果我调用了3次function1,那么我调用了一次function3,请问function3中a的值应该是4还是1?

最佳答案

这是三个不同的变量。他们只是碰巧有相同的名字。改变一个的值不会改变另一个的值。

一个变量有一个关联的范围,在这个范围内它是可见的。来自 C99 标准的 6.2.1 标识符范围部分:

An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter. The same identifier can denote different entities at different points in the program. A member of an enumeration is called an enumeration constant. Macro names and macro parameters are not considered further here, because prior to the semantic phase of program translation any occurrences of macro names in the source file are replaced by the preprocessing token sequences that constitute their macro definitions.

和:

For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)

和:

Every other identifier has scope determined by the placement of its declaration (in a declarator or type specifier). If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope, which terminates at the end of the function declarator. If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will be a strict subset of the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

发布代码中的每个 a(标识符)都有 function 范围,因此不会干扰另一个函数中声明的 a .

the static variable will be initialized automatlly by 0?

static指定了a的生​​命周期,即静态存储时长。来自 6.2.4 对象的存储持续时间部分:

An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. For such an object, storage is reserved and its stored value is initialized only once, prior to program startup. The object exists, has a constant address, and retains its last-stored value throughout the execution of the entire program.23)

来自 6.7.8 初始化部分:

If an object that has static storage duration is not initialized explicitly, then:

-- if it has pointer type, it is initialized to a null pointer;

-- if it has arithmetic type, it is initialized to (positive or unsigned) zero;

-- if it is an aggregate, every member is initialized (recursively) according to these rules;

-- if it is a union, the first named member is initialized (recursively) according to these rules.

所以a被初始化为0

关于我们可以在 C 的不同函数中声明相同的局部静态变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16913280/

相关文章:

c - 如何仅在没有尾随零时才将小数点显示到 2 位小数

c - GDB : Why can't I execute single statements when I compile without -g?

c - 循环运行的次数超过 C 中指定的次数?为什么?

javascript - Uncaught ReferenceError - 未定义函数

c - Gotoxy(int x,int y) 的使用

r - 在 R 中从 [包] 导入 [函数]

MATLAB:如何将参数传递给函数?

java - 如何在 Play! 的静态方法中使用 play.cache.CacheApi框架 2.4.2

C++ 模板静态成员构造函数未被调用

java - 如何修复 Main 中调用的非静态方法?