Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The importance of c enumeration (typedef enum) [duplicate]

Tags:

I recently saw this in an answer that was posted for me:

typedef enum {     NO_OP,     ADDITION, } operator_t;  int main() {     operator_t operator = NO_OP; } 

What is typedef enum and why should we use it? I googled and found the following: http://www.programiz.com/c-programming/c-enumeration

Right now it sounds slightly too technical for me so I don't think I understand what is going on or why anyone would use that.

Bonus (optional): What type of variable is the operator_t?

like image 851
Asperger Avatar asked Dec 16 '15 22:12

Asperger


People also ask

What is the importance of enumerated type?

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 is the use of typedef and enum in C?

There are two different things going on there: a typedef and an enumerated type (an "enum"). A typedef is a mechanism for declaring an alternative name for a type. An enumerated type is an integer type with an associated set of symbolic constants representing the valid values of that type.

Why do we use enumeration in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What are the advantages of using enumeration variables in C?

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.


2 Answers

  1. It's definitely not "too technical".

  2. "typedef" and "enum" are two completely different things.

  3. The basic reason to have "enums" is to avoid "magic numbers":

    Let's say you have three "states": STOP, CAUTION and GO. How do you represent them in your program?

    One way is to use the string literals "STOP", "CAUTION" and "GO". But that has a lot of problems - including the fact that you can't use them in a C "switch/case" block.

    Another way is to Map" them to the integer values "0", "1" and "2". This has a lot of benefits. But seeing "STOP" in your code is a lot more meaningful than seeing a "0". Using "0" in your code like that is an example of a "magic number". Magic numbers are Bad: you want to use a "meaningful name" instead.

Before enums were introduced in the language, C programmers used macros:

   #define STOP 0    #define CAUTION 1    #define GO 2 

A better, cleaner approach in modern C/C++ is to use an enum instead:

enum traffic_light_states {   STOP,   CAUTION,   GO };  

Using a "typedef" just simplifies declaring a variable of this type:

typedef enum {   STOP,   CAUTION,   GO } traffic_light_states_t ;  
like image 79
paulsm4 Avatar answered Oct 08 '22 02:10

paulsm4


typedef is used to define an alternative name for an existing type. The enum could have declared like this:

enum operator_t {     NO_OP,     ADDITION, }; 

and then you could declare a variable of this type like so:

enum operator_t x = NO_OP; 

This is kind of verbose so you would use typedef to define a shorter alias for this type:

typedef enum operator_t operator_t; 

This defines operator_t to mean the type enum operator_t allowing you to initialize a variable like so:

operator_t x = NO_OP; 

This syntax:

typedef enum {     NO_OP,     ADDITION, }   operator_t; 

does the whole process in one step, so it defines an (untagged or tagless) enum type and gives it the alias operator_t.

Bonus: operator_t is an enum data type; read more about it here: https://en.wikipedia.org/wiki/Enumerated_type

like image 35
Ryan Fitzpatrick Avatar answered Oct 08 '22 02:10

Ryan Fitzpatrick