You are writing a C program where you need to maintain a counter that persists across multiple function calls. Which type of variable would you use?
- Automatic variable
- Global variable
- Local variable
- Static variable
In this scenario, you would use a static variable to maintain a counter that persists across multiple function calls. Static variables retain their values between function calls, making them suitable for this purpose. Local variables have limited scope and are not suitable for maintaining a persistent counter. Global variables introduce unnecessary complexity and may lead to naming conflicts. Automatic variables are limited to a function's scope and are not designed for persistent storage.
Loading...
Related Quiz
- In C, an array name acts as a ________ pointing to the first element of the array.
- When is it appropriate to use the free function in C programming?
- What function is used in C to allocate a block of memory for an array of elements, initialize them to zero, and then return a pointer to the memory?
- How does the 'return' statement affect the flow of control within a loop?
- The #undef directive is used to ________ a macro defined by #define.