As we know, the keyword static
has multiple meanings in C. C99 added the possibility of legally writing
void foo (int arr[static 50]) { // ... }
which adds to the confusion, and C++ has static member variables and functions.
This would not be so troublesome if all the uses could be connected in some way, but I find it hard to find that link for some of the cases. Particularly why the static
keyword should be used to modify visibility (linkage), or what on earth it's got to do with an array's minimum amount of elements.
So is there a historical reason for the abuse of the static
keyword, or is there a secret link under the hood that connects all of its uses?
A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.
A global variable can be accessed from anywhere inside the program while a static variable only has a block scope. So, the benefit of using a static variable as a global variable is that it can be accessed from anywhere inside the program since it is declared globally.
When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.
In C, inside a function, we use the static modifier to declare variables with static storage duration. Such variables retain their value throughout multiple calls of the function. These variables are initialized only once at compile time. Their life time matches the life time of our program.
Adding new keywords to a language breaks backwards compatibility. So static
gets used where its use might possibly mean something ( int arr[static 50]
vs int arr[auto 50]
or int arr[extern 50]
) and cannot syntactically appear in that location based its use in previous versions.
Though in that case adding a not_less_than
context sensitive keyword in that position would not break previous code, it would add another keyword (so simple text editors which are keyword aware but not syntax aware would not know whether or not it is a keyword), and break the 'keywords are not context sensitive' simplification made in C.
There is a very simple way of remembering of all 3 C++ meanings of static
I'm aware of. static
means "pretty much like global variable/function but only available directly in scope of..."
struct
). This case is slightly different as you can access the variable/function through an object or by prefixing, but this is a little like asking the class or its object to provide you access to it, and it in fact can be denied (with private
). So the access isn't "direct".In case of the presented C99 array syntax, this is something completely different and I assume it was there to not introduce new keywords, as others suggest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With