The example contains declaring a new enumeration type, declaring an enum type variable, using enums in a switch statement, passing an enum to a function as well as a function returning an enum value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #define STR_MAX 128 #define FMT_LEN 16 #define LIST_BIRDS {"Eagle", "Peacock", "Flamingo", "Cockatoo", "Albatross", \ "Seagul", "Pigeon", "Passer"} #define LIST_PLANES {"Antonov AN-225", "The Wright Flyer", "Airbus A380",\ "Boeing 747-400", "Concorde"} #define SUPERMAN "Clark Kent" enum FlyingObject {OBJ_BIRD, OBJ_PLANE, OBJ_SUPERMAN, OBJ_UFO}; void PromptString(char *prompt, char *str, int max); enum FlyingObject IdentifyObjectType(char *str); void PrintObjectType(enum FlyingObject obj); bool IsTypeOfObject(const char *listOfObjects[], int n, char *object); int main(void) { char sNameOfObject[STR_MAX]; PromptString("What object did you see in the sky?", sNameOfObject, STR_MAX); enum FlyingObject eTypeOfObject = IdentifyObjectType(sNameOfObject); PrintObjectType(eTypeOfObject); return EXIT_SUCCESS; } /** * Description: Prints the prompt and stores reads a string from stdin in str, * maximum length of max. Input longer than max is left in stdin. * * Parameters: prompt - question printed before reading input * str - user answer is stored in here * max - maximum length of user answer. * * Return: none */ void PromptString(char *prompt, char *str, int max) { puts(prompt); // Prepare input format char fmt[FMT_LEN]; sprintf(fmt, "%%%d[^\n]s", max); scanf(fmt, str); } /** * Description: Identifies the type of flying object based on preset lists. * * Parameters: str - name of the flying object to identify * * Return: type of flying object */ enum FlyingObject IdentifyObjectType(char *str) { const char *birds[] = LIST_BIRDS; const char *planes[] = LIST_PLANES; int nBirds = sizeof(birds) / sizeof(char *); if (IsTypeOfObject(birds, nBirds, str)) return OBJ_BIRD; int nPlanes = sizeof(planes) / sizeof(char *); if (IsTypeOfObject(planes, nPlanes, str)) return OBJ_PLANE; if (!strcmp(SUPERMAN, str)) return OBJ_SUPERMAN; return OBJ_UFO; } /** * Description: Checks if a string is part of a list * * Parameters: listOfObjects - list of strings to compare against * n - number of strings in listOfObjects * object - string to check if it is a part of the list * * Return: true if it's part of the list, false if not */ bool IsTypeOfObject(const char *listOfObjects[], int n, char *object) { for (int i = 0; i < n; i++) { if (!strcmp(listOfObjects[i], object)) return true; } return false; } /** * Description: Prints the type of object from the enumerated list * * Parameters: obj - numerated identifier for the object to print * * Return: none */ void PrintObjectType(enum FlyingObject obj) { // Cheap puns ahead.. switch (obj) { case OBJ_BIRD: printf("It's a bird... "); break; case OBJ_PLANE: printf("It's a plane... "); // break; case OBJ_SUPERMAN: printf("It's superman!"); // break; case OBJ_UFO: printf("Unknown flying object or invalid input!"); break; } putchar('\n'); } |