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

Discussion

What is the output of this C code?

int main()
{
const int a;
a = 32;
printf("a is %d", a);
return 0;
}

 

  • A.a is 32
  • B.Compile time error
  • C.Run time error
  • D.none

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.