0

I'm taking the following example from The Fundamentals of Programming course:

enum color = { red, orange, yellow };
enum fruit = { apple, orange, kiwi}; // error: orange is redefined
int kiwi = 42; // error: kiwi is redefined

The author added a note:

An enumeration with = will give the identifier the provided value, which remains as start value for the next enumerators.

Is the code above valid? I mean, when should we (not) use = to create enumerations?

Trying to compile:

enum fruit = { apple, orange, kiwi};
main() {}

I get the following error:

$ gcc main.c 
main.c:1:12: error: expected identifier or ‘(’ before ‘=’ token
 enum fruit = { apple, orange, kiwi};

If I remove the = character, the file is compiled fine.

So, what's the usage of the equal character?

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 6
    Is the book about C? Of course this syntax is not valid in C. – ouah Jan 25 '15 at 12:48
  • 1
    And it seems like the author meant something like `enum color {red = ProvidedValue, ...};` – Iharob Al Asimi Jan 25 '15 at 12:50
  • The quote you show is about giving individual names inside an enumeration a specific value, e.g. `enum fruit { orange, kiwi = 5, apple };` – Some programmer dude Jan 25 '15 at 12:51
  • @dasblinkenlight The course is in Romanian, I translated the title... Fixed. :-) – Ionică Bizău Jan 25 '15 at 12:52
  • 1
    @ouah I guess so, [see my previous question too](http://stackoverflow.com/q/28136408/1420197). I will tell the author then. – Ionică Bizău Jan 25 '15 at 12:54
  • 1
    To be brutally frank, if that code comes from a textbook, it might be time to find a different textbook. – NPE Jan 25 '15 at 12:55
  • @mafso I do know that. I [worked with C# in the past](http://stackoverflow.com/search?q=user%3A1420197+%5Bc%23%5D) and we learned C and C++ at school. But I want to make clear the fact that this snippet is wrong, even the course is about C or C#. – Ionică Bizău Jan 25 '15 at 13:02
  • Yes, saw that after looking at the linked question again. So... where is this `enum fruit = { apple, orange, kiwi};` example from? In C, this simply is a syntax error (so scope and similar things don't apply). Is this question about redefining symbols or about the syntax error? – mafso Jan 25 '15 at 13:05
  • 1
    I was asking about this syntax error, since the code is taken from the course support textbook. I will ask the author to correct the mistake. – Ionică Bizău Jan 25 '15 at 13:07
  • Perhaps give the Romanian title (and the chapter) to allow others reading that book to find this question if they stumble across the same typo. – mafso Jan 25 '15 at 13:31

1 Answers1

2

The line,

enum color = { red, orange, yellow };

is a syntax error in C (also in C++). In general, if a compiler complains about a syntax error, it is practically always right (make sure to use the correct dialect; -std=c99 -pedantic for C99 with gcc, for example).

The quote is talking about a different use of the equal sign =, as in

enum color { red, orange = 4, yellow };

where red is 0, orange is 4, and yellow is 5.

The comments in the first snippet are about the redefinition of symbols, which is not allowed in the same scope and namespace (with few exceptions, but even for those they must declare the same thing). After correcting the syntax errors, the example and the comments make sense, I hope:

enum color { red, orange, yellow };
enum fruit { apple, orange, kiwi }; // error: orange is redefined
int kiwi = 42; // error: kiwi is redefined
mafso
  • 5,433
  • 2
  • 19
  • 40