Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the following C++ code?

#include <iostream>
    using namespace std;
    int main()
    {
        int  const  p = 5;
        cout << ++p;
        return 0;
    }

 

  • A.5
  • B.6
  • C.Error
  • D.8

Answer: C

Correct answer: Error

p is declared const, so ++p attempts to modify a const-qualified object and the compiler rejects it.

The order of int and const is immaterial - int const p and const int p are the same declaration - so nothing is wrong with the declaration itself. It is the increment that fails, and it fails at compile time: const-ness is enforced entirely by the compiler and has no run-time representation.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.