As the title says, I want my code to list the actual month name instead of listing only the month number. My code:
#include <stdio.h>
int main() {
int day, month, year;
printf("Enter day, month and year : ");
scanf("%d %d %d", &day, &month, &year);
if (month < 1 || month > 12) {
printf("Wrong input data!");
}
else if (year < 1 || year > 4000) {
printf("Wrong input data!");
}
else if (day < 1 || day > 31) {
printf("Wrong input data!");
}
else {
printf("The date entered is %d. %d %d. \n", day, month, year);
}
return 0;
}
Oh and I have literally just started learning C programming so I only know how to use switch case and else/else if.
Edit: Thanks to everyone that suggested using arrays and writing the code, but as I said I still dont know how to use them. Welp everyone is still giving the same response, so this question is useless. Thanks anyways guys.
CodePudding user response:
A cleaner way than switch/if/else, create a lookup table to store the month names:
static char * monthNames[12] = {"January","February",..etc..};
change the printf to use the month value as an index into the string array. Note that arrays are indexed starting at 0, so you have to decrement the month by one to get the correct index.
printf("The date entered is %d. %s %d. \n", day, monthNames[month-1], year);
CodePudding user response:
Use an array in this case array of pointers.
printf("The date entered is %d. (%d)%s %d. \n", day, month, (char *[]){"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}[month], year);
CodePudding user response:
As an alternative to the array approach, you can write a function that returns a pointer to the name of the correct month, using a switch statement/lookup table like you mentioned in your comment:
const char* getMonthName(int month)
{
switch(month)
{
case 1: return "January";
case 2: return "February";
// ...
case 12: return "December";
default: return "Unknown";
}
}
Then you can call it in main:
printf("The date entered is %d. %s %d. \n", day, getMonthName(month), year);
A couple of notes about this approach:
Each month name string defined in
getMonthNameis a string literal. These are most often placed in read only memory of the executable, and you invoke undefined behavior if you try to modify them. Since I've madegetMonthNamereturn aconst char*, You should at least get some support from the compiler in the form of a warning or error if you try to do something you shouldn't. In general, be careful returning strings (more generally, arrays) from functions. You cannot return local arrays from functions, as they go out of scope when the function returns, and the pointer that is returned now points to invalid data. Since these string literals are not local togetMonthName, you're safe to return a pointer to them.Some coding standards require one
returnstatement per function. I have yet to hear a good reason for that, but if that's your case, you can easily modify this to set a pointer to the appropriate string, then return that pointer.
