Enum with Values in C

Level 4197 keystrokes

Defines an enum with explicit integer values for HTTP status codes.

C code

// Each constant gets an explicit value instead of 0, 1, 2
1enum HttpStatus {
2 OK = 200,
3 NOT_FOUND = 404,
4 SERVER_ERROR = 500
5};
6
// Declare enum variable
7enum HttpStatus status = NOT_FOUND;
// %d prints the numeric value: 404
8printf("Status code: %d\n", status);
// Compare using enum constant name
9if (status == NOT_FOUND) {
10 printf("Page not found\n");
11}

Output

Status code: 404
Page not found
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • =× 6
  • _× 4
  • ;× 4
  • "× 4
  • ,× 3
  • (× 3
  • )× 3
  • {× 2
  • }× 2
  • \× 2

More C examples at level 4

← C typing practice