Hello, and welcome. In this video, I'd like to demonstrate
the solution to our exercise for using enum types.
So I'm going to go ahead. You remember this solution.
It basically, you create an enum named company,
you define some values for it and you print them out. So I'm going to run code blocks.
I'm going to create a new project. I'm going to name this project enum example
because that's a good one for it. It's going to go up to file say new,
new project.
We should be getting pretty good at this by now selecting console application c.
And we're going to name this enum example
and it's going to put everything in our usual spot.
I'm going to hit next and finish.
And this will create our main.c file, which is good. We'll get it started,
just delete some of the data.
Again, we can add comments at the top for the author and the source.
We should be doing that for good practice.
For demonstration purposes and just efficiency, I'm going to leave that
blank. But you should be adding comments throughout your source code.
So the requirements for this challenge
were to basically create an enum
that was named company. And then
the company she can only have valid values of Google, Facebook, Xerox,
Yahoo eBay and Microsoft
and then create three variables and assign them some values
and then print those values out.
So if we want to create an enum type, we have to use the enum keyword.
So we say enum.
And then we want to specify the name of the enum. In this case, we're going to name it company.
And to specify valid values of this enum type,
we have to use the squiggly bracket, which is right above the semicolon on the keyboard
and then we can just type in our names.
Here, I'm going to say Google, I'm going to put everything in all caps just because
it's easier to see. Facebook is going to be our second one.
Xerox, our third.
Yahoo, our fourth.
eBay and then Microsoft.
And that's it. This is how you can create a new enum type named company.
And these are the only values that can be applied for this enum.
So any variables that you create of this type cannot contain other values.
And so the first part of enums is defining them, defining what they are
and then using them.
In line 6, we're defining it. We're saying company can only
have values of Google, Facebook, Xerox, Yahoo, eBay and Microsoft.
The next part of the assignment was to create some variables.
Three variables that had values of Xerox, Google and eBay in them.
To create an enum variable of type company, you just have to say enum.
The name of the name of the type which is company and then the name of the variable.
Here we can just say let's name it Xerox.
And we're going to initialize that declaration
on the same line, so we're going to use the equals operator,
and we're going to say that's equal to Xerox.
So remember, the name company, it cannot be assigned any value.
If I were to just do this and build it, it would compile good because we're able to
actually
assign that variable. But if I tried signing something else, like my name,
you would get a compiler error
because that's not valid.
My build message here is saying incompatible types when initializing type
enum company with a character.
So there's an error and I won't be able to run that.
So the compiler actually protects you from what you can assign the enum.
So again, we're just going to say Xerox.
We don't have to put that in double quotes.
We don't have to do anything because it's actual value.
We want to create a couple more. One for Google and one for eBay.
So here I'm going to say company Google.
I'm going to assign that all caps GOOGLE.
And then the third one I'm going to create is called ebay.
All lowercase is the name of the variable.
The value of that variable is going to be enum value eBay.
And what you should be doing when you're writing this code
in all the actual examples that I've been providing. You should write small chunks.
Write a couple lines compile test.
Do not write a lot of code and run it. And some of the examples that I go through here
demonstrations, I'm compiling at the end for efficiency sake.
But as a new developer and a new programmer,
you should be compiling after a few lines of code and testing.
So after I created my enum type on line 6, I should compile and test that.
Not much you can test, but you can at least compile it.
Lines 8, 9 and 10, I should compile and run so forth.
Now after we created the enums, we can then actually
print out their values. We know how to print out values by using the printf function
anything inside the double quotes for the printf will be displayed as output.
So I'm just going to say the value of Xerox is,
put a colon in there, I need to use a format specifier.
Remember that enums are actual integers under the hood.
So we can do a percent d here.
And then we do a comma for our mapping.
So after the comma would go the variable name.
Here we'd have to actually provide the variable name, which is Xerox.
And then we want to do the same for the other ones.
Now the thing that i forgot here in line 12 is we want to make sure the format's easily read,
so I want to add a new line. So I'm going to say backslash n.
This is an escape sequence, special character interpreted by the printf.
So we're also going to do a printf, and we're going to say the value of Google is.
We're going to say percent d
backslash n
and here we're going to print out the value of google variable,
which is all lowercase google.
And then lastly, we're going to print out the value of eBay.
And this is also going to be an integer percent d with the line feed.
Even though I don't need it because there's nothing after this. It's good practice, we use the comma
for the argument and then we say eBay variable.
And we end all those statements with a semicolon. And that's it.
So now we should compile and run.
And the expected output here is going to be integers
represented to the order of these values in the list.
So as you notice that the list is defined on line 6.
The list is Google, Facebook, Xerox, Yahoo, eBay and Microsoft.
In the in c compiler, those are represented as integers.
So Google is 0. Facebook is 1. Xerox is 2.
So what I should see as output here is I should see his output 2
0 and then eBay which is 4.
So let's see if that matches. Let's go ahead and build.
I have no warnings, no errors. Let's go ahead and run it.
And what I see is the value of Xerox is 2.
The value of Google is 0. And the value of eBay is 4, so it looks correct,
pretty neat.
Now remember from some of the lectures, you can actually change the value of the integers.
When you define the enum, you send an assignment.
So if I actually wanted to change the order,
I could say Yahoo equals 10
and then that would mean eBay would be 11 and Microsoft would be 12.
So let's go ahead and save that. Now the output should be
Xerox is still going to be equal to 2.
Google's going to be 0, but eBay is going to be 11.
So we run it again, and we should see that 2 0 11.
So you can change those integer values if you wanted to for some reason.
Otherwise, it's going to be the order that they were defined in the list.
And again, we can play around with this.
We could actually create other variables to see
if Xerox is equal to Google and so forth.
Enums are really cool, and you'll probably end up using them quite a bit.
They can restrict what is valid for a variable,
which is nice because it provides some kind of validation.
Really easy to use,
and it's also very easy to read. It makes your code more readable.
Now we have a Xerox variable, and the actual value is Xerox.
So we can actually provide other conditional statements and things and it makes the code easier to read.
So this was a quick little exercise that helps you understand enums.
Basically, how to define an enum type, and then how to use that type.
Also we covered printf again. We covered print format specifiers.
We covered escape sequences and
pretty good example.
I'll provide this example as a zip file, so you can download it. Thank you.