codescracker


c

C Program to Print Date



« Previous Program Next Program »


Print Date in C

To print date in C programming, use getdate() function.

C Programming Code to Print Date

Following C program will print the current system date on the screen (like 09/04/2010). The following C program is for TurboC++ user, the compiler TurboC++ supports dos.h header file :

/* C Program Example - C Program to Print Date 
*  This C program will print the current date */

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
	clrscr();
	struct date d;
	getdate(&d);
	printf("Current system date is %d/%d/%d",d.da_day, d.da_mon, d.da_year);
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program to print date

C Program To Print Date in Words

To print date in words in C programming, use the same procedure as above, but you have to print month name manually. To do, first initialize month number to a variable say m, now put this in switch statement, and start checking for the matched month name to print the current month in words as shown below.

C Programming Code to Print Date in Words

Following C program will print the current date in words like 19 Aug, 2011 :

/* C Program Example - C Program to Print Date in Words
*  This C program will print the current date in words */

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
	clrscr();
	struct date d;
	getdate(&d);
	int m;
	m = d.da_mon;
	printf("Today is %d ", d.da_day);
	switch(m)
	{
		case 1: printf("Jan, %d", d.da_year);
			break;
		case 2: printf("Feb, %d", d.da_year);
			break;
		case 3: printf("March, %d", d.da_year);
			break;
		case 4: printf("April, %d", d.da_year);
			break;
		case 5: printf("May, %d", d.da_year);
			break;
		case 6: printf("June, %d", d.da_year);
			break;
		case 7: printf("July, %d", d.da_year);
			break;
		case 8: printf("Aug, %d", d.da_year);
			break;
		case 9: printf("Sep, %d", d.da_year);
			break;
		case 10: printf("Oct, %d", d.da_year);
			break;
		case 11: printf("Nov, %d", d.da_year);
			break;
		case 12: printf("Dec, %d", d.da_year);
			break;
	}
	getch();
}

When the above C program is compile and executed, it will produce the following result:

c program to print date in words

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test