0

I have reviewed this but the accepted answer doesn't make sense to me. I should be able to define an enum in C99 as

enum WeekDays
{
    MON, TUES, WED, THURS, FRI, SAT, SUN
}days;

and utilize the enum as follows in main as

days = FRI;
if (days == FRI)
{
    printf("Thank God it's Friday!");
}

Why the additional work in the accepted answer to utilize the enum?

Mushy
  • 2,535
  • 10
  • 33
  • 54
  • What is your question? Are you confusing C and C++? – fukanchik Sep 21 '17 at 21:52
  • @fukanchik Perhaps I am confusing c99 and c++11. I recall when learning c that enums and struts can declare a variable in the definition but apparently this is not recommended as the answer below stipulates. – Mushy Sep 21 '17 at 21:57
  • The accepted answer to that question is actually incorrect. The error described in the question occurs when the two lines of code appear at file scope. See my recent comment on [the question](https://stackoverflow.com/q/1102542/827263). – Keith Thompson Sep 21 '17 at 22:10

1 Answers1

1

Your code should work. In general though the accepted answer you point to is better programming practice. It's desirable to separate the declaration of new types from the use of those types. For example if you wrote a library for day manipulation, you might include the enum weekdays in that library. But that would be a bad place to define a variable for your program to use. Over time programmers have found this sort of separation valuable. It generally helps code be more readable

Sam Hartman
  • 6,210
  • 3
  • 23
  • 40
  • I can appreciate the knowledge you bring in this answer. What I represent above must be the old way of defining and using an enum. Thank you. – Mushy Sep 21 '17 at 21:55
  • @https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/3/html/System_Administration_Guide/s1-swap-adding.html if my answer answered your question please accept it – Sam Hartman Sep 21 '17 at 22:01