C++ Constants

C++ FIRST PROGRAM

Constants are fixed values. They are not able to change once declared. They called as literals.
Constants can be any data type.
The keyword 'const' is used to declare constant variables.
We must assign value when declaring const variable
Syntax to declare constants:
    const data_type variable_name = value;
            or
    data_type const variable_name = value;
Examples:
    const float PI = 3.14f;
    string const name = "Naga";

Example Program

             #include <iostream>
             using namespace std;
             int main(){
                     const float PI=3.1415f;
                     cout << PI << endl;
                     return 0;
             }


Note: It is not possible to reassign the values to const variables. If we try to re assign values program will throw an error.
    "error: assignment of read-only variable ‘PI’"
Some IDE's throw error while re assigning value only based on features available.

define const with #define preprocessor

          #include <iostream>
          using namespace std;
          #define PI 3.1415
          int main(){
                  cout << PI << endl;
                  return 0;
          }

 Contact Us:

          codecalculatenr@gmail.com