- A static variable inside a function keeps its value between invocations.
- A static global variable or a function is "seen" only in the file in which it's declared in.
a = 15, sa = 15 a = 15, sa = 20 a = 15, sa = 25 a = 15, sa = 30 a = 15, sa = 35 a = 15, sa = 40 a = 15, sa = 45 a = 15, sa = 50 a = 15, sa = 55 a = 15, sa = 60
a = 15, sa = 15 a = 15, sa = 20 a = 15, sa = 25 a = 15, sa = 30 a = 15, sa = 35 a = 15, sa = 40 a = 15, sa = 45 a = 15, sa = 50 a = 15, sa = 55 a = 15, sa = 60
(2) Isis used widely as an "access control" feature. If you have a .c file implementing some functionality, it usually exposes only a few "public" functions to users. The rest of its functions should be made static, so that the user won't be able to access them. This is encapsulation, a good practice.
Quoting Wikipedia[Wikipedia][1]:
In the C programming language, staticstatic is used with global variables and functions to set their scope to the containing file. In local variables, staticstatic is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.
Additionally, in C, keyword static can be used in array declarators to specify[specify minimum size of the array.array][1] (Nonnon-function array declarators cannot use this keyword.). Consider this functiondeclaration:
Such aThe function can takefunc() takes an array of at least 42 elements.
Note that in C++, such usage does not support this use of the static keyword is not allowed.
cppreference.com covers this topic quite well: https://en.cppreference.com/w/c/language/array