这两个枚举声明有什么区别? [英] What's the difference between these two enum declarations - C?
问题描述
DEFINE
/ const int
,正确吗? 这两个声明有什么区别?
#include< stdio.h>
//方法1
枚举日{
星期一,
星期二
};
int main()
{
//方法1
今天的日子;
明天明天;
today = Monday;
明天=星期二;
if(today< tomorrow)
printf(yes\\\
);
//方法2
枚举{星期一,星期二}天;
days = Tuesday;
printf(%d\\\
,days);
return 0;
}
枚举应优先于当您想要声明只能从有限范围的 c 相关,相互排斥的值。所以这个星期的日子是一个好的例子,但这将是一个恶劣的例子。
枚举AboutMe
{
myAge = 27,
myNumberOfLegs = 2,
myHouseNumber = 54
};
回到你的代码示例;第一种方法声明一个名为 enum days
的类型。您可以使用此类型声明任意多的变量。
第二种方法声明单个变量 >枚举{...}
。您不能声明该类型的任何其他变量。
I've started learning C and have reached the point of enums. An enum is basically a preferred alternative to DEFINE
/ const int
, correct?
What's the difference between these two declarations?
#include <stdio.h>
// method 1
enum days {
Monday,
Tuesday
};
int main()
{
// method 1
enum days today;
enum days tomorrow;
today = Monday;
tomorrow = Tuesday;
if (today < tomorrow)
printf("yes\n");
// method 2
enum {Monday, Tuesday} days;
days = Tuesday;
printf("%d\n", days);
return 0;
}
An enumeration should be preferred over #define
/const int
when you want to declare variables that can only take on values out of a limited range of related, mutually exclusive values. So the days of the week is a good example, but this would be a bad example:
enum AboutMe
{
myAge = 27,
myNumberOfLegs = 2,
myHouseNumber = 54
};
Going back to your code example; the first method declares a type called enum days
. You can use this type to declare as many variables as you like.
The second method declares a single variable of type enum { ... }
. You cannot declare any other variables of that type.
这篇关于这两个枚举声明有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!