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.1) 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 FILE_NOT_FOUND equal to 3, 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 3values by the programmer. 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 these 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.2) 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.3) 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.
C
Topic: Variables
What is the benefit of using an enum rather than a #define constant?
Browse random answers:
How do I write a function that takes variable number of arguments?
What are the different properties of variable number of arguments?
Define the scope of static variables.
What are volatile variables?
What is the benefit of using an enum rather than a #define constant?
Constant volatile variable declaration is possible or not? if give any one example and reason?
What is an lvalue?
Can a variable be both constant and volatile ?
What are register variables? What are the advantages of using register variables?
Can we specify variable field width in a scanf() format string? If possible how?
Difference between pass by reference and pass by value?
Left shifting a number by 1 is always equivalent to multiplying it by 2 ?
Bitwise & and | are unary operators?
What is scope & storage allocation of register, static and local variables?
What is the difference between 'for' and 'while' loops?
Swap two variables without using third variable.
Are the variables argc and argv are always local to main?
What are the two forms of #include directive?
Write a program to swap two numbers using a temporary variable.
Where is the auto variables stored?
What is a constant variable?
What are the advantages of auto variables?
Is it acceptable to declare/define a variable in a C header?
Difference between : - 1)Global variable and Local variable , 2)Static variable and Global variable ?
Differentiate between an internal static and external static variable?
What does a static variable mean?
How can you determine the maximum value that a numeric variable can hold?
In order to properly use a variable?
Can a variable be both const and volatile?
What is static identifier?
Constant volatile variable declaration is possible or not? if give any one example and reason.?
What is the difference between declaring a variable and defining a variable?
Are the variables argc and argv are local to main()?
Can static variables be declared in a header file?
Where does global, static, local, register variables, free memory and C Program instructions get stored?
Are the variables argc and argv are always local to main?
Where are the auto variables stored? What is the use of register variables?
What is difference between static and global static variable?
How much memory does a static variable takes?
What is a pointer variable?
What is the type of the variable b in the following declaration? #define FLOATPTR float*FLOATPTR a,b;
What is storage class and what are storage variable ?
Declaration of variables in c?
Explanation of value of a variable in c programming language by examples and questions and answers
How to swap the content of two variables without a temporary variable
Where in memory are my variables stored?
Do variables need to be initialized?
Should variables be stored in local blocks?
What is a register variable?
What is scope & storage allocation of extern and global variables?
What is the difference between 'for' and 'while' loops?
Are the variables argc and argv are local to main?
How do I print the contents of environment variables?
Explain the variable assignment in the declaration