-1

With static in C one can keep a stack variable around, even after the creating function exists.

In this case however,

void static_func() {
    static int var = 1;
    var += 1;
}

I can access var outside of the static_func() and return its value. Let's assume static_func() is called three times in the main(), then the value of var is 3.

However, it is set to 1 every time the function is executed. Why do I still get the value 3?!

four-eyes
  • 10,740
  • 29
  • 111
  • 220
  • `static` and _static_ variables mean something different. – too honest for this site Jun 02 '16 at 14:38
  • 3
    "However, it is set to `1` every time the function is executed." Why? Because your compiler contains a bug? – MikeCAT Jun 02 '16 at 14:38
  • 2
    And where is your `stack`? This is no keyword in C, nor does C require a _stack_. And typical implementations which use a _stack_ don't store `static` variables (which are a subset of _static_ variables) on the stack. – too honest for this site Jun 02 '16 at 14:39
  • @MikeCAT I do not hope so! ;) – four-eyes Jun 02 '16 at 14:43
  • After three calls, the value in `var` is 4, not 3. You can't access `var` outside the function. You could return (a copy of) its value. If you returned a pointer to the variable, it could be indirectly accessible outside the function, but not otherwise. As written, you have no way of doing anything useful with `var`. If you had `static int static_func(void) { static int var = 1; return ++var; }` then you'd have a monotonic counter that's not resettable except by overflow (which is undefined behaviour, which is bad). – Jonathan Leffler Jun 02 '16 at 14:44
  • No, you *cannot* access your static local variable `var` outside function `static_func()`. Local variables have *no linkage*, regardless of their storage class. – John Bollinger Jun 02 '16 at 14:44
  • 1
    "I can access var outside of the static_func()" - If that is true, **instantly** `rm -f $YOUR_COMPILER_BINARY_WITH_PATH`. And `(1 + 3 * 1) == 4`, not `3`. – too honest for this site Jun 02 '16 at 14:45
  • @Olaf, I don't understand "(1 + 3 * 1) == 4, not 3", it's indeed 4 – ForceBru Jun 02 '16 at 14:48
  • @ForceBru: I did not use a pocket calculator, but I'm pretty confident the expression evaluates `true`. OP seems to think three calls incrementiong from `1` yields `3`. What is not clear about my comment? – too honest for this site Jun 02 '16 at 14:50
  • @Olaf, oops, now I got it :P no problem – ForceBru Jun 02 '16 at 14:51
  • @Olaf yeah, I made a mistake there... It should be four! Bottom line of my question was why it does not get set back to one every time the function is called... – four-eyes Jun 02 '16 at 14:51
  • @ForceBru Thought about `var` being `3` or `4`. Given the trivial code, perhaps a compiler can optimize out `var` and so it has no value and does not even exist? IOW can a compile simple do `void static_func() { ; }`? Hmmm? – chux - Reinstate Monica Jun 02 '16 at 15:30

2 Answers2

6

Once you say static int var = 1;, this variable is created and initialized. This can happen only once, otherwise you're gonna flood your memory with useless duplicates since this variable will stay alive during whole program runtime.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
4

The initial value of a static variable is only applied once at program startup. It doesn't happen each time the function is entered.

dbush
  • 205,898
  • 23
  • 218
  • 273