Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • A static variable in a function will retain its value between function calls.

    • Consider the following code snippet:

      Code Block
      languagec
      //Setup code
      
      void spike_crash_counter_output()
      {
        static uint8_t number_of_crashes = 0;
        number_of_crashes++;
        printf("Number of crashes: %d\n", number_of_crashes);
      }
      
      int main()
      {
        spike_crash_counter_output();
        spike_crash_counter_output();
      }
      
      /*Output:
      Number of crashes: 01
      Number of crashes: 12 */
  • A variable declared as static in a file is a localized global, i.e. the variable cannot be seen by other files, only in the file that it was declared in.

  • A function declared as static in a file may only be called by functions in the same file, i.e. the function cannot be seen by other files, only in the file that it was declared in.