Search⌘ K

C++ Constants/Literals

Explore how to declare and use constants in C++ to create variables whose values remain unchanged throughout program execution. Understand why initializing constants at declaration is essential, and learn common errors to avoid when working with fixed values in your code.

Constants or literals #

Let’s write a program in which we will overwrite the value of a variable.

Run the code below and see the output!

C++
#include <iostream>
using namespace std;
int main() {
int number = 10;
cout << "Number = " << number << endl;
number = 20;
cout << "Number = " << number << endl;
number = 30;
cout << "Number = " << number << endl;
}

In the above code, we have declared a variable number. We see that we can overwrite the value of the number during the execution of the program. Initially, ...