1

I've started learning C and have reached the point of enums. An enum is basically a preferred alternative to DEFINE / const int, correct?

What's the difference between these two declarations?

#include <stdio.h>

// method 1
enum days {
    Monday,
    Tuesday
};

int main()
{
    // method 1
    enum days today;
    enum days tomorrow;

    today    = Monday;
    tomorrow = Tuesday;

    if (today < tomorrow)
        printf("yes\n");

    // method 2
    enum {Monday, Tuesday} days;

    days = Tuesday;
    printf("%d\n", days);

    return 0;
}
dave
  • 7,717
  • 19
  • 68
  • 100

3 Answers3

2

An enumeration should be preferred over #define/const int when you want to declare variables that can only take on values out of a limited range of related, mutually exclusive values. So the days of the week is a good example, but this would be a bad example:

enum AboutMe
{
    myAge = 27,
    myNumberOfLegs = 2,
    myHouseNumber = 54
};

Going back to your code example; the first method declares a type called enum days. You can use this type to declare as many variables as you like.

The second method declares a single variable of type enum { ... }. You cannot declare any other variables of that type.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • What is the second method good for then? If `days = Tuesday`, wouldn't it be easier to write `const int Tuesday = 1` ? Unless I'm using the 'enum variable' incorrectly in my sample code#second method... – dave Feb 18 '11 at 08:21
  • @dan: "Anonymous" enumeration types do still have advantages. For instance, they tell the reader of your code that the variable is not just "any old integer"; instead, it can only take on one of a specific range of values. A debugger will typically display the current value of `days` as `Tuesday`, rather than as `1`. – Oliver Charlesworth Feb 18 '11 at 08:26
  • "instead, it can only take on one of a specific range of values" - This makes sense. Also seems quite useful. Thanks for the info – dave Feb 18 '11 at 08:28
1

Being a typenut I would write the first as

typedef enum { Monday, Tuesday } days_t;

and do declaration as

days_t day = Tuesday;

The second method I find no use for.

Erik
  • 2,013
  • 11
  • 17
  • The second example is useful if you only want a *single* variable that can represent days of the week. – Oliver Charlesworth Feb 18 '11 at 08:33
  • Can also be used when you don't want any values, but do want some enumerated constants without creating a type and thereby giving the false impression that you might use it for variables. – Tony Delroy Feb 18 '11 at 10:26
  • @Tony: Can you give an example? Where would you want *enumerated* constants, but without any variables of that type? – Oliver Charlesworth Feb 18 '11 at 17:20
  • @Oli: I'm not saying it's good practice, but people - particularly from a C background - sometimes use e.g. `enum { Repetitions = 10 };` then loop an `int` up to `Repetitions`: poor man's constant, though better than a `#define`. – Tony Delroy Feb 20 '11 at 01:49
1

The difference is that in the first case, days is the name of the enumeration. Therefore you can access the definition by

enum days today;

enum days = makes clear which enumeration, the one which is named days

today = variable name

In the second case, days is a variable of the unnamed enumeration.

enum {Monday, Tuesday} days;

enum {Monday, Tuesday} = unnamed enumeration, therefore needs to be defined with the curl brackets {}

days = variable name

Bort
  • 11
  • 1