

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Enums
incomplete
2: Non-Default Values
incomplete
3: Switch Case
incomplete
4: Sizeof Enum
incomplete
This lesson's interactive features are locked, please to keep using them
One of the best features of enums is that it can be used in switch statements. Enums + switch statements:
Here's an example:
switch (logLevel) {
case LOG_DEBUG:
printf("Debug logging enabled\n");
break;
case LOG_INFO:
printf("Info logging enabled\n");
break;
case LOG_WARN:
printf("Warning logging enabled\n");
break;
case LOG_ERROR:
printf("Error logging enabled\n");
break;
default:
printf("Unknown log level: %d\n", logLevel);
break;
}
You'll notice that we have a break after each case. If you do not have a break (or return), the next case will still execute: it "falls through" to the next case. Many devs have written bugs when using switch statements, because they forgot to add break.
In some rare cases, you might want the fallthrough:
switch (errorCode) {
case 1:
case 2:
case 3:
// 1, 2, and 3 are all minor errors
printf("Minor error occurred. Please try again.\n");
break;
case 4:
case 5:
// 4 and 5 are major errors
printf("Major error occurred. Restart required.\n");
break;
default:
printf("Unknown error.\n");
break;
}
But usually, it's a footgun. You'll almost always want a break at the end of each case statement.
Complete the http_to_str function. Given the enum defined in http.h, it should return a hard-coded string (char *) with the human-readable version of the HTTP status code: