Hello and welcome. In this lecture, I want to walk you through another challenge.
I want to help you guys learn all about enums.
So let's take a look at a
challenge for enums.
So in this challenge, I want you to create a c program that defines an enum type
and uses that type to display the values of some variables.
Remember, enums are awesome because
you can create your own type and you can say this type is only going to have these certain values
that can be assigned to it. We had examples for month,
where month only had values of January through December.
We had an example of a gender, where you could only assign
variables of the type enum gender, male or female,
same type of idea.
But in this code, we want the program to create an enum type named company.
And for this enum type, the only valid values are GOOGLE, FACEBOOK, XEROX,
YAHOO, EBAY, MICROSOFT, all caps.
And also
the order is important because they represent integers, so it should be
the same order as well. Google should be first, Facebook second and so forth.
So we're going to create this enum type
because that's the first thing you do with enums. You create the type and then you use the type.
After we've created the enum type called company,
we then want to actually create the three variables of this type
and we want to assign these three variables values of Xerox, Google and eBay.
And then the last thing this program should do is just display the output.
Simple program, it's just demonstrating enums.
So what we need to do here is we need to use the printout function,
display output. We should display the value of those three variables that we created.
And we should also use a new line so that it's easy to read.
Essentially the output would be if we printed out Xerox, Google and eBay variables
would be something like 2 0 4.
Now you could actually put something before those integers, so they're meaningful, something like
the value of Xerox is 2. The value of google is 0.
The value of eBay is 4.
Now why are those values 2 0 4 because those are integers.
Remember, enums are represented integers in c.
Those are the integers of the order of the declarations
in the list. So Xerox is 2.
Google is zero. Remember that Google was the first one in the list,
and the list starts at 0.
So Google 0, Facebook 1, Xerox 2, Yahoo 3, eBay 4, Microsoft 5.
And that's why it prints out 2 0 4.
Those are the requirements. That's what I want you to try to accomplish.
So some hints, you're going to have to first define that enum type
and its values.
So what you're going to need to do is use an enum keyword. You're going to need to use
the squiggly brackets for the valid values.
You're going to have to declare and initialize three variables.
The way that you declare an enum type is just use the enum followed by the name
and then you can assign values using the equals operator.
And then you need to use printf to display the value of the enum variables.
We're getting pretty good at the printf, so this shouldn't be too bad.
We'll need to use a format specifier.
Since the enums are ints, we can just use percent d.
We'll also want to add this escape sequence to the make the output more readable
backslash n.
And that's it. That's what I want you to try to accomplish. You'll learn a lot from this
little exercise, mainly all about enums. Thank you.