Constants are the tokens in C++ that stand in for values that cannot be altered while the program me is running. It has a single definition and stays that way while the program me runs. There are numerous ways to define constants in C++. We will talk about these techniques for defining constants and how they vary from one another in this article.
Syntax
Const data_type Variable_name = Value/Number
Types of Constants/Literals
- Integer Constant
Example
#include <iostream> using namespace std; int main() {    // Integer Constants    const int x = 10;    const int y = 15;    // z is initialized with the value of x + y    const int z = x + y;    // Display    cout << "Addition: " << z << endl;    return 0; }
Output
Addition: 25
- Â Floating point Constant
Example
#include <iostream> using namespace std; int main() {    // Floating-Point Constants    const float x = 7.5;    const float y = 9.3;    // z is initialized with the value of x + y    const float z = x + y;    // Display    cout << "Addition: " << z << endl;    return 0; }
Output
Addition: 16.8
- Character type Constant
Example
#include <iostream> using namespace std; int main() {    // Character Type Constant    const char x = 'S';    // Display    cout << "Character: " << x<< endl;    return 0; }
Output
Character: S
- String Constant
Example
#include <iostream> using namespace std; int main() {    // String Constants    const string x = "Ramkrishna";    // Display    cout << "String is: " << x << endl;    return 0; }
Output
String is: Ramkrishna
- Boolean Constant
Example
#include <iostream> using namespace std; int main() {    // String Constants    const bool x = true;    // Display    cout << "boolean is: " << x << endl;    return 0; }
Output
boolean is: 1