Tokens in C++: Constants
WHAT ARE CONSTANTS?
Constants are a special type of variables, whose values can be assigned only during development, while at runtime, they are treated as non-editable pieces of code. In other words, constants are variables which are set as read-only, thus preventing corruption and unrequired editing and changes. The following illustration demonstrates a variable encapsulated into a constant.

As demonstrated by the illustration, there are two independent ways of converting a variable into a constant, by declaring variables using #define or by using the const keyword alongside variable declaration. Refer to the code below to understand both the methods.
- By using #define#define PI 3.14In this case, we have added the value of π, which is 3.14. This value is constant, and we do not want to change it anytime during the runtime. Hence we have declared the value itself, ensuring data purity.
- By using constconst float gravitationalAccn = 9.81In this case, a float variable gravitationalAccn is the representation of the gravitational acceleration, which has an average value of 9.81. To ensure that the value is not changed at runtime, we use the keyword const, so that we make the variable read-only.
- The #define method, as the name suggests, uses the pre-processor directive #define to temporarily rename a particular literal with an identifier. This process does the following:
1. It renames a literal for the entire program. This means that wherever that literal was to be used, the alias can be called, and the alias will replace itself with the literal value during runtime.
2. This process of renaming occurs during pre-processor directing phase of the compilation, and hence no additional memory space is consumed for this process during active runtime.
3. Finally, this method renames the literal for the entire program without exceptions, and hence, there is no local scope bounds on the literal. In other words, the literal wears a mask for the entire program, and does no exceptions for any method, function or class.
This way of constant creation is used for values which will be in constant use, and whose value wouldn't change in the global scope. The drawback however, is the fact that reassignment of such identifiers to updated values are not possible during runtime. Additionally, multiple declarations at the pre-processor stage hinders application launch time.
The best candidate for such constant creation are static mathematical constants like π or e, whose values are kept constant, and will not change based on situations. - The const method uses the const keyword to put a read-only lock on a variable initialized with a certain value. Unlike the #define method, in this method a variable is created during runtime, which is first populated by data. This variable is then restricted using a read-only lock, such that no data can be added after declaration and initialization.
This method offers a few unique flexibilities.
1. Firstly, we can take user input, which can later be converted into a read-only variable via reassignment.int data1;
cin >> data1;
const int constantData = data1;2. Next, we can limit the scope of a constant variable to its class or function, thus freeing up the literal to be used as a name for a non-constant variable in another scope.
3. The concept of scope brings us to the fact that the we can use conditional assignment to create different values for the same constant variable.
However, there are some limitations to the process as well. Firstly, this process is executed during the active execution and runtime, and thus consumes memory, possibly slowing down the program. Additionally, declaring constants with global scope is redundant using const as the other method is a faster, less memory-intensive process.
Best candidates for this method of constant creation are physical constants which change with situations, such as gravitational acceleration, which changes based on elevation, gravitational force, tangential acceleration, etc.
Comments
Post a Comment