2009-12-15 80 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)上。