Constants c++ programming language.

C++ Programming Tutorial

Constants c++ programming language.


Constant does not change during the execution of the program.Constants refer to fixed values that the program may not modify and they are called literals.

How to declare constant? Let's look at the first of its various methods with examples. In which we will declare constant variable using const keyword.






#include <iostream> using namespace std; int main() { const float PI = 3.14; cout<<"Value of PI "<<PI; return 0; }
Output:
Value of PI 3.14

If you talk about constant in the above program, now you can not change the value of PI = 3.14 again anywhere in the program. If you remove the const keyword then the value of the variable will never change the value. Can not change run time and compile time.

#define constants

#include <iostream> using namespace std; #define PI 3.14 int main() { cout<<" Value of PI: "<<PI; return 0; }
Output:
Value of PI: 3.14

Wherever you use PI. Which you understand that where you are writing PI, you are not writing PI value 3.14, that is, when you process your code to compile, the compiler will get constant value instead of PI.