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.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *