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

Discussion

Can we have enum containing enum with the same constant?

#include "stdio.h"

enum Yenum{
       C, CPP, Java
};

enum Xenum{
    C, CPP, Java, Yenum
};

int main()
{
  enum Xenum var;
  printf("%d",sizeof(var));
  return 0;
}

 

  • A.Enum can contain enum but duplicate constants are not allowed
  • B.Enum can't contain enum
  • C.Enum can contain enum but not accessible
  • D.This code will produce compilation error

Answer: A

Correct answer: Enum can contain enum but duplicate constants are not allowed

The two enumerations both declare C, CPP and Java, and enumerators occupy the ordinary identifier namespace at file scope - so the second set are redefinitions and the program will not compile. Naming one enumeration as a member of another is not the difficulty; the repeated constant names are.

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

Post your comment

Please log in or register to post a comment.