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?

3

4 Answers 4

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.

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

8 Comments

that's weird. As my code re-assign this static var and it changes once.
I don't understand that comment
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
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.
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?
|
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.

Comments

0

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.