2

I'm trying to fully understand how static variables work in C. I understand that using the static keyword makes the lifetime of the variable equal to the duration of the program. The following example has one thing that confuses me though...

My static_test.c file:

#include <stdio.h>

void foo(){
    static int counter = 0;
    counter++;
    printf("This function has been called %i times.\n",counter);
    return;
}

int main(){
    int i;
    for(i=0;i<10;i++){
         foo();
    }
    return 0;
}

When I first read this example it makes me wonder why the function doesn't print out 0 every time since we are assigning 0 to the static variable.

Is this because once a static variable is declared the compiler ignores another declaration of the same variable on the next calls to my foo() function?

Thanks!

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Hosack
  • 1,597
  • 1
  • 8
  • 10

4 Answers4

1

Static variables are initialized exactly once at program start, before your function is ever called. The fact that it's a local variable doesn't affect this rule.

In addition, static storage class variables are automatically initialized to zero if not explicitly initialized; the = 0 in your program is superfluous.

From the C11 drafts standard (ISO/IEC 9899:201x), section 6.2.4 Storage durations of objects, paragraph 3:

An object whose identifier is declared … with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • "automatically initialized to zero, even if there's no explicit initializer" makes it seem that `i` in `static int i = 42;` will be initialized to 0 instead of 42. – Casey Jul 29 '13 at 21:10
  • But there *is* an explicit initializer in that statement. – Carl Norum Jul 29 '13 at 21:11
  • I know that - but your statement implies that zero initialization applies to everything regardless. – Casey Jul 29 '13 at 21:13
  • So you just don't like the "even", or what? You have plenty of rep to make an edit if you'd like. – Carl Norum Jul 29 '13 at 21:15
  • Done. I saw the answer rapidly updating, and didn't want to step on your toes with an edit. – Casey Jul 29 '13 at 21:18
1

Despite the use of =, this is an initialization, NOT an assignment. So it happens when the variable is initialized and not when the 'statement' is apparently 'executed'. Since it is a static variable, it is initialized once when the program starts, rather than every time the function runs.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Thank you! I guess my mistake was in not understanding the difference between initialization and assignment. Read up on the differences and now it makes sense. – Hosack Jul 29 '13 at 21:46
0

Yes, the compiler looks for an initial assignment after declaring a static variable and only executes it once, when the program starts.

This only happens when you declare the variable though. For example,

void foo(){
    static int counter;
    counter = 0;
    counter++;
    printf("This function has been called %i times.\n",counter);
    return;
}

Would print 1 every time.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • The initialization in a declaration is **not** an assignment. People may be mislead by the `=` token, but it is part of initialization syntax and is distinct from the `=` operator in an assignment. Declaration statements have initialization. Expression statements have assignment. So the compiler does not “look for an initial assignment”. – Eric Postpischil Jul 29 '13 at 21:22
0

Static means the variable exists outside of the life time of the function. Think of it as a slightly clever global variable.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39