What is the effect of the static keyword for a local variable inside a function?

Enhance your programming skills with the RECF Programming Test. Features flashcards and multiple choice questions with hints and explanations. Prepare for success!

Multiple Choice

What is the effect of the static keyword for a local variable inside a function?

Explanation:
Static local variables have static storage duration, which means the variable exists for the entire run of the program and preserves its value between function calls. The function’s scope stays local to that function, so it isn’t accessible from outside, but its memory isn’t released after the function returns. Initialization happens only once (on the first call, or at program start if given a constant initializer), and later calls see the value left from the previous call. It’s not global, and it isn’t allocated on the per-call stack, which is why the value persists.

Static local variables have static storage duration, which means the variable exists for the entire run of the program and preserves its value between function calls. The function’s scope stays local to that function, so it isn’t accessible from outside, but its memory isn’t released after the function returns. Initialization happens only once (on the first call, or at program start if given a constant initializer), and later calls see the value left from the previous call. It’s not global, and it isn’t allocated on the per-call stack, which is why the value persists.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy