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.nextdawn.nl 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.