Can someone explain the difference between a static
and const
variable?
Constant variables never change from their initial value. Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants. Static variables are created when the program starts and destroyed when the program stops.
“static const” is basically a combination of static(a storage specifier) and const(a type qualifier). The static determines the lifetime and visibility/accessibility of the variable.
const is a constant value, and cannot be changed. It is compiled into the assembly. static means that it is a value not related to an instance, and it can be changed at run-time (since it isn't readonly ). So if the values are never changed, use consts.
A constant value cannot change. A static variable exists to a function, or class, rather than an instance or object.
These two concepts are not mutually exclusive, and can be used together.
The short answer:
A const
is a promise that you will not try to modify the value once set.
A static
variable means that the object's lifetime is the entire execution of the program and it's value is initialized only once before the program startup. All statics are initialized if you do not explicitly set a value to them.The manner and timing of static initialization is unspecified.
C99 borrowed the use of const
from C++. On the other hand, static
has been the source of many debates (in both languages) because of its often confusing semantics.
Also, with C++0x until C++11 the use of the static
keyword was deprecated for declaring objects in namespace scope. This deprecation was removed in C++11 for various reasons (see here).
The longer answer: More on the keywords than you wanted to know (right from the standards):
C99
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
/* file scope, static storage, internal linkage */
static int i1; // tentative definition, internal linkage
extern int i1; // tentative definition, internal linkage
int i2; // external linkage, automatic duration (effectively lifetime of program)
int *p = (int []){2, 4}; // unnamed array has static storage
/* effect on string literals */
char *s = "/tmp/fileXXXXXX"; // static storage always, may not be modifiable
char *p = (char []){"/tmp/fileXXXXXX"}; // static, modifiable
const char *cp = (const char []){"/tmp/fileXXXXXX"} // static, non-modifiable
void f(int m)
{
static int vla[ m ]; // err
float w[] = { 0.0/0.0 }; // raises an exception
/* block scope, static storage, no-linkage */
static float x = 0.0/0.0; // does not raise an exception
/* ... */
/* effect on string literals */
char *s = "/tmp/fileXXXXXX"; // static storage always, may not be modifiable
char *p = (char []){"/tmp/fileXXXXXX"}; // automatic storage, modifiable
const char *cp = (const char []){"/tmp/fileXXXXXX"} // automatic storage, non-modifiable
}
inline void bar(void)
{
const static int x = 42; // ok
// Note: Since an inline definition is distinct from the
// corresponding external definition and from any other
// corresponding inline definitions in other translation
// units, all corresponding objects with static storage
// duration are also distinct in each of the definitions
static int y = -42; // error, inline function definition
}
// the last declaration also specifies that the argument
// corresponding to a in any call to f must be a non-null
// pointer to the first of at least three arrays of 5 doubles
void f(double a[static 3][5]);
static void g(void); // internal linkage
C++
Has the same semantics mostly except as noted in the short answer. Also, there are no parameter qualifying static
s.
extern "C" {
static void f4(); // the name of the function f4 has
// internal linkage (not C language
// linkage) and the function’s type
// has C language linkage.
}
class S {
mutable static int i; // err
mutable static int j; // err
static int k; // ok, all instances share the same member
};
inline void bar(void)
{
const static int x = 42; // ok
static int y = -42; // ok
}
There are a few more nuances of C++'s static
that I leave out here. Have a look at a book or the standard.
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