Enumeration (enum) in C
What is enumeration (enum) in C ? How to use Enum in C ? Let's find out with TipsMake!
What is Enum in C?
Enum is a special type, representing a group of constants (values that cannot be changed).
To create an enum in C , use the enum keyword, followed by the name of the enum, and separate the enum items with a comma:
enum Level { LOW, MEDIUM, HIGH };
Note that the last item does not need a comma. You don't have to capitalize, but it's generally recommended.
Enum is an abbreviation for enumeration. It means 'to enumerate specifically'.
To access the enum, you must create a variable for it. Inside the method main()
, specify the enum keyword, followed by the name of the emum ( Level
), then the name of the enum variable ( myVar
) in this example):
enum Level myVar;
Now that you have created an enum variable ( myVar
), you can assign it a value.
The attached value must be one of the items inside emum ( LOW
, MEDIUM
or HIGH
):
enum Level myVar = MEDIUM;
By default, the first item ( LOW
) has value 0, the second item ( MEDIUM
) has value 1
.
Now if you try printing myVar
, it will produce the result 1
, representing MEDIUM
:
#include enum Level { LOW, MEDIUM, HIGH }; int main() { // Tạo một biến enum và gắn cho nó một giá trị enum Level myVar = MEDIUM; // Print biến enum printf("%d", myVar); return 0; }
Change the values
As you know, the first item of the enum has value 0. The second item has value 1…
To make the values more meaningful, you can easily change them:
#include enum Level { LOW = 25, MEDIUM = 50, HIGH = 75 }; int main() { enum Level myVar = MEDIUM; printf("%d", myVar); return 0; }
Note that if you assign a value to an item, the next item will update the corresponding number:
enum Level { LOW = 5, MEDIUM, // Now 6 HIGH // Now 7 };
Enum in a Switch command
Enum is often used in Switch commands to check corresponding values:
enum Level { LOW = 1, MEDIUM, HIGH }; int main() { enum Level myVar = MEDIUM; switch (myVar) { case 1: printf("Low Level"); break; case 2: printf("Medium level"); break; case 3: printf("High level"); break; } return 0; }
Why and when to use Enum in C?
- Enum in C is used to name constants. That makes the code easier to read and maintain.
- Use enum in C when you have immutable values like date, color, tag set.
You should read it
- Attribute in C #
- Data type in C / C ++
- How will play Super Mario at 380,000 frames per second?
- 25 unexpected facts about orange sure you don't know
- What to do when Skype video doesn't work?
- Learn about Krita - Free alternative to GIMP
- How to view battery life on iOS 12
- Toshiba introduces a Core i3 touchscreen laptop
May be interested
- AZ word about eggs in Pokemon Gothere are many different pokemon eggs that can hatch into many pokemon, but to make these eggs hatch into pokemon, you will have to perform some tasks. each player begins with an egg incubator, which allows the user to incubate an egg after performing certain steps, gps and pedometer tracking on the mobile device of you and the game.
- Things you didn't know about 26 types of Pokéball - Part 1pokéball is the most important item in pokémon go, allowing gamers to quickly catch pokémon back to their team. the higher the pokéball, the more effective the ability to catch pokémon.
- 10 tips to help you become a great Pokemon trainerin the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
- How to use GoChat application in Pokémon GOpokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
- How to play Pokemon GO in Landscape Mode on the iPhonealthough players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
- The secret to controlling Pokemon Go employees at workthese days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
- Check out the 'buffalo' Pokémon in Pokémon Goeach type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
- Sitting home can also locate Pokemon around, do you believe it?the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
- 5 undeniable benefits when playing Pokemon Goget to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
- Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.