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
?
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.
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.
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.
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.
It's definitely not "too technical".
"typedef" and "enum" are two completely different things.
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 ;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With