About enumeration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bindiya182003
    New Member
    • Oct 2007
    • 16

    About enumeration

    I am a bit confused here.

    enum DAY /* Defines an enumeration type */
    {
    saturday, /* Names day and declares a */
    sunday = 0, /* variable named workday with */
    monday, /* that type */
    tuesday,
    wednesday, /* wednesday is associated with 3 */
    thursday,
    friday
    } workday;

    The value 0 is associated with saturday by default. The identifier sunday is explicitly set to 0. The remaining identifiers are given the values 1 through 5 by default.
    So both Saturday and Sunday will have teh value as 0??
    Please help me and if possible why we go for enumeration in C.
  • Cucumber
    New Member
    • Sep 2007
    • 90

    #2
    Originally posted by bindiya182003
    I am a bit confused here.

    enum DAY /* Defines an enumeration type */
    {
    saturday, /* Names day and declares a */
    sunday = 0, /* variable named workday with */
    monday, /* that type */
    tuesday,
    wednesday, /* wednesday is associated with 3 */
    thursday,
    friday
    } workday;

    The value 0 is associated with saturday by default. The identifier sunday is explicitly set to 0. The remaining identifiers are given the values 1 through 5 by default.
    So both Saturday and Sunday will have teh value as 0??
    Please help me and if possible why we go for enumeration in C.
    The best way to answer that kind of questions is creating a program and print the values of Saturday and Sunday.

    Enumerations are a great way to declare related constant values. Rather than declaring 7 constant integers "const int Sunday=0;", "const int Monday=1;", etc, etc, you use an enumeration that handles the assignation of values automatically.

    Enumerations are heavily used in COM and .NET too.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The rule with values of enums is that a) they are named integer values and b) the count starts at 0 by default and increments for the next member.

      If you assign a value, then it is incremented for the next enum member.

      There is no check that values are duplicated.

      Comment

      Working...