Discussion
What is the output of this C code?
int main()
{
const int a;
a = 32;
printf("a is %d", a);
return 0;
}
Answer: B
Correct answer: Compile time error
A const object must be given its value in its declaration, because there is no later opportunity: any assignment to it afterwards is an attempt to modify a const-qualified object, which the compiler rejects.
So both lines are problematic - const int a; declares a const object with no initialiser, and a = 32; then tries to assign to it. Either alone would be diagnosed. The correct form is const int a = 32;, initialising at the point of declaration.
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