Open In App

How to Define Constants in C++?

Last Updated : 15 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, constants are the tokens that are used to represent those values that cannot be changed during the program execution. It is defined only once and remains the same throughout the execution of the program. C++ provides various different methods to define constants. In this article, we will discuss such methods to define constants and see how they are different form each other.

Different Ways to Define Constants in C++

In general, we can define constants using the const keyword but C++ also provides more methods to define constants. Some of the methods to define constants in C++ are:

  1. Using const Keyword
  2. Using Macro
  3. Using enum
  4. Using constexpr

Constants defined using each of these keywords have some similar and different properties. Let’s discuss them one by one.

1. Constants Using const Keyword

This method is one of the most prominent and most basic methods of defining constants. Here, we use the keyword const as a prefix in the variable declaration.

Syntax

const variable_type variable_name  = value;

We have to assign the value to the constant variable at the declaration as we cannot change its value after declaration.

Example

C++




// C++ program to demonstrate the declaration of constant
// using const keyword
#include <iostream>
using namespace std;
  
int main()
{
    const float PI = 3.14;
  
    cout << PI << endl;
  
    // trying to change the value
    PI = 111; // error will occur here
  
    cout << PI << endl;
  
    return 0;
}


Output

main.cpp: In function ‘int main()’:
main.cpp:13:8: error: assignment of read-only variable ‘PI’
13 | PI = 111; // error will occur here
| ~~~^~~~~

2. Defining Constant Using #define

Defining constants as a macro (using #define) is different from the method using const keyword as the constants are just a placeholder for the actual value. This alias is replaced by its value during the preprocessing.

Syntax

#define constantName value

Here, ConstantName is the name which is used as an alias to its value.

Example

The below example demonstrates the declaration of constant using #define.

C++




// C++ program to define constant using  #define
#include <iostream>
using namespace std;
  
// defining constant
#define half 0.5
  
int main()
{
    int base = 10;
    int height = 10;
    // Storing the value of area in a float data type
    float area = half * base * height;
    // calculating area of triangle
    cout << "The area of right angle triangle is: " << area;
  
    return 0;
}


Output

The area of right angle triangle is: 50

3. Defining Constant Using enum

Enumeration (or enum) is a data type in which we assign some name to the integer values. We can use this property to create constant values. Enumerations are generally used where multiple constants are required and we can only use the integer values.

Syntax

enum enum_Name
{
constant1 = value;
constant2 = value;
}

Example

The below demonstrates the use of enum to create multi constants.

C++




// C++ program to demonstrate the use of enum to create
// multi constants
#include <iostream>
using namespace std;
  
// defining multiple constants using enum
enum No_Color { RED = 4, GREEN = 6, BLUE = 10 };
  
int main()
{
  
    // printing values of all balls
    cout << "The no. of Red Balls is " << RED << endl;
    cout << "The no. of Green Balls is " << GREEN << endl;
    cout << "The no. of Blue Balls is " << BLUE << endl;
    return 0;
}


Output

The no. of Red Balls is 4
The no. of Green Balls is 6
The no. of Blue Balls is 10

4. Defining Constant Using constexpr

In the C++11 and later versions, we can also use ‘constexpr’ keyword to declare constants. It its the modern and safe way to declare the constants in C++.This ‘constexpr’ keyword ensures that the value is processed at compile time instead of runtime to improve the execution efficiency.

Syntax

constexpr DataType constantName = value;

Example

The below example demonstrates the use of constexpr to define constant.

C++




// C++ program to demonstrate the use of constexpr to define
// constant.
#include <iostream>
using namespace std;
  
// Using constexpr to declare a constant variable
constexpr int Num = 5;
  
// Using constexpr to define a constant function
constexpr int factorial(int n)
{
    return (n <= 1) ? 1 : n * factorial(n - 1);
}
  
int main()
{
    // Using the constexpr constant variable
    constexpr int res = factorial(Num);
    cout << "Factorial of " << Num << " is: " << res
         << endl;
  
    return 0;
}


Output

Factorial of 5 is: 120

Conclusion

In conclusion, constants in C++ programming are used to represent values that remain unchanged during program execution. The const keyword and other methods like #define, enum, and constexpr, allows us to declare and define constants as required. We can choose any of the above method based on our convenience and



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads