1

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?

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
arya
  • 81
  • 1
  • 8
  • 3
    See [this answer](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program) and [this tutorial](http://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.6.html) on static variables. – Enzo Ferber Sep 10 '15 at 13:25

5 Answers5

8

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.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
7

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.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • I understand this.But my question is in the second call to `f1`, the statement of initialization is ignored?only next statement gets executed. – arya Sep 10 '15 at 13:24
  • 2
    Yes, that's correct. That's what "one-time" means; the initialization is only performed once (possibly before you even call the function for the first time), and never again; one subsequent calls of `f1`, it definitely __won't__ be performed. – ShadowRanger Sep 10 '15 at 13:25
4

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.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
1

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.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Or rather, all `static` variables have _static storage duration_, as do any file scope ("global") variables. All such variables follow the same rules of initialization and duration. Whether they end up in `.data` or `.bss` segment depends on if they are initialized to a value or zero. Don't think of static variables as global variables... think of static variables _and_ global variables as variables with static storage duration. Also, please don't use the term "function scope" as that refers to goto labels. The correct term is block scope or local scope. – Lundin Sep 10 '15 at 13:57
1

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!

piFra
  • 11
  • 2
  • 1
    `static` is a specifier (as in the citation), not a qualifier. Note that C89 is not the valid standard; which is [C11](http://port70.net/~nsz/c/c11/n1570.html). – too honest for this site Sep 10 '15 at 20:20
  • thank you! I used C89 for a bit, so I'm still using that :) – piFra Sep 10 '15 at 20:23
  • There have been quite some advances with C99. Some have different semantics and type-checking has been strengthened. You really should advance. (You would not drive a Ford Pinto (pick any other 70ies rotting model) anymore, wouldn't you?) – too honest for this site Sep 10 '15 at 20:35