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?

1

5 Answers 5

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.

1
  • sorry my mistake it is initialization.
    – arya
    Sep 10, 2015 at 13:25
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.

2
  • 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, 2015 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. Sep 10, 2015 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.

4
  • Do you mean before application has started means at compile time?
    – arya
    Sep 10, 2015 at 13:26
  • Not necessarily, that is up to the compiler. Sep 10, 2015 at 13:27
  • 1
    @arya: The value must be known at compile time, so it is likely to happen in this way. Sep 10, 2015 at 13:34
  • Typically there is a snippet of code linked to your program which executes the static initialization before main() is called, usually referred to as "copy-down" or similar.
    – Lundin
    Sep 10, 2015 at 13:59
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.

1
  • 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, 2015 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!

3
  • 1
    static is a specifier (as in the citation), not a qualifier. Note that C89 is not the valid standard; which is C11. Sep 10, 2015 at 20:20
  • thank you! I used C89 for a bit, so I'm still using that :)
    – piFra
    Sep 10, 2015 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?) Sep 10, 2015 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.