Pitfalls of Static Variables in C: Why the Reset Function Cannot Change Its Value?

Hello everyone, I am a programmer who loves to share. I am happy to share my experiences and understanding from my work.

-begin-

Title: What is the output of the following code?

#include <stdio.h>

int count() {

static int num = 0;

num++;

return num;

}

void reset() {

int num = 0;

printf(“num in reset: %d\n”, num);

}

int main() {

printf(“First call: %d\n”, count());

printf(“Second call: %d\n”, count());

reset();

printf(“Third call: %d\n”, count());

return 0;

}

Analysis Process:

1. Code Intent: The count function is intended to count the number of calls, and one might think that the reset function can reset the count, but it overlooks the essential difference between static and local variables.2. Actual Output:

First call: 1

Second call: 2

num in reset: 0

Third call: 3

3. Deep Reason: The static int num = 0 in the count function is a static local variable, whose lifetime spans the entire duration of the program and is initialized only once. Each time count is called, num increments based on its previous value, thus returning 1 and 2 for the first two calls. The int num = 0 in the reset function is a regular local variable, valid only within the reset function, and has no relation to the num in count. Its initialization and assignment do not affect the static variable in count, so after calling reset, the third call to count still returns 3.

I previously made a similar mistake while developing a device status counter: I wanted to reset the count using a function, but defined a local variable with the same name in the reset function. As a result, no matter how I called it, the counter kept increasing, and it took me a long time to realize it was a “variable scope confusion” issue.

Conclusion: The core feature of static variables is “globalized lifetime, localized scope”:

Lifetime: Initialized only once from program start to end, and the value is retained; Scope: Visible only within the function or file where it is defined (e.g., global variables modified by static can only be used in the current file).

Points to note when using:

Static local variables are suitable for storing “values that need to be retained across function calls” (e.g., counters, state flags), but excessive use can lead to functions depending on the state of static variables, affecting reentrancy (which may cause race conditions in multi-threaded environments); Avoid defining static variables with the same name in multiple functions, as this can easily lead to logical errors due to scope confusion; If you need to reset the value of a static variable, provide a reset interface within the function where it resides (e.g., add if (reset_flag) num = 0; in count), rather than using a local variable from an external function.

In embedded systems, static variables are often used to pass state between interrupt service routines (ISRs) and the main function (e.g., recording the number of interrupts), but care must be taken to lock protect in multi-tasking environments to prevent data corruption.

-end-

If this article has helped you, please like, share, and follow. Thank you very much.

More advanced debugging in C language

Advanced coredump debugging command practice: gdb -x—— batch analysis of coredump, making regression testing “automatically solve cases”.

Leave a Comment