C99枚举-需要澄清 [英] C99 Enum - Need Clarification

查看:76
本文介绍了C99枚举-需要澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经审查了,但已被接受答案对我来说没有意义。我应该可以在C99中将枚举定义为

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?

推荐答案

您的代码应该可以使用。通常,尽管您指出的是可接受的答案,但它是更好的编程实践。最好将新类型的声明与这些类型的使用分开。例如,如果您编写了一个用于日操作的库,则可以在该库中包含枚举工作日。但这对定义供程序使用的变量将是一个糟糕的地方。
随着时间的流逝,程序员发现这种分离很有价值。通常,它有助于使代码更具可读性

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

这篇关于C99枚举-需要澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆