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.
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.
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.
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.
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.
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;
}
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;
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With