How to use enum in C How to use enum in C c c

How to use enum in C


Your struct typedef is basically saying "If I had a "status" field in my record, it could have the value "call" or the value "wait". The warning is basically saying "you never allocated a field".

Possible change:

enum status {CALL, WAIT};typedef struct restaurant{    char name[30];    int groupSize;    enum status my_status;    struct restaurant *nextNode;}list;

Here's more info:


Your enum must either be declared outside the structure:

enum Status {call, wait};typedef struct restaurant{    char name[30];    int groupSize;    struct restaurant *nextNode;} list;

or must declare a member of that type inside the structure:

typedef struct restaurant{    char name[30];    int groupSize;    enum Status {call, wait} status;    struct restaurant *nextNode;} list;

or both:

enum Status {call, wait};typedef struct restaurant{    char name[30];    int groupSize;    enum Status status;    struct restaurant *nextNode;} list;

You could create a typedef for the enum Status too. And since the tags (such as Status in enum Status) are in a different namespace from structure members, you could actually use:

enum status {call, wait} status;

and the compiler won't be confused but you might well be.

Very often, people write enumeration constants in ALL_CAPS. This is partly a hangover from the days of using #define WAIT 0 and #define CALL 1 instead of enum Status { WAIT, CALL };.