I've been stuck for hours trying to solve the following exercise:
"Write a function called monthName that takes as its argument a value of type enum
month (as defined in this chapter) and returns a pointer to a character string containing
the name of the month. In this way, you can display the value of an enum month variable
with a statement such as:
printf (“%s\n”, monthName (aMonth));"
I think I solved it, but the solution and the code doesn't feel right.
Is there a simpler or better way to solve this exercise?
Also, could you point out the errors in the code, if any.
Code:
#include <stdio.h>
enum month { january = 1, february, march, april, may, june,
july, august, september, october, november, december };
int main (void)
{
enum month aMonth;
char *monthName (enum month temp);
printf ("%s\n", monthName (1));
return 0;
}
char *monthName (enum month temp)
{
switch (temp) {
case january:
return "January";
case february:
return "February";
case march:
return "March";
case april:
return "April";
case may:
return "May";
case june:
return "June";
case july:
return "July";
case august:
return "August";
case september:
return "September";
case october:
return "October";
case november:
return "November";
case december:
return "December";
default:
return "Please choose the correct value.";
}
}