simple calendar app in c – dead useful, innit
intro
Oi, you lot! New to C and fancy summat a bit handy? We’re gonna sling together a lil program to make a simple calendar app—ya know, chuck in a month and year and it spits out the days like a proper wall chart. It’s dead basic but proper cracking—like when I was tryna figure out what day the pub quiz was on after a sesh (got it wrong, too smashed). Great for getting yer head round loops and logic in C without a right faff. Let’s have a butcher’s!
code bit
Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total shambles on yer blog. take a gander:
int isLeapYear(int year) { // check leap year
return (year%4==0&&year%100!=0)||(year%400==0); // fancy rule
}
int daysInMonth(int month, int year) { // days per month
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(month==2&&isLeapYear(year)) {
return 29; // leap year feb
}
return days[month-1]; // usual days
}
int firstDay(int month, int year) { // find first day
int y=year-(month<3); // tweak for jan/feb
return (y+y/4-y/100+y/400+(month*13+8)/5+1)%7; // zeller’s mess
}
void printCalendar(int month, int year) {
char* months[]={"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"};
int days=daysInMonth(month,year);
int start=firstDay(month,year); // sunday=0
printf("\n%s %d\n",months[month-1],year);
printf("Su Mo Tu We Th Fr Sa\n");
int i;
for(i=0;i
}
for(int day=1;day<=days;day++) {
printf("%2d ",day); // days with padding
if((i+1)%7==0||day==days) {
printf("\n"); // new line at week end or last day
}
i++;
}
}
int main() { // nearly did void, ha!
int month,year; // buckets for date
printf("gimme month (1-12): ");
scanf("%d",&month);
printf("gimme year: ");
scanf("%d",&year);
if(month<1||month>12||year<1) { // no daft stuff
printf("oi, gimme proper month (1-12) and year (1+)!\n");
}
else {
printCalendar(month,year); // chuck it out
}
return 0; // forgot ; once, ugh!
}
what ya get
Here’s what it chucks out when ya run it. I tested it with a couple dates—worked bang on, mate!
gimme year: 2024
Feb 2024
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29
gimme month (1-12): 3
gimme year: 2023
Mar 2023
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
how it works, kinda
Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!
1. that `#include
This is me nicking my toolkit. `
2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!
3. boxes: `int month,year;`
These are my lil buckets—`month` and `year` hold what ya chuck in. `int` coz we’re keeping it whole, no faffy decimals!
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme month (1-12): ")` is me barking, “Oi, month, sharpish!” Then `scanf("%d",&month)` grabs it. Same for `year`. `%d` for integers, `&` like a post-it saying “bung it here, ya twit!”
5. `isLeapYear` funtion
`isLeapYear(int year)` checks if it’s a leap year—`return (year%4==0&&year%100!=0)||(year%400==0)` is the fancy rule: divisible by 4 but not 100, or by 400. Gives Feb an extra day if true!
6. `daysInMonth` funtion
`daysInMonth(int month, int year)` sorts the days—`days[]` holds 31, 28, etc. If `month==2` and leap year, `return 29`. Else, `return days[month-1]`—like picking days off a pub wall chart!
7. `firstDay` funtion
`firstDay(int month, int year)` uses Zeller’s mess to find the first day—tweaks `year` for Jan/Feb, then `(y+y/4-y/100+y/400+(month*13+8)/5+1)%7` spits out 0 (Sunday) to 6 (Saturday). Proper head-scratcher, but works!
8. `printCalendar` funtion
Here’s the guts! `printCalendar(int month, int year)` does it—`months[]` names em, `days=daysInMonth(month,year)` gets the count, `start=firstDay(month,year)` sets the first day. Prints the header, spaces out `start` with blanks, then loops `day=1` to `days`, chucking `%2d ` for padding. New line at week ends or last day—like a proper calendar!
9. chucking it out: `printCalendar` and check
If `month<1||month>12||year<1`, yells “oi, proper stuff!” Else, `printCalendar(month,year)` shows it. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!
10. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.
And that’s yer lot! A cracking lil program to make a calendar. Faff with it—chuck in daft dates, tweak it to say “pub days” if ya fancy. First time I ran this, I did Feb 2024 and got 29 days—bang on, eh! You’re a star already, mate—keep smashing it! 😊