简体   繁体   中英

can we declare the same local static variable in different functions in C?

can we declare the same local static variable in different functions in C?

for example:

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
}

in this case, if I call function1 three times, then I call function function3 once, should the value of a is 4 or 1 in function3?

Those are three different variables. They just happen to have the same name. Changing the value of one will not change the value of another.

A variable has an associated scope within which it is visible. From section 6.2.1 Scopes of identifiers of the C99 standard:

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.

and:

For each different entity that an identifier designates, the identifier is visible (ie, 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.)

and:

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.

Each a (the identifier) in the posted code has function scope, and therefore does not interfere with an a declared in another function.

the static variable will be initialized automatlly by 0?

The static specifies the lifetime of a , which is static storage duration. From section 6.2.4 Storage durations of objects :

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)

and from section 6.7.8 Initialization :

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.

So a is initialized to 0 .

  1. local static variable scope is local so all are different.
  2. but their life is till your program not terminate.
  3. all have initial value (default) 0 .
  4. memory from the static segment.
  5. internal static variables are active(visibility) in the particular function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM