Javatpoint Logo
Javatpoint Logo

Global constant in C++

A global constant in the C++ programming language is a variable whose value stays constant during program execution and is declared and defined outside of any functions. The const keyword is used to declare variables as constants to ensure that a variable's value cannot be changed after initialization. Global constants are frequently employed to define values that are available from anywhere in the program, and that shouldn't change while it's running.

Syntax:

Syntax of global constant in C++ as follows:

In this instance, the global constant is called GLOBAL_CONSTANT, and its value is 20. You are unable to modify the value of this constant while the program is running once it has been defined. For numbers that shouldn't change, such as configuration settings, mathematical constants like pi, or any other value, global constants are frequently utilized.

It is imperative to take into account specific best practices while declaring global constants in C++ to maintain the organization and effectiveness of the code. The following are some essential things to remember:

  1. const Keyword Usage: It's best practice to declare constants with the const keyword to help prevent unintentional changes to the constant's value.
  2. Naming Conventions: It is important to make sure that the constant names adhere to a standard naming practice and are easily understood. It improves its readability and understanding for other developers who could come across the code in the future.
  3. Scope: Recognize the constant's range. It's critical to make sure a global constant is applicable across the entire codebase because they are accessible throughout the program.
  4. Header Files: Think about utilizing header guards and specifying global constants in header files to avoid multiple inclusions. Constants are easier to manage and organize when they are utilized in numerous source files, which is why this practice is helpful.
  5. Value Initialization: Initialize the global constant at the moment of declaration by setting its value. By doing this, you can be confident that the constant gets assigned a value before it is ever utilized in the program.

There are several reasons why using global constants can be advantageous.

  1. Readability: Code readability can be improved by using global constants, particularly when a variable is used repeatedly throughout the program.
  2. Maintainability: Rather than having to go through the entire code looking for instances of a constant value, you can easily change it in one place (the declaration).
  3. Avoiding Magic Numbers: Reducing the Use of "Magic Numbers": Global constants reduce the use of "magic numbers", which are nameless numerical constants that can make code more difficult to maintain and interpret.

Global constants are commonly used in C++ to define variables that don't change while the program runs, like configuration values, mathematical constants, and other values that shouldn't change. You can improve the readability and maintainability of your code and avoid unintentional changes to crucial values by utilizing global constants.

Furthermore, functions, classes, and other global variables are only a few of the contexts in which global constants might be employed within the program. As a result, the constant value can be quickly accessed from many areas of the codebase.

Example:

Let's take an example to illustrate the global constant in C++:

Output:

Enter the radius of the circle: 4
The area of the circle is: 50.2654

Explanation:

  1. #include <iostream>: This line adds the functionality for input and output operations provided by the input-output stream library.
  2. const double PI = 3.14159;: The value 3.14159 is declared for the global constant PI in this line. The mathematical constant pi's value is kept there.
  3. void calculate_Area(double radius): This function takes a radius as input and returns the area of a circle. It does the computation using the global constant PI and then outputs the outcome to the console.
  4. std::cout << "The area of the circle is: " << area << std::endl;: This line prints the calculated area of the circle to the console.
  5. int main(): It is where the program begins.
  6. double radius;: The radius of the circle that the user provides is stored in this variable, which is declared as type double.
  7. std::cout \\ "Enter the circle's radius: ";: This line outputs a message requesting that the user enter the circle's radius.
  8. std::cin >> radius;: This line assigns the user-supplied input to the radius variable.
  9. calculate_Area(radius);: The user-supplied radius is passed as an input to the calculate_Area function in this line.
  10. return 0;: The operating system receives 0 as a result of the program's successful execution on this line.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA