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;
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development