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

Discussion

What will be the output of the following C code?

#include <stdio.h>
enum hi{a,b,c};
enum hello{c,d,e};
main()
{
    enum hi h;
    h=b;
    printf("%d",h);
    return 0;
}

 

  • A.2
  • B.1
  • C.Error

Answer: C

Correct answer: Error

Both enumerations declare an enumerator called c, and enumerators share the ordinary identifier namespace at file scope - so the second declaration is a redefinition and the compiler rejects it. Unlike structure tags, enumerators are not scoped to their own type in C. (C++'s enum class was introduced to fix exactly this.)

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

Post your comment

Please log in or register to post a comment.