1

I'm trying to learn how static variable work in c for when they are defined in a given function. For example, when I write the following:

#include <stdio.h>
void inc() {
  static int c = 0;
  c++;
  printf("%d\n", c);
}

int main(void) {
  inc();
  inc();
  inc();
  return 0;
}

The expected output is obviously:

1
2
3

On the first call of the function, the static variable c is defined and given the value of 0, which makes perfect sense. It is the incremented and printed. However, on the second call to inc() why is it that the integer c is maintained and not set to zero, even though the code literally says static int c = 0;. What mechanism in the compiler stops c from having it's value set to zero like during the first call?

Animator Joe
  • 69
  • 1
  • 6
  • Its more about scopes than variable being static. – Hirasawa Yui Jun 21 '19 at 06:04
  • Did you compare the behaviour to `static int c; c=0;` ? You might want to do that and mention it in the question to make things clearer. E.g. as in "Why is the behaviour not identical?" – Yunnosch Jun 21 '19 at 06:08
  • Is your question "what exactly happens?" or "why do compilers act like that?" . The latter is answered by Sourav below. The former cannot really be answered, since compilers (and linkers) are quite free how to implement the required externally observable behaviour. – Yunnosch Jun 21 '19 at 06:09
  • 2
    `c` isn't set to 0 on the first call. It's already 0 when the file containing the function is loaded. `c` is in static memory, which basically means it's like a global variable except its name is only available inside `inc`. – Tom Karzes Jun 21 '19 at 06:14
  • Very similar questions, but I hesitate to mark as duplicate. Probably an interesting read nonetheless : [Where are static variables stored in C and C++?](https://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-and-c), [how do static variables inside functions work?](https://stackoverflow.com/questions/7740761/how-do-static-variables-inside-functions-work) – Sander De Dycker Jun 21 '19 at 06:31
  • the way compilers implement static variables is usually the same as global variables, only that they use linker tricks to hide them -- like [decorating their names](https://unix.stackexchange.com/a/471885/308316), not quite dissimilar to how c++ compilers are implementing functions with the same name but different signatures. –  Jun 21 '19 at 08:13

2 Answers2

7

Quoting C11, chapter §6.2.4, Storage durations of objects (emphasis mine)

An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or 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.

So, the initialization inside the function call does not take place on every call to the function. It only happens once, before the execution of main() starts. The variable retains the last stored value through the program execution, i.e, the value is retained between the repeated calls to the function.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    For those folk (like me) who like to collect differences between C and C++, note that in the latter, the `static` is initialised the first time it's encountered in program flow as specified by the source code: you can also initialise to the result of a function call. – Bathsheba Jun 21 '19 at 07:22
  • @Bathsheba yes, and the constructors of function-local static variables will also be called when the function is first run, not before main as the other static constructors. –  Jun 21 '19 at 09:42
-1

Its lifetime is the entire execution of the program and its valie is intialized before program starts and only once.