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

Discussion

What is the output of this C code?

#include <stdio.h>
    enum birds {SPARROW, PEACOCK, PARROT};
    enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
    int main()
    {
        enum birds m = TIGER;
        int k;
        k = m;
        printf("%d\n", k);
        return 0;
    }

 

  • A.Compile time error
  • B.1
  • C.8

Answer: C

Correct answer: 8

Enumerators in C are simply integer constants, and an enumeration type is compatible with an integer type. Nothing prevents assigning TIGER, an enumerator of animals with value 8, to a variable of type enum birds.

The compiler may warn but is not required to reject it, and the value stored is 8, so that is what is printed. This looseness distinguishes C from C++ and from languages with genuine enumerated types - a C enumeration provides names and documentation, not type safety.

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

Post your comment

Please log in or register to post a comment.