User Input for Enums in C | scanf, Type Casting & Input Validation @LearningLad
User Input for Enums in C | scanf, Type Casting & Input Validation  @LearningLad
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
User Input for Enums in C | scanf, Type Casting & Input ValidationHow to Install C C++ Compiler / Tool set  ( gcc g++ gdb ) Easily in Windows ComputerSwap Two Numbers Using Pointers in C | Swap Function & Pointer DereferencingHow to Compile and Run a C++ Program from Command Prompt in Windows 10Simple Trick to Open up Command Prompt ( cmd ) in a Folder with current location as Path Windows 10Displaying float double long double values in Scientific Notation in C Programming Language2D Arrays with Loops in C | User Input & Variable Length 2D ArraysHow to Set up Visual Studio Code for Python ProgrammingHow to Add Two Numbers in Python | Simple User Input Program TutorialArray of Strings in C | 2D Character Arrays, Input & Accessing StringsC Calculator Program Using Do While Loop & Switch Statement | Beginner TutorialSize Modifiers in C Programming | Short, Long & Unsigned Integers Explained
LearningLad |

User Input for Enums in C | scanf, Type Casting & Input Validation

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER