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 the day number of the first day in that year, 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.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 32 responses to “How to make a Calendar in C”

Why not let us know what you think by adding your own comment!

  1. Frederik on July 8th, 2010:

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

  2. tamanna on November 7th, 2010:

    can yu please specify wat exactly is a day code..
    n wat are the calculations been done to determine it.?

  3. nisa on January 27th, 2011:

    can you please explain on how to get the day code?
    the formula confusing me…

  4. yasin warraich,awais,naser,shuja,ahsan on March 25th, 2011:

    codingunit is great website.we love members of coding unit.

  5. Kevin Olson on June 15th, 2011:

    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?

  6. Kevin Olson on June 15th, 2011:

    On my last post the months (February and April) should been a few spaces foward like the week day

  7. Denis on July 8th, 2011:

    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.

  8. Lloyd on July 12th, 2011:

    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.

  9. Amiya on July 30th, 2011:

    This is a very nice program..

  10. mehedi on August 12th, 2011:

    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

  11. Shashi Nitave on September 25th, 2011:

    This program is difficult,but understand very easily.So thanks.

  12. Assasin23 on October 7th, 2011:

    it is a big help for my project. tnx

  13. Lali on November 30th, 2011:

    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.

  14. gowtham on January 27th, 2012:

    sir,i need output of this program

  15. nandini on February 3rd, 2012:

    I want to implement in c# where the year,month and date are entered in individual textboxs by the user and the corresponding day has to be displayed in another textbox. how can i do it???? plz help me…. i have tried but am facing prob in coding it.

  16. josh on February 8th, 2012:

    Argh!! my head’s gonna blow!! can’t understand a thing..I’m noob… T_T

  17. josh on February 8th, 2012:

    I have a program, then you ask the user to input the month, day, year(MM/DD/YYYY), then if the user inputs the number of the month(1-12), the program will output the month in words(Jan to Dec), and if he inputs the day(1-7), the program will output in words(Mon to Sun), and if he inputs the year, the program will output if it is a leap year or not. Can someone help me with the codes? I can’t make mine work.

  18. ville on April 13th, 2012:

    Thanks!
    Very good example :)

  19. sandeep kosta on June 11th, 2012:

    hey admin thanks for making this article so useful……..
    but bro one place i m stucked on one thing…..how did u determine the daycode……its completely beyond me to understand….plz sort my problme out…….expecting ur answer…….thank u so much….

  20. OnceMore on June 11th, 2012:

    @sandeep kosta
    As stated in the text of this tutorial:

    “The next function determinedaycode() is used to get the day number of the first day in that year, so we can print the date on the correct position. (So it is only used for output purposes.)”

    and later on in the text:

    “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.”

    So for example for 2002 this is 2, so the printing is started on Tuesday.
    In the year 2012 this first day is on Sunday, so the output of determinedaycode() will be 0 (zero).

    Hope this helps!

  21. vikas on August 15th, 2012:

    CALENDER PROGRAM:: use the following Simple source code of calender program and reply me in the my email id: vikas.sidhi@gmail.com
    This code is written by self me I am student of MCA(Master of Computer Application) II year.

    #include
    #include
    void main()
    {
    int dd,mm,yy,ndd,dn,yr,rs,i,x,y,z,w,u,td,k,j,tm=0;
    int mnth[12]={3,0,3,2,3,2,3,3,2,3,2,3};
    char c;
    clrscr();
    do
    {
    printf(“\n Enter the any Date(dd/mm/yyyy):”);
    scanf(“%d/%d/%d”,&dd,&mm,&yy);
    if(yy30200)
    printf(“\n :INVALID DATE:”);
    else
    {
    if(mm12)
    printf(“\n :INVALID DATE:”);
    else
    {
    if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
    if(dd31)
    printf(“\n INVALID DATE:”);
    else
    goto agn;
    else if(mm==4||mm==6||mm==9||mm==11)
    if(dd30)
    printf(“\n INVALID DATE:”);
    else
    goto agn;
    else if(mm==2 && (yy%400==0||(yy%100!=0 && yy%4==0)))
    if(dd29)
    printf(“\n INVALID DATE:”);
    else
    goto agn;
    else if(mm==2 && !(yy%400==0 || (yy%100!=0 && yy%4==0)))
    if(dd28)
    printf(“\n INVALID DATE:”);
    else
    goto agn;

    else
    {

    agn:
    yr=yy-1;

    ndd=01;
    if(yy%400==0||(yy%100!=0 && yy%4==0))
    {
    mnth[1]=1;
    }
    else
    mnth[1]=0;
    tm=0;
    for(i=0;i<=mm-2;i++)
    tm=tm+mnth[i];
    x=yr%400;
    y=x/100;
    z=x%100;
    w=z/4;
    u=z%4;

    td=y*5+w*5+u;
    rs=(td+tm+ndd)%7;
    printf("\n");
    printf("\t**This Software Is Developed By: VIKAS TIWARI**\n\n");
    if(mm==1)
    printf("\t\tJanuary\t%d\n",yy);
    if(mm==2)
    printf("\t\tFebuary\t%d\n",yy);
    if(mm==3)
    printf("\t\tMarch\t%d\n",yy);
    if(mm==4)
    printf("\t\tApril\t%d\n",yy);
    if(mm==5)
    printf("\t\tMay\t%d\n",yy);
    if(mm==6)
    printf("\t\tJune\t%d\n",yy);
    if(mm==7)
    printf("\t\tJuly\t%d\n",yy);
    if(mm==8)
    printf("\t\tAugust\t%d\n",yy);
    if(mm==9)
    printf("\t\tSeptember\t%d\n",yy);
    if(mm==10)
    printf("\t\tOctoober\t%d\n",yy);
    if(mm==11)
    printf("\t\tNovember\t%d\n",yy);
    if(mm==12)
    printf("\t\tDecember\t%d\n",yy);
    printf("\n Mon \tTue \tWed \tThu \tFri \tSat \tSun\n");

    if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
    dn=31;
    if(mm==4||mm==6||mm==9||mm==11)
    dn=30;
    if(mm==2 && (yy%400==0||(yy%100!=0 && yy%4==0)))
    dn=29;
    if(mm==2 && !(yy%400==0||(yy%100!=0 && yy%4==0)))
    dn=28;

    if(rs==0)
    {
    printf("\t\t\t\t\t\t");
    j=6;
    }
    if(rs==1)
    {
    j=0;
    }
    if(rs==2)
    {
    printf("\t");
    j=8;
    }
    if(rs==3)
    {
    printf("\t\t");
    j=9;
    }
    if(rs==4)
    {
    printf("\t\t\t");
    j=10;
    }
    if(rs==5)
    {
    printf("\t\t\t\t");
    j=11;
    }
    if(rs==6)
    {
    printf("\t\t\t\t\t");
    j=12;
    }
    for(k=1;k30200)
    yy=30200;
    goto agn;
    }
    if(c==80)
    { clrscr();
    yy=yy-1;

    if(yy<1)
    yy=1;
    goto agn;
    }
    if(c==75)
    { clrscr();
    –mm;
    if(mm<1)
    {
    mm=12;
    yy=yy-1;
    if(yy12)
    {
    mm=1;
    yy++;
    if(yy>30200)
    yy=30200;
    }
    goto agn;

    }

    }
    }
    } /*First else statemen */

    printf(“\n Do You Want to Continue…..(y/n):”);
    fflush(stdin);
    c=getche();

    }
    while(c==’y’ || c==’Y’);

    getch();
    }

  22. sravya on August 20th, 2012:

    is there any extra things that we can add to calender so that it will replace the present day calender

  23. santhu on September 3rd, 2012:

    Thanks for developing these type of programs in languages so usefull for professional students / learners.

  24. sudhir reddy on November 9th, 2012:

    It is very good code. I worked on it. It really help to me on daycode. Thank you very much!

  25. ankit on May 7th, 2013:

    genious baby

  26. Tushar Shukla on June 9th, 2013:

    I want a program for my project that will count the number of days by inputting two distinct dates. Example: 1st date= 15/05/2013; 2nd date= 26/11/2013. the difference of days between these two dates is required.

  27. M Arslan on August 22nd, 2013:

    This code:
    for ( gun = 1; gun <= 1 + d * 5; gun++ )
    {
    printf(" ");
    }

    What does part of the this code task ? I can't understand. Can you help that issue ??

  28. arvind sahu on September 18th, 2013:

    This program is difficult,but understand very easily.So thanks.

  29. sajjad ahmad on November 12th, 2013:

    This site is really good for first time users I mean start users.The way it explains and codes is very basic and easy to understand.Thanks

  30. Sagar on January 26th, 2014:

    How can i start week from any day let say from friday

  31. santhosh on February 14th, 2014:

    hai,
    I,need some reference from u. if we put today as a input(like 14/2/2014) how to get next day as output(15/2/2014) using for loop

  32. Shreya on March 28th, 2014:

    hi,
    Actually i need a help from you.
    I am making a signup page and i want to add the calender to that for the DOB column, but i want to print an error if the age preceeds 18 year from now..
    can any one help that how i can do this?