While reading some questions and answers in stackoverflow I come across this question
I tried to understand it but the answers were really hard to understand especially the terms like
static storage duration
the expression cannot be evaluated during translation phase
etc...
Besides, I thought that constants are always constants (This is what I learnt from school)
Please can someone makes it a little bit easy to understand ?
const variables are variables that a function (or structure) promises not to change, though other things may change that same variable.
Const (constant) in programming is a keyword that defines a variable or pointer as unchangeable. A const may be applied in an object declaration to indicate that the object, unlike a standard variable, does not change. Such fixed values for objects are often termed literals.
The keyword const is a little misleading. It does not define a constant value. It defines a constant reference to a value.
The const keyword allows you to specify whether or not a variable is modifiable. You can use const to prevent modifications to variables and const pointers and const references prevent changing the data pointed to (or referenced).
Difference between #define and const in C? #define is a preprocessor directive. Data defined by the #define macro definition are preprocessed, so that your entire code can use it. This can free up space and increase compilation times.
This means we can typecast, move addresses, and everything else you’d be able to do with a regular variable besides change the data itself, since the data assigned to that variable is constant. In general, const is a better option if we have a choice and it can successfully apply to the code.
The same goes for const member variables in classes, the only difference with non-const member variables is that when we try to modify them we will get the same compile error shown above.
The const keyword is used when the value of the variable is known at compile-time and never changes. In other words, the compiler knows in advance what value is to be stored in that variable. const int x = 1; //At compile time, the value of x is going to be 1 and will not change.
In C (unlike C++), an arithmetic expression is a "constant expression" only if every value in the expression is a numeric constant or the name of an enumeration value. That is, although you might have declared a variable to be a static const int
, you still cannot use that (constant) variable in a constant arithmetic expression.
Note that "constant expression" is a phrase defined by the formal standard which defines the C language. There are other expressions which are intuitively constant, but they are not included in the formal definition.
A variable with "static storage duration" is simply a variable which exists throughout the execution of the program. Most such variables are global variables (i.e. not part of any function, not even main
), but in C and C++ you can have a static
variable inside the scope of a function. Such a variable is initialized only once, and only a single instance of it exists regardless of how many times the function is called.
Global variables, and other variables with static storage duration, can only be initialized to a constant expression as per the above definition. This is the case whether or not they are const
variables. The issue is simply that the variables have static storage duration, which means that they must be initialized before the program executes. (A variable with static storage duration exists throughout the execution of the program, so if it is initialized -- that is, given an initial value, as opposed to being assigned a value during the program's execution -- the initialization must occur before the program executes.)
In C++, a variable declared static const
is considered a constant value, so it can appear in constant expressions. In C, however, that is not the case, so a C compiler does not need to track the initial value of static const
variables.
There are two almost completely unrelated concepts here:
4+5
is a constant expression.const A=4;
makes A
into a constant expression sometimes in certain contexsts, since it's const
and initialized from a constant expression 4
. (This only applies to C++, not to C)B=A;
A
may itself a constant expression, but B
is a variable, and may not itself be in constant expressions.const
variables are variables that a function (or structure) promises not to change, though other things may change that same variable.In C, a const
-qualified variable is not the same thing as a constant. The integer literals 50
and 100
are constant expressions; their values are known at compile time (that is, they can be evaluated during the translation phase, meaning when the compiler is translating the source code into machine code). However, the variables a
, c
, and d
won't have their values set until run time1; this means they cannot be used in a context where a compile-time constant is required2 (they cannot be evaluated during the translation phase). All the const
qualifier does is tell the compiler to reject any code that attempts to modify those variables after they've been initialized.
The problem in the linked question is that endX
and endY
are being declared at file scope, outside of any function. Because of this, the variables have static storage duration, meaning storage for them is set aside when the program is first loaded into memory, before main
executes, and held until the program terminates3. Since they are loaded before main
executes, they cannot be initialized with an expression whose value won't be known until after main
executes.
static const
-qualified variables will have their initial value set when the program is loaded; however, they are still treated as though they aren't initialized until after main
starts.
static const
-qualified variables are considered to be compile-time constants in that language.
static
keyword also have static storage duration, meaning they exist for the lifetime of the program, but are not accessible outside of that function or block.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