Uploaded September 2026 | Updated November 2026, 1 day ago
Learn how to take User Input for Enum Variables in C programming using scanf(), Type Casting, Input Validation, and a Switch Statement.
In this tutorial, we build a simple game difficulty example with three levels: EASY, MEDIUM, and HARD. We define an enum Difficulty and assign EASY the value 1, followed by MEDIUM and HARD.
We also specify int as the Fixed Underlying Type for the Enum. You will understand the difference between the Enum Type of a variable and the Underlying Type used to represent the Enumeration.
Next, we display a menu and ask the user to enter 1 for Easy, 2 for Medium, or 3 for Hard. Since scanf() with %d expects the address of an int variable, we first read the User Input into a separate integer variable.
We then check the return value of scanf() to verify whether an integer value was successfully read. After that, we validate the entered value to make sure it is within the valid range from 1 to 3.
Once the input is validated, we use Type Casting to convert the integer value to enum Difficulty and store it in the Enum Variable.
Finally, we use a Switch Statement with the Enum Enumerators EASY, MEDIUM, and HARD to process the selected difficulty and display the corresponding message.
Our Website
LearningLad.com
Social Media
Facebook facebook.com/LearningLad
Twitter twitter.com/LearningLadEdu
Instagram instagram.com/LearningLadOfficial
Learn how to take User Input for Enum Variables in C programming using scanf(), Type Casting, Input Validation, and a Switch Statement.
In this tutorial, we build a simple game difficulty example with three levels: EASY, MEDIUM, and HARD. We define an enum Difficulty and assign EASY the value 1, followed by MEDIUM and HARD.
We also specify int as the Fixed Underlying Type for the Enum. You will understand the difference between the Enum Type of a variable and the Underlying Type used to represent the Enumeration.
Next, we display a menu and ask the user to enter 1 for Easy, 2 for Medium, or 3 for Hard. Since scanf() with %d expects the address of an int variable, we first read the User Input into a separate integer variable.
We then check the return value of scanf() to verify whether an integer value was successfully read. After that, we validate the entered value to make sure it is within the valid range from 1 to 3.
Once the input is validated, we use Type Casting to convert the integer value to enum Difficulty and store it in the Enum Variable.
Finally, we use a Switch Statement with the Enum Enumerators EASY, MEDIUM, and HARD to process the selected difficulty and display the corresponding message.
Our Website
LearningLad.com
Social Media
Facebook facebook.com/LearningLad
Twitter twitter.com/LearningLadEdu
Instagram instagram.com/LearningLadOfficial










