Jay Patel
Jay Patel

Reputation: 93

What is the use of enum types and variables created from the particular enum type

I was reading about enums on stack overflow but couldn't find a satisfactory answer

I just want to know why we create enum type like

enum result {pass,fail};    //result - user defined type

I could have just written

enum{pass,fail};

Only use of enum type I can see is to make variable of that particular type Like

enum result test;

So only for this purpose is enum type used? ( to create variables of enum type) Or is there some better practical use of making variables of type enum

Sorry it may seem like a duplicate question But I did some research wasn't satisfied by answers I read

As I am a beginner in C

Upvotes: 2

Views: 187

Answers (3)

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. The first advantage is that enumerated constants are generated automatically by the compiler. Conversely, symbolic constants must be manually assigned values by the programmer.

For instance, if you had an enumerated constant type for error codes that could occur in your program, your enum definition could look something like this:

enum Error_Code
{
    OUT_OF_MEMORY,
    INSUFFICIENT_DISK_SPACE,
    LOGIC_ERROR,
    FILE_NOT_FOUND
};

In the preceding example, OUT_OF_MEMORY is automatically assigned the value of 0 (zero) by the compiler because it appears first in the definition. The compiler then continues to automatically assign numbers to the enumerated constants, making INSUFFICIENT_DISK_SPACE equal to 1, LOGIC_ERROR equal to 2, and so on. If you were to approach the same example by using symbolic constants, your code would look something like this:

#define OUT_OF_MEMORY 0
#define INSUFFICIENT_DISK_SPACE 1
#define LOGIC_ERROR 2
#define FILE_NOT_FOUND 3

Each of the two methods arrives at the same result: four constants assigned numeric values to represent error codes. Consider the maintenance required, however, if you were to add two constants to represent the error codes DRIVE_NOT_READY and CORRUPT_FILE. Using the enumeration constant method, you simply would put these two constants anywhere in the enum definition. The compiler would generate two unique values for hese constants. Using the symbolic constant method, you would have to manually assign two new numbers to these constants. Additionally, you would want to ensure that the numbers you assign to these constants are unique. Because you don’t have to worry about the actual values, defining your constants using the enumerated method is easier than using the symbolic constant method. The enumerated method also helps prevent accidentally reusing the same number for different constants.

Another advantage of using the enumeration constant method is that your programs are more readable and thus can be understood better by others who might have to update your program later. For instance, consider the following piece of code:

void copy_file(char* source_file_name, char* dest_file_name)
{
    ...
    Error_Code err;
    if (drive_ready() != TRUE)
        err = DRIVE_NOT_READY;
    ...
}

Looking at this example, you can derive from the definition of the variable err that err should be assigned only numbers of the enumerated type Error_Code. Hence, if another programmer were to modify or add functionality to this program, the programmer would know from the definition of Error_Code what constants are valid for assigning to err.

Conversely, if the same example were to be applied using the symbolic constant method, the code would look like this:

void copy_file(char* source_file, char* dest_file)
{
    ...
    int err;
    ...
    if (drive_ready() != TRUE)
        err = DRIVE_NOT_READY;
    ...
}

Looking at the preceding example, a programmer modifying or adding functionality to the copy_file() function would not immediately know what values are valid for assigning to the err variable. The programmer would need to search for the #define DRIVE_NOT_READY statement and hope that all relevant

constants are defined in the same header file. This could make maintenance more difficult than it needs to be and make your programs harder to understand.

NOTE Simply defining your variable to be of an enumerated type does not ensure that only valid values of that enumerated type will be assigned to that variable. In the preceding example, the compiler will not require that only values found in the enumerated type Error_Code be assigned to err; it is up to the programmer to ensure that only valid values found in the Error_Code type definition are used. A third advantage to using enumeration constants is that some symbolic debuggers can print the value of an enumeration constant. Conversely, most symbolic debuggers cannot print the value of a symbolic constant. This can be an enormous help in debugging your program, because if your program is stopped at a line that uses an enum, you can simply inspect that constant and instantly know its value. On the other hand, because most debuggers cannot print #define values, you would most likely have to search for that value by manually looking it up in a header file.

Upvotes: 3

Bathsheba
Bathsheba

Reputation: 234685

With enum result {pass, fail};, you can create a function with result as a parameter type:

void foo(enum result r);

As well as helping source code readability, it can also assist enormously with debugging: a good debugger will display the enumeration name for r, despite the fact that you can pass any value permissible by the enum's backing type to the function.

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83527

enum types allow you to give meaningful names to int values. In your example, you have pass and fail as the values of the enum result type. This allows you to do something like

enum result r = pass;

Alternatively, if you use numbers 0 and 1 instead, you would have to do

int r = 1;

Note that when you read the enum version, the meaning is immediately obvious. When you read the int version, you may ask "Why the value 1?" and "What does 1 stand for?".

In short, enum makes code easier for humans to read.

Upvotes: 1

Related Questions