简体   繁体   English

为什么在文件范围内初始化枚举类型变量时出现错误?

[英]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: 您的第一个声明了一个名为clubenum 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 foostruct foo而不会造成名称冲突,因为enumstruct类型名称存在于不同的命名空间中。

(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.

相关问题 为什么我在C中获取参数初始化错误? - Why I am getting parameter is initialized error in C? 尝试从.c文件中的标头使用结构时,为什么会出现类型错误? - Why am I getting a type error when trying to use a struct from a header in my .c file? 为什么我在重新声明变量时没有收到错误消息? - Why am I not getting an error when re-declaring a variable? 将数字传递给期望枚举的函数时,为什么没有得到错误(或至少警告)? - Why am I not getting an error (or at least warning) when I pass a number to a function that is expecting an enum? 为什么我在 C 的枚举中收到“错误:错误指令”? - Why am I getting “Error: bad instruction” in an enum in C? 文件范围内的变量未使用其他变量初始化 - Variables at file scope are not getting initialized using other variable 为什么在声明可变大小的C数组时,我没有收到编译错误? - Why am I not getting a compile error when declaring a C array with variable size? 为什么在 C 中将变量乘以常量时会出现“预期的表达式”错误? - Why am I getting an "expected an expression" error when multiplying a variable by a constant in C? 为什么在 Windows 上使用 dirent.h 时出现类型不完整的错误? - Why am I getting an incomplete type error when using dirent.h on Windows? 为什么我得到,变量未初始化错误? - Why do I get, variable is not initialized error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM