6
Aesthetics: goto or not (piefed.blahaj.zone)
submitted 3 months ago* (last edited 3 months ago) by akunohana@piefed.blahaj.zone to c/c_lang@programming.dev

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);  
}  
you are viewing a single comment's thread
view the rest of the comments
[-] CameronDev@programming.dev 2 points 3 months ago

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:

char* williswas = "";
if (year == 2026) {
    williswas = "is";
}
...

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.

this post was submitted on 16 May 2026
6 points (80.0% liked)

C Programming Language

1343 readers
3 users here now

Welcome to the C community!

C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.

ยฉ Dennis Ritchie

๐ŸŒ https://en.cppreference.com/w/c

founded 3 years ago
MODERATORS