C99 Enum - Need Clarification

放肆的年华 提交于 2019-12-25 11:54:26

问题


I have reviewed this but the accepted answer doesn't make sense to me. I should be able to define an enum in C99 as

enum WeekDays
{
    MON, TUES, WED, THURS, FRI, SAT, SUN
}days;

and utilize the enum as follows in main as

days = FRI;
if (days == FRI)
{
    printf("Thank God it's Friday!");
}

Why the additional work in the accepted answer to utilize the enum?


回答1:


Your code should work. In general though the accepted answer you point to is better programming practice. It's desirable to separate the declaration of new types from the use of those types. For example if you wrote a library for day manipulation, you might include the enum weekdays in that library. But that would be a bad place to define a variable for your program to use. Over time programmers have found this sort of separation valuable. It generally helps code be more readable



来源:https://stackoverflow.com/questions/46353655/c99-enum-need-clarification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!