arya
arya

Reputation: 81

Assignment to static variable is ignored

I have following piece of code:

#include <stdio.h>

int f1()
{
    static int s=10;
    printf("s=%d\n",s++);
}
int main()
{
    f1();
    f1();
    return 0;
}

The output is:

s=10
s=11

Why is the line static int s=10 ignored at the second time, when f1 is called?

Upvotes: 1

Views: 2823

Answers (5)

too honest for this site
too honest for this site

Reputation: 12263

That is no assignment, but an initializer. Local static variables are only initialized once at program startup like global variables. They keep their last assigned value even between invocations of the function. Thus after your first call, it retains the value 11. In fact, they are like file-scope static variables, with their name only known in the scope of the block they are declared (but you can pass them by pointer).

Drawback is they only exist once. If you invoke the same function from multiple threads, they all share the same variable.

Try a third call: you will get 12.

Note: the initializer must be a constant expression. Try static int s = 10, t = s + 5; and read the compiler error message.

Upvotes: 8

piFra
piFra

Reputation: 11

From the C89 Standard HTML version at 3.1.2.4 Storage durations of objects it specifies:

An object 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 and retains its last-stored value throughout the execution of the entire program

(The emphasis is mine)

So it says that everytime you use the static qualifier, that variable preserves its value across multiple function calls. Local variables that are not static are initialized everytime you call the function that delcares them, so they do not preserve their value across function calls.

Hope this helped!

Upvotes: 1

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37924

The static variables are initialized only once, conceptually even before application has started.

From C11 (N1570) §5.1.2/p1 Execution environments:

All objects with static storage duration shall be initialized (set to their initial values) before program startup.

along with §6.2.4/p3 Storage durations of objects:

Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

Upvotes: 4

dbush
dbush

Reputation: 223917

As others have said, a static variable at function scope is initialized only once. So the assignment doesn't happen on subsequent calls to the function.

Unlike other local variables, a static local is not defined on the stack but in the data segment, probably in the same location as global variables. Globals are also initialized at application startup (they have to, since they don't live inside of a function and therefore can't be executable code), so conceptually you can think of a static variable as a global variable with limited visibility.

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155418

Initialization of a static variable is one-time (with the time of initialization guaranteed to occur before the first call, which could occur at compile time or at run time; compiler dependent). That's the main reason to use them.

Upvotes: 7

Related Questions