Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enum in C

Basically we have to implement a queue (linked list) for a restaurant waiting queue.

We get extra points for using enum but I've never used it before. I'm wondering does this look right how I am using it? I have looked it up but haven't seen any examples using linked lists.

Here is the instructions for our structure:

When writing your code, you MUST create a C struct for the nodes in the linked list of the wait list. These data items must include the following (and may include others if needed).

  • the name of the group

  • the integer variable specifying the size of the group (number of people in the group)

  • the in-restaurant status (extra points for using an enum!)

  • a pointer to the next node in the list

The restaurant status is walk-in or call-in (call ahead of time to put name on waiting list)

Here's the structure of mine:

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

I'm asking because I get this warning when I compile:

lab6.c:11:28: warning: declaration does not declare anything [enabled by default]
like image 988
ShadyBears Avatar asked Feb 26 '13 01:02

ShadyBears


People also ask

What is the use of enum in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

How do you use enums?

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: “permanent”, “temp”, “apprentice”), or flags (“execute now”, “defer execution”).

How is enum stored in C?

Enumeration "values" aren't stored at all, as they are compile-time named constants. The compiler simply exchanges the use of an enumeration symbol, with the value of it. Also, the type of an enumeration value is of type int , so the exact size can differ.

What is enum explain example?

An enumeration is used in any programming language to define a constant set of values. For example, the days of the week can be defined as an enumeration and used anywhere in the program. In C#, the enumeration is defined with the help of the keyword 'enum'.


1 Answers

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:

  • How to define an enumerated type (enum) in C?
like image 66
paulsm4 Avatar answered Sep 21 '22 15:09

paulsm4