2011-04-06 90 views
18
enum protocol { 
    ascii_prot = 3, /* arbitrary value. */ 
    binary_prot, 
    negotiating_prot = 4 /* Discovering the protocol */ 
}; 

binary_protnegotiating_prot等於4在C中有重複的枚舉值是合法的嗎?

+2

它被** [N1265 C99草案](http:// www在6.7.2.2/3「Enumeration specifiers」中使用「.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf」:「使用帶有'='的枚舉器可能產生枚舉常量,值在同一個枚舉中。「。我已經把這個答案,但http://stackoverflow.com/users/100297刪除它,因爲重複的答案:http://stackoverflow.com/a/30916409/895245我不認爲這些問題是重複的。 – 2015-06-18 17:25:55

回答

36

是的。

+0

+1爲聰明;) – Mehrdad 2011-04-06 03:34:51

+0

+1簡潔但正確。 – 2011-12-14 21:36:01

6

是; C不是關於枚舉值的。爲什麼你可能會這樣做是另一個問題,除非有理由將發現數據包與數據包一樣對待。 (其他人設計協議時可能會出現這種情況,並且您希望儘可能貼近他們的文檔。)