A static variable has 3 primary uses:
A static variable in a function will retain its value between function calls.
Consider the following code snippet:
//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: 0 Number of crashes: 1 */
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.
Add Comment