0

(C beginner alert)

Wikipedia define a static variable as, ".... a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program. "

Then it goes on to give an example in C:

#include <stdio.h>

void func() {
    static int x = 0; 
    /* x is initialized only once across four calls of func() and
      the variable will get incremented four 
      times after these calls. The final value of x will be 4. */
    x++;
    printf("%d\n", x); // outputs the value of x
}

int main() { //int argc, char *argv[] inside the main is optional in the particular program
    func(); // prints 1
    func(); // prints 2
    func(); // prints 3
    func(); // prints 4
    return 0;
}

Here's my issue: variable x, when defined as static, doesn't hold its value for the entire run of the program. In fact, it does quite the opposite, namely, for any subsequent call of func(), it has the value it was assigned in the previous calling. It's only when I remove the static keyword that x retains its value of 0 no matter how many times func() is called.

So:

1) Is Wikipedia's explanation inaccurate/misleading, and if so, how would you better explain the nature of a static variable?

2) What is actually happening under the hood on the second and subsequent calls of func() such that the initialization of x to 0 is effectivey being ignored?

6
  • Are you saying that the above example doesn't print "1 2 3 4" ? Commented May 14, 2015 at 18:08
  • As much as I hate defending Wikipedia, it doesn't say "holds its value." It's location in memory does not change, even across multiple calls to the function that provides the declaring scope. The fact its value increments with each call demonstrates the static allocation. Commented May 14, 2015 at 18:10
  • @rost0031 well spotted. OK bad example - let's move the print statement before the increment. Commented May 14, 2015 at 18:14
  • lifetime & extent have to do effectively with describing how the variable remains alive beyond the normal scope of its logical block, not the value it holds or does not hold in memory. Commented May 14, 2015 at 18:16
  • If you move the printf statement ahead of the increment, the value of x would still increment. But the printf would record values starting from 0 rather than 1. Commented May 14, 2015 at 18:19

3 Answers 3

3

You simply misunderstand the quoted explanation. What it means to say that a variable's "lifetime" extends to the whole program is that it is allocated in memory once, initialized once, and any changes made to it are reflected at the same memory location--that memory is allocated for the life of the program. Subsequent calls to the function will find the contents of that memory (the value of x) as they left it. They are still allowed to change the value, but it will always be the same piece of memory.

When you remove the "static", you're telling the compiler to allocate a new variable "x" each time the function is called, assign it 0, and discard it when the function returns. On the next call, an entirely different variable X will be created, assigned 0 again, and discarded again. This variable's "lifetime" is thus only inside the function.

This is an entirely separate concept from the variable's "scope", which is who can see and change the variable: it's scope is still inside the function, even when static.

Sign up to request clarification or add additional context in comments.

Comments

1

The local static variables are located in the .bss section along with the global ones. So their initialization is effectively "taken out" of the function and moved to the same place where global statics are initialized. So, basically, declaring some variable as local and static in some function is only limiting it's scope to this function, but by other means it is not different from any other static variable.

1 Comment

Local static variables could also be located in the .data section if initialized to some value other than zero.
1

The Wikipedia entry is neither misleading nor incorrect; it says the variable's lifetime extends across the lifetime of the program (even before func is called), which is exactly the case. A single instance of x is created and initialized once at program startup and will persist until the program exits, retaining the last value written to it from each call to func. Think of it as a "global" variable, but only visible from within func.

If you remove the static storage class qualifier, then x only exists for the lifetime of func; each call to func creates a new instance of x, which is initialized to 0.

1 Comment

"Think of it as a "global" variable, but only visible from within func. " - helpful

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.