Discussion
Whats is the output of this C code?
void main()
{
int const k = 11;
k++;
printf("k is %d", k);
}
Answer: D
Correct answer: Error, because a constant variable cannot be changed
The declaration int const k = 11; makes k a const-qualified object, and k++ attempts to modify it, which the compiler rejects.
Note that int const and const int are equivalent - the qualifier may be written on either side of the type - so there is nothing wrong with the combination itself. It is the increment that fails. The error is a compile-time diagnostic, not a run-time fault, since const-ness is enforced entirely by the compiler.
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