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);  
}  
  • unmagical@lemmy.ml
    link
    fedilink
    arrow-up
    3
    ·
    8 days ago

    I honestly kinda love date/time manipulation stuff while coding because it’s very nature is reliant on exceptions and edge cases. While it can be annoying it is far from boring especially if your day to day is just managing ledgers or APIs. Even with my odd endeavor into date/time shenanigans though it’s almost always better to use a library as the complexity will quickly grow out of scope.

    • printf("%s", name);@piefed.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      8 days ago

      Cool insight! Also, this reminds me, where I work, we have a piece of really old, probably early 2000’s, software with which we book and document interviews with our clients (convicts on parole, infact). When I once accidentally tried to enter a future date as the date of an interview, it gave me an error message saying “you can only book interviews between 1753 and present day”. After looking into it, I found that the Gregorian calendar, in my country, was officially adopted as late as 1753 because of religious opposition. I laughed so hard at this, still wondering if this was left there as an Easter egg by a developer or if it’s using a library that behaves this way.