我有一个非常简单的 C 代码:
#include<stdio.h>
int main()
{
enum boolean{true,false};
boolean bl=false;
if(bl==false)
printf("This is the false value of boool\n");
boolean bl1=true;
if(bl1==true)
{
printf("This is the true value of boool\n");
}
return 0;
}
我只是想使用 enum 类型的变量。但它给出了以下错误:
tryit4.c:5: error: ‘boolean’ undeclared (first use in this function)
tryit4.c:5: error: (Each undeclared identifier is reported only once
tryit4.c:5: error: for each function it appears in.)
tryit4.c:5: error: expected ‘;’ before ‘bl’
tryit4.c:6: error: ‘bl’ undeclared (first use in this function)
tryit4.c:8: error: expected ‘;’ before ‘bl1’
tryit4.c:9: error: ‘bl1’ undeclared (first use in this function)
我看不出有任何理由。您能解释一下这可能是什么原因吗?
最佳答案
在 C 中,有两种(实际上更多,但我保留它)类型的命名空间:普通标识符和标记标识符。结构、 union 或枚举声明引入了标记标识符:
enum boolean { true, false };
enum boolean bl = false;
从中选择标识符的 namespace 由周围的语法指定。在这里,它以 enum
为前缀。如果你想引入一个普通的标识符,把它放在一个 typedef 声明中
typedef enum { true, false } boolean;
boolean bl = false;
普通标识符不需要特殊语法。如果愿意,您也可以声明一个标签和普通标签。
关于c - 枚举声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1909825/