[英]Why am I getting error when enum type variable is initialized in file scope?
I am unable to understand the situation when I try to initialize the enum variable as global, ie, outside of any scope. 当我尝试将枚举变量初始化为全局(即,在任何范围之外)时,我无法理解情况。 For example if I try to compile below code: 例如,如果我尝试编译以下代码:
#include <stdio.h>
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;
card = club;
int main()
{
printf("Size of enum variable = %d bytes", sizeof(card));
return 0;
}
Following compilation errors happen: 发生以下编译错误:
prog.c:9:1: warning: data definition has no type or storage class
card = club;
^
prog.c:9:1: warning: type defaults to 'int' in declaration of 'card' [Wimplicit-int]
prog.c:9:1: error: conflicting types for 'card'
prog.c:8:3: note: previous declaration of 'card' was here
} card;
^
But when I put the initialization within the main() scope , no error occurs like the code below: 但是,当我将初始化放在main()范围内时,不会发生以下代码所示的错误:
#include <stdio.h>
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card;
int main()
{
card = club;
printf("Size of enum variable = %d bytes", sizeof(card));
return 0;
}
Output is : 输出为:
Size of enum variable = 4 bytes
Your first declares a global variable named club
of type enum suit
, however you cannot initialize a global variable on another line outside of a function - you must do it on the same line, like so: 您的第一个声明了一个名为club
的enum suit
类型的全局变量,但是您不能在函数外部的另一行上初始化全局变量-您必须在同一行上进行初始化,如下所示:
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card = club;
I feel your code would be more readable if you moved the enum club
definition away from the variable declaration: 我觉得如果将enum club
定义从变量声明中移开,您的代码将更具可读性:
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
};
enum suit card = club;
The extra enum
keyword is because C (unlike C++) has a different namespace for each kind of type - so you can have enum foo
and struct foo
in the same project without the names colliding because enum
and struct
type names exist in different namespaces. 额外的enum
关键字是因为C(与C ++不同)对于每种类型都具有不同的命名空间-因此,您可以在同一项目中使用enum foo
和struct foo
而不会造成名称冲突,因为enum
和struct
类型名称存在于不同的命名空间中。
(I'm personally not a fan of using typedef
- but feel free to go with that if you choose). (我个人不喜欢使用typedef
但可以选择使用它)。
This happens because 这是因为
card = club;
is an assignment statement , which needs to be in block scope (some function body) not in file scope, not an initialization . 是一个赋值语句 ,它需要在块范围(某些函数体)中,而不在文件范围中,而不是初始化中 。
If you use initialization , it'll work. 如果使用初始化 ,它将起作用。
Also note the extra changes 另请注意额外的更改
int main()
should be int main(void)
int main()
应该是int main(void)
sizeof
yields a size_t
, so you should use %zu
format specifier to print the result. sizeof
产生size_t
,因此您应该使用%zu
格式说明符来打印结果。 Example 例
#include <stdio.h>
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3
} card = club;
int main(void)
{
printf("Size of enum variable = %zu bytes", sizeof(card));
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.