Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy C enum question

Tags:

c

enums

I have just started C very recently and I have been asked to answer some coding exercises in which the following piece of code appears:

typedef enum {
  false = 0,
  true = 1
} Bool;

Could someone please provide a brief and clear explanation to that?

Thanks very much.

like image 548
nunos Avatar asked Feb 27 '10 17:02

nunos


People also ask

What is enum in C with example?

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.

When should I use enum in C?

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

What is the advantage of enum in C?

The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future. Makes code easier to read, which means it is less likely that errors will creep into it.

What is enum used for?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


3 Answers

Since C does not have a Boolean data type, your code simulates one by making use of typedef.

You can make use of the new user defined data type as follows:

Bool find_key_in_array() {
        Bool found = false; // assume not found.

        // do searching and set found suitably.    

        return found;
}

int main()  {

        Bool result = find_key_in_array();

        return 0;
}
like image 30
codaddict Avatar answered Sep 24 '22 19:09

codaddict


It's really doing two things; you can break it down something like this:

enum _bool {
   false = 0,
   true = 1
};

And:

typedef enum _bool Bool;

This code creates a new enumeration type and then uses typedef to give it a convenient name. It would let you use a new 'type' called Bool elsewhere in your code, and assign it the values false and true. Here's a simple use case:

Bool logical_not(Bool in)
{
    if (in == true)
        return false;
    else
        return true;
}
like image 191
Carl Norum Avatar answered Sep 25 '22 19:09

Carl Norum


It's just a definition of an enum, a type that can assume only a discrete number of values, i.e. the ones enclosed in these brackets. Each of these values is given a name, that you can later use to refer to it. If you only specify the name of the values and not the actual value, the compiler will set them for you in increasing order, starting from zero for the first element.

See the wiki article about enumerated types (and in particular its C section) for more information.

That specific enum defines a boolean type, i.e. a type that can assume only two values: true and false, where false=!true. The boolean values are used very often in programming, for example as flags to indicate if a condition is met, and actually many languages include them as a native type (C++ and C99, for example, do that).

By the way, to define that enum this:

enum Bool
{
    false = 0,
    true = 1
};

would be enough; however, because of how C was designed to declare a variable of the Bool type with this code you would need to put always the enum keyword before Bool:

enum Bool myFlag=true;

Using the typedef trick, instead, you define an anonymous enum made in that way, and then you provide an alias to it named Bool; in that way you can simply do:

Bool myFlag=true;
like image 23
Matteo Italia Avatar answered Sep 23 '22 19:09

Matteo Italia