Good day! How are you this gloomy, rainy afternoon?
I just realized - through experimentation, naturally ๐ - that I can return whole printf()s into main. Mind blowing. Anyway, I would much appreciate any input on how to write my leapYear function more aesthetically pleasing. I tried slapping the printf statements at the end of leapYear or at the end of each if statement. Still feels repetitive...
Also, as it stands, I'm having leapYear return int as per the declaration and definition. But I am returning printf() statements. Why does this work? I know that, for instance, char is int under the hood, using the machine's underlying character table, for instance ASCII. Is the explanaition something similar to this?
Thank you in advance! ๐
#include <stdio.h>
#include <string.h>
int leapYear(int year);
int main() {
int year = 0;
printf("Enter the year that you would like to check and press ENTER: ");
scanf("%d", &year);
leapYear(year);
return 0;
}
int leapYear(int year) {
char wasiswill[12] = "";
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
if (year < 2026) strcpy(wasiswill,"was");
if (year == 2026) strcpy(wasiswill, "is");
if (year > 2026) strcpy(wasiswill, "will be");
goto EXIT_LEAP;
}
else {
if (year < 2026) {
strcpy(wasiswill,"was");
goto EXIT_NLEAP1;
}
else if (year == 2026) {
strcpy(wasiswill, "is");
goto EXIT_NLEAP2;
}
else if (year > 2026) {
strcpy(wasiswill, "will not be");
goto EXIT_NLEAP3;
}
}
EXIT_LEAP: return printf("%d %s a leap year.\n", year, wasiswill);
EXIT_NLEAP1: return printf("%d %s not a leap year.\n", year, wasiswill);
EXIT_NLEAP2: return printf("%d %s not a leap year.\n", year, wasiswill);
EXIT_NLEAP3: return printf("%d %s a leap year.\n", year, wasiswill);
}
This is not a good way to use GOTO :(.
For starters: Your GOTO targets aren't unique, NLEAP1 and NLEAP2 are the same, and LEAP and NLEAP3 are the same.
I would probably just put the printfs where the gotos are and save the strcpys. Just make sure to return out after the printfs. Sometimes being verbose in your code is better than being clever with gotos.
Your returns are returning the return value from the printfs.
I.e: same as:
Printfs returns an int with the number of bytes it wrote out.
Thank you very much for your input! ๐ How clumsy of me not to make the GOTO targets unique... Then I'll reverse the code to what I had initially written, namely, exactly what you said.
Could you clarify what you mean by "save the strcpys"?
If you moved the printfs back, you'd end up with 6 printfs, and you can just hardcode in the wasiswill value in the format string.
As it happens, the strcpys are unnecessary anyway, you can just assign "will be" to a char pointer:
The strings are already in your read only memory, you just need a pointer to them.
Making the GOTO targets unique makes a stronger argument for just dropping the gotos entirely, because the execution flow for each of the 6 cases is now getting intertwined. You may understand it today, but in 3 months time you'll hate reading it.