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

Discussion

Whats is the output of this C code?

void main()
{
int const k = 11;
k++;
printf("k is %d", k);
}

 

  • A.k is 12
  • B.Error because const and int are used together
  • C.garbage value
  • D.Error, because a constant variable cannot be changed

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.