Hubbry Logo
search
logo

Static (keyword)

logo
Community Hub0 Subscribers
Write something...
Be the first to start a discussion here.
Be the first to start a discussion here.
See all
Static (keyword)


static is a reserved word in many programming languages to modify a declaration. The effect of the keyword varies depending on the details of the specific programming language, most commonly used to modify the lifetime (as a static variable) and visibility (depending on linkage), or to specify a class member instead of an instance member in classes.

In the predecessors of C, including BCPL and B, there was already a concept of static storage, which meant a storage which is always in existence. However, In B, there wasn't a static keyword, but there was an extrn keyword to specify external storage (external to all functions and must be defined outside a function), which is always static, in contrast with the auto keyword, which declared a variable with automatic storage – one appears whenever the function is invoked, and disappears when the function returns. All variables must be declared as one of auto, extrn, or implicitly as function arguments.

C was developed as a successor of B, and the static and register keywords were added as storage class specifiers, along with auto and extern, which kept their meaning from B. However, in C, the concept of linkage for variables outside of functions was introduced. A C program can be formed by multiple compilation units and linked together to form a complete program, which a variable or a function can be either specified as having internal linkage (visible to its own compilation unit only), or external linkage (visible to the whole program). These keywords specify both the storage duration and linkage as follows:

Every variable and function has one of these storage classes; if a declaration does not specify the storage class, a context-dependent default is used:

So, in C, although the static keyword – when used on variables – always declares a variable with static storage duration, there are two distinct meanings of the keyword, depending on where it is used:

Therefore, in C, the term "static variable" has two meanings which are easy to confuse:

Variables with storage class extern, which include variables declared at top level without an explicit storage class, are static in the first meaning but not the second.

In C++ (not C), the static keyword can also be used to declare a member variable in a class to have static storage duration as well, independent from the storage duration of the class object itself, and such a variable must be defined outside the class. The effect is that the variable is shared among all class instances, becoming a class member instead of an instance member. When applied to a member function (i.e. a method), it specifies that the member function operates independently of an instance, which means it can't access any non-static members of the class nor the this pointer.

See all
User Avatar
No comments yet.