gdb
gdb

Reputation: 7789

Is it legal to have duplicate enum values in C?

enum protocol {
    ascii_prot = 3, /* arbitrary value. */
    binary_prot,
    negotiating_prot = 4 /* Discovering the protocol */
};

Both binary_prot and negotiating_prot equals to 4?

Upvotes: 22

Views: 10546

Answers (2)

geekosaur
geekosaur

Reputation: 61369

Yes; C is not particular about enum values. Why you might do that is another question, unless there's some reason to treat the discovery packet the same as data packets. (Which there might well be when someone else designed the protocol and you want to stick as closely as possible to their documentation.)

Upvotes: 6

James McNellis
James McNellis

Reputation: 355049

Yes.

Upvotes: 42

Related Questions