c - 枚举声明错误

标签 c

我有一个非常简单的 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/

相关文章:

c - 在C中的两个字符串之间添加空格

c++ - libusb 在 IDE 中不可见

c - 调用 daemon() 和调用 fork()、setsid()、fork() 等有什么区别?

c - 如何确定此可执行文件在启动时崩溃的原因? (*nix 上的 C)

c - 分配顶级数组

c++ - 使用 MPI Scatter 分配数字 - 错误

c - 如何处理c中的结构?

c++ - 遍历数组的简单任务。这些解决方案中哪个最有效?

创建一个以运行时尺寸作为输入的矩阵

c - 如何使用 GLUT 进入指定分辨率的全屏模式?