- An enum in C is a user-defined data-type and it consists set of named constant integer.
- Two enums couldn't hold duplicate members but its member can have same value.
- Enums can hold an enum within it.
- Enums can hold negative values.
- Enum's index always starts from 0(same as array)
👉Drawback of Enums are:
- we have to inialize enum's object with all enum members again in main after declare in enums itself.
- An enumeration obeys the scope rules.
- we can declare unnamed enumerator data type.
- An enumeration tag also follows the scope rules. So enumeration tag should be different from the tag of structure, union or the enumeration type.
- If we do not assign the value to the enumeration constant then the compiler automatically assigns the value to the enumeration constant. We can assign the value to the enumeration constant in any order, unassigned constant get the value from the previous constant and plus one.
- The value assigned to the enums member should be an integral constant and within the range of integer.
int main(int argc, char *argv[])
{
//declaration of enum
enum ERROR_LIST { ERROR
=9999999999,LOG_ERROR,FLAS_ERROR=0};
//Assign Mon to enumeration variable
enum ERROR_LIST eGetError = ERROR;
printf("ERROR = %d\n",eGetError);
return 0;
}
//Assign Mon to enumeration variable
enum ERROR_LIST eGetError = ERROR;
printf("ERROR = %d\n",eGetError);
return 0;
}
👉Enums VS Macros:
- An enums increases the readability of the code and easy to debug in comparison to the macro.
- All elements of enums grouped together which is not possible with macro.
- enum in C define new type but the macro does not define a new type.
- enum follow scope rules and compiler automatic assigns the value to its member constant.
- enum in C type is an integer but the macro type can be any type.
Comments
Post a Comment