2009-12-15 76 views
3

的声明我有一个非常简单的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; 
    } 

我只是想用枚举类型的变量。但它给以下错误:

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) 

我没有看到任何理由。你能解释一下可能的原因吗?

+0

有些gyz可能有太多的空闲时间,没有比在互联网上生气更好的事情了。 – 2009-12-15 19:44:38

+0

@mawia:点击你的用户名,选择“活动”,然后浏览你所提出的所有问题,并从每个问题中选择一个最佳答案。 – wallyk 2009-12-15 19:48:22

+0

感谢所有的答复! – mawia 2009-12-15 20:01:24

回答

7

当你声明enum boolean { true, false },您声明一个名为enum boolean的类型。在声明后您必须使用该名称:enum boolean,而不仅仅是boolean

如果你想有一个较短的名称(如刚boolean),你必须把它定义为一个别名原来的全名

typedef enum boolean boolean; 

如果你愿意,你可以声明同时enum boolean类型和在boolean别名上的一个声明

typedef enum boolean { true, false } boolean; 
7

你必须声明变量的类型为枚举布尔型,而不仅仅是布尔型。使用typedef,如果你发现写入enum boolean b1 = foo();繁琐。

10

在C中,有两个(实际上更多,但我保留它)类型的名称空间:普通标识符和标记标识符。甲结构,联合或枚举声明引入标签识别符:

enum boolean { true, false }; 
enum boolean bl = false; 

从中选择的识别符的命名空间由围绕语法指定。在这里,前面加上enum。如果你想介绍一个普通的标识,把它放在一个typedef声明

typedef enum { true, false } boolean; 
boolean bl = false; 

普通标识符不需要特殊的语法内。如果你愿意的话,你也可以声明一个标签和普通标签。

+0

对于第39页中的enum en和en rich的enum的声明风格,你会怎么说? ex enum boolean {yes,no}。 – mawia 2009-12-15 19:52:12

+0

哦,谢谢,我知道了! – mawia 2009-12-15 19:56:46

+0

@mawia:在'enum boolean {yes,no}'中,名称'boolean'被用作标签标识符。在'typedef enum {true,false} boolean'中,名称'boolean'被用作普通标识符。如果它是在'enum'或'struct'或'union'之后出现的,那么它就是一个标签标识符。 – benzado 2009-12-15 19:57:08

3

您声明枚举,但不是类型。你想要的是

typedef enum{false, true} boolean; // false = 0 is expected by most programmers 

仍有多个问题与此:
* truefalse在许多C编译器
*是保留字明确使用真假违背用C布尔表达式的一般做法,其中零表示假,任何非零意味着真。例如:

int found = (a == b); 


编辑:这适用于GCC 4.1.2:

[[email protected] ~]$ ./a.out 
This is the false value of boool 
This is the true value of boool 
[[email protected] ~]$ cat t2.c 
#include<stdio.h> 
int main() 
{ 
     typedef enum {true,false} boolean; 
     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; 
} 
+0

第一个问题可以通过添加下划线前缀或其他东西来解决。 – 2009-12-15 19:47:24

+0

感谢您的回复, 嗨不幸的是问题仍然存在,即使在定义枚举类型后仍然存在。 – mawia 2009-12-15 19:47:41

+0

是的前prog编码完美typedef像typedef枚举{假,真}布尔; 但是你会如何对kernigham和richie声明一个枚举的39页给出的风格说! – mawia 2009-12-15 19:53:51

7

这真的是一个好主意,这样定义您的枚举:

typedef enum { 
    False, 
    True, 
} boolean; 

几个原因:

  • truefalse(小写)很可能保留字
  • 假为1,真正的为0可以使你的逻辑问题后
+2

+1用于捕获以其他顺序定义它们的问题。 – qid 2009-12-15 21:04:14

1

像以前的答案演示了,使用的typedef:

typedef enum { true, false } boolean; 
+6

如果有以前的答案,有什么好处是添加重复的答案? – GManNickG 2009-12-15 19:48:06

0

FAQ - 的特征的列表C++支撑该C不包括:

bool keyword

那常见问题是有点不准确,并作为

添加#include <stdbool.h>到您的代码,它会编译为C99“是C++支持其C89不包括的功能列表”中更好的说明在试图实现C99的编译器(如gcc)上。