Study Notes on C Language, Interrupts, and Debugging

Static Keyword in C Language

  • The static keyword applied to variables within a function:

    • When a variable is declared as static, it retains its value even after the function has exited.

    • Upon subsequent calls to the function, the static variable remembers the last value assigned to it.

    • Example:

    • If you have a static variable as an incrementer within a function, it will preserve its count through multiple calls.

    • This is useful for scenarios like rate-limiting where you might need to throw an error code if the function is executed too frequently.

  • Benefits of using static variables:

    • Provides a mechanism to maintain state information across function invocations without exposing that state to other functions.