Constant variables can be created in CPP

Q.  Constant variables can be created in CPP by using ________ .
- Published on 19 Oct 15

a. enum
b. const
c. #define
d. All of these
e. None of these

ANSWER: All of these
 

    Discussion

  • Nihal   -Posted on 08 Oct 15
    You can declare constant variable by using enum, const, #define.
    enum Color { RED, GREEN, BLUE };
    int main() {

    Color c;

    c = RED;
    cout << "RED = " << c << endl;

    c = GREEN;
    cout << "GREEN = " << c << endl;

    c = BLUE;
    cout << "BLUE = " << c << endl;

    return 0;
    }


    Output:
    RED = 0
    GREEN = 1
    BLUE = 2
    const keyword is used to make a variable constant. You cannot change its value.
    .Another mechanism to define constant values is the use of preprocessor
    #define PI 3.14159

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)