tags:

views:

149

answers:

6

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.

+1  A: 

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;
}
codaddict
+1  A: 

It provides readable literals for true and false. But you could guess that.

How does it work? An enumeration (keyword enum) connects a series of tokens with integer values, and the typedef ... Bool; makes Bool the typename of the enumeration.

On the whole I would discourage this idiom because you might later be tempted to use

int flag=false;
// something happens that might change the setting of flag that *doesn't* use the enum
if (flag == true) {
   //...
}

and if flag got set to 2 this will not do what you expect.

If you use c for long the zero is false everything else is true interpretation will beome second nature.

dmckee
Not that I feel this is precise or complete, but care to explain the downvote?
dmckee
+7  A: 

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;
}
Carl Norum
+1  A: 

A enum is an integral type that has a limited number of symbolic values. This allows you do things like:

Bool finish = false;

while (finish != true)
{
    ...
}
R Samuel Klatchko
A: 

From what I remember:

This declares an enumeration and then associates a name, Bool, to this enumeration (through typedef) . You can get more information about C enumerations here

Ando
+2  A: 

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;
Matteo Italia