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

Discussion

What is the output of this C code?

int main()
{
enum {ORANGE = 12, MANGO, BANANA = 11, APPLE};
printf("APPLE = %d\n", APPLE);
}

 

  • A.APPLE= 11
  • B.APPLE= 12
  • C.APPLE= 23
  • D.APPLE= 0

Answer: B

Correct answer: APPLE= 12

An enumerator without an explicit value takes the previous enumerator's value plus one, and an explicit value resets the count from that point.

So ORANGE = 12, then MANGO becomes 13, then BANANA = 11 resets the sequence, and APPLE follows at 12. Two enumerators therefore share the value 12, which C permits - enumerator values need not be distinct. The trap is expecting APPLE to continue from MANGO rather than from the explicitly reset BANANA.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.