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;
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development