0
 BOOL foo(void){

    static BOOL displayed = FALSE;
    static BOOL initialized = FALSE;

    if (displayed)
        return FALSE;

    //more code 

    displayed = TRUE;
    return FALSE;
}

what's the meaning of a static local variable in C ?

if this method is called a second time, the displayed won't be re-initialized to FALSE?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • https://stackoverflow.com/questions/14992116/c-c-the-purpose-of-static-const-local-variable – Engineer2021 Mar 19 '14 at 12:27
  • possible duplicate of [What is the use of Static local variable when we can get a global variable at the same cost?](http://stackoverflow.com/questions/15808049/what-is-the-use-of-static-local-variable-when-we-can-get-a-global-variable-at-th) – Engineer2021 Mar 19 '14 at 12:27
  • if this method is called a second time, the displayed won't be re-initialized to FALSE? - no, it will keep the value assigned to it last time – Andrey Chernukha Mar 19 '14 at 12:30

4 Answers4

4

Static local variables are initialized once only, before program startup. Their values then persist between invocations.

From the standard, section 6.2.4/3 Storage durations of objects:

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • that's weird. As my code re-assign this static var and it changes once. – Elad Benda Mar 20 '14 at 08:06
  • I don't understand that comment – David Heffernan Mar 20 '14 at 08:13
  • in my code there is a line `displayed = TRUE;` and i see it changes the value only once. you said the value is set once `prior to program startup.` but i see it changes once after first run – Elad Benda Mar 20 '14 at 09:10
  • No. I said the value is **initialized** once before program startup. And actually I gave you a quote from the C standard so it cannot really be argued with. So, the variable is initialized once. Indeed any variables is initialized at most one time. But you can then subsequently modify the variable. That is assignment, which is different from initialization. – David Heffernan Mar 20 '14 at 10:10
  • but debug shows the var can also be assigned only once. – Elad Benda Mar 20 '14 at 10:26
  • @EladBenda No. It is **initialized** once, and can be **assigned** many times. In your code, `static BOOL displayed = FALSE` is the initialization, and `displayed = TRUE` is an assignment. – David Heffernan Mar 20 '14 at 10:29
  • see this comment to my q: `if this method is called a second time, the displayed won't be re-initialized to FALSE? - no, it will keep the value assigned to it last time – Andrey Chernukha` – Elad Benda Mar 20 '14 at 13:51
  • 1
    That comment is correct. My answer says the same. A static local variable has static (or global) storage, but local scope. Are you perhaps not quite grasping the difference between initialization and assignment? – David Heffernan Mar 20 '14 at 14:13
2

Static variables are initialized only once. This can be used in special cases like counting the no of run-time executions of a function. The static variables have a life time same as global variables. But their scope is limited to where it is defined.

Ginu Jacob
  • 1,588
  • 2
  • 19
  • 35
0

the initialization is performed only once at the time of memory allocation by the compiler. The variable retains its value during program execution.

Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
0

Static automatic variables continue to exist even after the block in which they are defined terminates. Thus, the value of a static variable in a function is retained between repeated function calls to the same function. The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in which it is defined; however, the storage allocated becomes permanent for the duration of the program. Static variables may be initialized in their declarations; however, the initializers must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable.

user3437379
  • 79
  • 1
  • 1
  • 5