Basically, regardless of the language, a constant is a declared value that goes along with the Runtime distribution of your code - that is, it is not a value that will change coming from some external source after the program is running (a terminal input, reading from the internet, database, etc...)
Note that this definition is different from whether the language has a way of associating the value of the constant to a name that "looks like a variable, works almost like a variable, but cannot be modified".
The value that goes inside the "Runtime" of your program - which depending on the language can be a binary file (.exe, . so for C, C++, go, Rust), a bytecode file (.jar, . pyc Java and Python) or the source code itself (.js, . php)
So let’s do what I mean -
independent of its C program being as
#define numero 2
and
int numero = 2;
The value "2" will be within your code. In the case of C, at the time of compilation, at any point the string "number" appears, the compiler already trades the value "2" - as if it had been typed at that point.
And that’s why in C and other compiled languages, the value that’s used in constant (#define
) have to be declared already in the statement.
Already, even in "C", if you use "const" instead of "define", what happens inside is something different - it will depend on the compiler implementation, but in general it will be a variable like any other for reading access, and the compiler itself will refuse to generate code to modify that variable. If such code appears in the compilation path, the compiler generates an error.
IN Java, the modifier final
will work like const
from C and C++ - the compiler refuses to generate code to modify that variable, but inside the bytecode, it is identical (ok, it can have some optimizations, it depends on the completion), to a normal variable.
This is a simple mechanism in languages called "static" - where variables act as "boxes" where an object of a certain size fits - the compiler has to generate all the code to change the contents of the boxes.
In languages such as Javascript, Python, PHP, Ruby, so-called dynamic languages, the functioning of variables is completely diverse. In most of them, a variable is just a name given to an object that is in memory. The "=" sign only points the name to another object.
The compiler of these languages (yes, even if it is a compilation made in a transparent and invisible step for the programmer, all modern languages of general use are compiled, ie, translated from the representation in text to a sequence of binary values that is interpreted by a virtual machine similar in concept to the Java JVM - some older languages, or developed by hobby, can still be interpreted as appearing in books "text"- each line of code is "understood" (Parsed), and for each set of tokens a routine in the interpreter’s code is executed - that on each run of the same line. BASIC that came embedded in 8 bit computers worked like that) has no way of knowing (and does not even care) all objects that a name will point to while running the program - so even if the language has a "const" statement or equivalent, the program will only know this at runtime - when some value is assigned there.
Now, as I described at the beginning, any value typed in the source code of a program -be a string, a number, anything, will be a "constant" that is and data structures within the executable file - and at the right time of the program, the language Runtime will give a name to that value -
That is, this code in Python:
numero = 2
texto = "palavra"
It will generate a Python bytecode file (.pyc), in which the values "2" and "'word'" are embedded in the internal code structures. These values are "constants" in the sense that they can only be changed by changing the bytes in that file, or by using resources to change the memory of the running program - however, the Python language, for example, does not care about having an explicit mechanism of the type "const" that can be used from code - since this doesn’t even make sense with the variable mechanism that are names for objects. At any point in the above program, the programmer can write numero = 5
, and points out the name numero
to another object.
Javascript should do the same thing, but the language runtimes do not expose intermediate data structures - neither as a file, nor by introspection, so this is not visible (and will change between each implementation)
With these considerations in mind, let us visit your three questions:
What is a constant and what is its difference from a variable?
A value that cannot be changed during the execution of the program and that is usually determined in the source code itself and "failed" in the executable file.
Static languages have mechanisms to offer this feature as language functionality, and the compiler itself can use it as a normal variable, but error instead of issuing any code to change the variable. Dynamic languages do not necessarily have "constants" as a language resource, although it can be emulated.
Can I delay the initialization of a constant? In other words, declare it but assign a value to it later.
It depends on the language specification - a static language would have to "steal" a little - find further ahead in the code where the assignment is, and assign the value where the variable is created within the scope. The value would be there "before" the assignment (which is not to say that the compiler should not give an error if you try to read the value before this assignment).
In a dynamic language, code "constant" functionality has to be emulated by attribute access mechanisms - so you can do whatever’s in the emulation code. Going back to the Python example, it has in the standard library the types "Enum" - classes with members that cannot be changed after being created, so they are "constants" - but the name that is given to Enum itself, can always point to another object, including for an Enum with other members:
from enum import Enum
class Frutas(Enum):
banana = "banana"
abacate = "abacate"
Frutas.banana = "maçã"
# Causa um 'AttributeError',
# mas voce pode redeclarar a classe "Frutas" com novos valores
What is the difference between a constant, a readonly variable of C#, and the C member initializer++?
That and I’m going to - I don’t know enough of these languages to know how this makes sense internally and how it’s likely to be implemented. But above, I put the difference between the #define
and the const
of the C language, which can give an idea of the diversity that can have the implementation of "initialized and then immutable values".