How to make a Calendar in C
In the C tutorial “How to use Time and Date in C” some people asked questions in the comment section about determining dates and day of the week. That’s why we created this tutorial to show you what things you have to lookout for, such as leap years.
Gregorian Calendar and Leap Years
The Gregorian calendar is the internationally accepted calendar. In the Gregorian calendar there are leap years. If you take a period of four hundred years, there are 303 normal years and 97 leap years. Most people think that every fourth year is a leap year, but strictly speaking this isn’t true.
How to determine which leap years
If a year is divisible by 4, then it is a leap year. But if that year is divisible by 100, then it is not a leap year. However, if the year is also divisible by 400, then it is a leap year. So we can construct the following statement:
if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
{
// It is a leap year and February has 29 days.
}
else
{
// It is not a leap year, so February has 28 days.
}
Complete Calendar Source-code Example
Below you’ll find the complete calendar example. The example asks the user to enter a year, for instance 2010.
// This calendar example is provided by:
// http://www.codingunit.com Programming Tutorials
#include<stdio.h>
#define TRUE 1
#define FALSE 0
int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
" ",
"\n\n\nJanuary",
"\n\n\nFebruary",
"\n\n\nMarch",
"\n\n\nApril",
"\n\n\nMay",
"\n\n\nJune",
"\n\n\nJuly",
"\n\n\nAugust",
"\n\n\nSeptember",
"\n\n\nOctober",
"\n\n\nNovember",
"\n\n\nDecember"
};
int inputyear(void)
{
int year;
printf("Please enter a year (example: 1999) : ");
scanf("%d", &year);
return year;
}
int determinedaycode(int year)
{
int daycode;
int d1, d2, d3;
d1 = (year - 1.)/ 4.0;
d2 = (year - 1.)/ 100.;
d3 = (year - 1.)/ 400.;
daycode = (year + d1 - d2 + d3) %7;
return daycode;
}
int determineleapyear(int year)
{
if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
{
days_in_month[2] = 29;
return TRUE;
}
else
{
days_in_month[2] = 28;
return FALSE;
}
}
void calendar(int year, int daycode)
{
int month, day;
for ( month = 1; month <= 12; month++ )
{
printf("%s", months[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n" );
// Correct the position for the first date
for ( day = 1; day <= 1 + daycode * 5; day++ )
{
printf(" ");
}
// Print all the dates for one month
for ( day = 1; day <= days_in_month[month]; day++ )
{
printf("%2d", day );
// Is day before Sat? Else start next line Sun.
if ( ( day + daycode ) % 7 > 0 )
printf(" " );
else
printf("\n " );
}
// Set position for next month
daycode = ( daycode + days_in_month[month] ) % 7;
}
}
int main(void)
{
int year, daycode, leapyear;
year = inputyear();
daycode = determinedaycode(year);
determineleapyear(year);
calendar(year, daycode);
printf("\n");
}
Download here the source code of this calendar example.
First we make two arrays; one with the number of days for a given month and one with all the month names. Note: in both arrays the first position is empty on purpose, we want to use 1 to 12 to keep things simple.
The first function inputyear() is used to get the user input. We ask the user to input a year. Note: that there is no input checking or error handling is performed to keep things simple.)
The next function determinedaycode() is used to get day number, so we can print the date on the correct position. (So it is only used for output purposes.)
The next function determineleapyear() is used to determine if input of the user is a leap year. If so, the number if days in February is changed to 29.
The last function calendar() is used to print each month onto the screen. The first for loop is used to loop through all months. We then print the month’s name and all the days of the week. We then use the daycode to position the prompt under the right weekday. Then we print all the dates for one month. The last thing we do is to set the position of the prompt on the right weekday.
That’s all for this C programming tutorial. We hope that you now better understand how to determine days-names or date in a year, month or week and that you can use the calendar example to create your own date/days-names functions.
-The- internationally recognized calendar is the -ISO- calendar, which is different from the Gregorian calendar in several ways.
1) it defines year 0 (the year before year 1 in the Gregorian calendar is year -1)
2) it defines the first day of the week to be Monday (the Gregorian calendar does define a week at all)
3) the first week of the year is the week which contains the first Thursday of the year. (the Gregorian calendar doesn’t define this as it doesn’t define what a week is).
The calendar implemented in this program is the standard US calendar, which is not the Gregorian calendar, but a calendar derived from it/extending it.
can yu please specify wat exactly is a day code..
n wat are the calculations been done to determine it.?
can you please explain on how to get the day code?
the formula confusing me…
codingunit is great website.we love members of coding unit.
How do I put the months side by side.
Like this:
Janunary February
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
March April
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
I’m trying to do this since last month but I simply can’t figure it out. Can anyone help me?
On my last post the months (February and April) should been a few spaces foward like the week day
Am trying to develop a program to output the date on requesting for a 30 days leave and when the leave will end. But it is not working out. some one to help me out.
This code:
int determinedaycode(int year)
{
int daycode;
int d1, d2, d3;
d1 = (year – 1.)/ 4.0;
d2 = (year – 1.)/ 100.;
d3 = (year – 1.)/ 400.;
daycode = (year + d1 – d2 + d3) %7;
return daycode;
}
compiled with PellesC generates a wrong daycode. The floats are causing it. If the decimals are removed it works fine and the code works fine as is compiled with MinGW.
This is a very nice program..
this code show only whole cal of 12 months but beside this i want, it will show the specipic day when i input date & month —how can i do it???? plz help
This program is difficult,but understand very easily.So thanks.
it is a big help for my project. tnx
how do i get a specific day in the calendar, example: u ask the users to enter a date and the outputs that specified date, example: 02/11/2011.
sir,i need output of this program