CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
What is the benefit of using an enum rather than a #define constant?
Category C Interview Questions
Rating (4.5) By 123 users
Added on 10/22/2004
Views 17642
Rate it!
Answers:

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 3

values 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.



by using enum constants declared by compiler itself and it reduces the time but by using #define ourself declare the constants



 Posted by: RAJASEKHAR    

Contact RAJASEKHAR  Contact RAJASEKHAR

Using enum, developer could represents how these constants are related to eachother.But #define wont represent it.



 Posted by: Satheesh    

Contact Satheesh  Contact Satheesh

I can say that two major difference b/w enumerated constant & sympolic constants.
1. Symbolic constant may be of any type like char,integer,float but enum constant are integer constants.
2. while using enumerated data type variable inadvertent run time error of loading error values can be neglected as these variables can have values only from the enumerated constants.



 Posted by: A.Mohan Kumar    

Contact A.Mohan Kumar  Contact A.Mohan Kumar

the true difference between the 2 is that
1)#define has global effect where as enum has a effect in the local block.
2)disadvantage of enum is that we have no control over its size.



 Posted by: Revz    

Contact Revz  Contact Revz

I would like to add one more point to the above listed benefits
ENUM actualy look like this
enum e_tag{
a, b, c, d=20, e, f, g=20, h
}var
The names declared inside the enumeration are constants with int type. Their values are these:

a == 0
b == 1
c == 2
d == 20
e == 21
f == 22
g == 20
h == 21

The only use for these things is to give a better-scoped version of this:

#define a 0
#define b 1
you reduce the number of #define statements in your program.



 Posted by: PREETI    

Contact PREETI  Contact PREETI


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
How are portions of a program disabled in demo versions?
View Answer
Is it better to use a macro or a function?
View Answer
What is the difference between #include <file> and #include ?file??
View Answer
Can you define which header file to include at compile time?
View Answer
Can include files be nested?
View Answer
How many levels deep can include files be nested?
View Answer
How can type-insensitive macros be created?
View Answer
What are the standard predefined macros?
View Answer
What is a pragma?
View Answer
What is #line used for?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/1028/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

1.04