Question:

What is the benefit of using enum to declare a constant?

Answer:

Using the enum keyword to define a constant can have several benefits. First, constants declared with enumare automatically generated by the compiler, thereby relieving the programmer of manually assigning unique values to each constant. Also, constants declared with enum tend to be more readable to the programmer, because there is usually an enumerated type identifier associated with the constant's definition.

Additionally, enumerated constants can usually be inspected during a debugging session. This can be an enormous benefit, especially when the alternative is having to manually look up the constant's value in a header file. Unfortunately, using the enum method of declaring constants takes up slightly more memory space than using the #define method of declaring constants, because a memory location must be set up to store the constant.

Here is an example of an enumerated constant used for tracking errors in your program:

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

Keywords:

© 2017 QuizBucket.org