Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of allowing an identifier for enum?

Tags:

c

enums

Why doesn't the compiler complain when I try to assign incorrect values to variable a of type enum answer?

#include <stdio.h>

int main()
{
    enum answer {NO, YES};
    enum gender {MALE, FEMALE};

    enum answer a = 5; /* Assign an invalid value. */
    printf("answer: %d\n", a);

    a = MALE; /* Assign a value of wrong type */
    printf("answer: %d\n", a);

    return 0;
}

Here is the output:

$ gcc -std=c99 -pedantic -Wall -Wextra enum.c 
$ ./a.out
answer: 5
answer: 0

If enum doesn't lead to type-checking, then what is the point of having the syntax as:

enum [identifier] {enumerator-list}

I used answer and gender as the identifier for my enum. What is the point of allowing this syntax?

I mean this code could be very well written as

enum {NO, YES};
enum {MALE, FEMALE};

What is the point of allowing this syntax?

enum answer {NO, YES};
enum gender {MALE, FEMALE};
like image 937
Lone Learner Avatar asked Aug 12 '12 15:08

Lone Learner


People also ask

What is the point of an enum?

Enumerations make for clearer and more readable code, particularly when meaningful names are used. The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future.

What are the advantages of using enumeration variables in a program?

The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability.

Why do we use enum in C++?

In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex.

What is the use of enum in Python?

Enum is a class in python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values. The members of an enumeration can be compared by these symbolic anmes, and the enumeration itself can be iterated over.


2 Answers

Why doesn't the compiler complain when I try to assign incorrect values to variable a of type enum answer?

Because in C, an enum is practically equivalent to an int. It was standarized like it, and simply too many programs rely on that behavior for it to change.

In C++, they are distinct types and the compiler complains:

$ g++ -Wall -Wextra a.c
a.c: In function 'int main()':
a.c:8:24: error: invalid conversion from 'int' to 'main()::answer' [-fpermissive]
a.c:11:14: error: cannot convert 'main()::gender' to 'main()::answer' in assignment

What is the point of allowing this syntax?

My quick guess would be forward compatibility.

like image 77
Michał Górny Avatar answered Oct 19 '22 19:10

Michał Górny


C exposes enumeration values directly as integer while in C++ enum is a real type. Hence in C++ enum results in a type check while in C enum just represents constants of type int. Therefore integer and enum values can be intermixed in all the arithmetic operations.

like image 21
krammer Avatar answered Oct 19 '22 21:10

krammer