The time.h header defines four variable types, two macro and various functions for manipulating date and time. A brief description of the variable types defined in the header time.h is as :
- size_t : This is the unsigned integral type and is the result of the sizeof keyword.
- clock_t : This is a type suitable for storing the processor time.
- time_t is : This is a type suitable for storing the calendar time.
- struct tm : This is a structure used to hold the time and date.
Below is the implementation of digital clock. On executing the program, the output window will show the time when the program was executed.
C
#include <stdio.h>
#include <time.h>
int main()
{
time_t s, val = 1;
struct tm * current_time;
s = time (NULL);
current_time = localtime (&s);
printf ( "%02d:%02d:%02d" ,
current_time->tm_hour,
current_time->tm_min,
current_time->tm_sec);
return 0;
}
|
Output :

Below is the C program to show the current time inside a rectangular bar. The output window shows the day, month, date, current time and year.
C
#include <graphics.h>
#include <time.h>
int main()
{
int dt = DETECT, gmode, midx, midy;
long current_time;
char strftime [30];
initgraph(&dt, &gmode, "" );
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(WHITE);
rectangle(midx - 200, midy - 40, midx + 200,
midy + 40);
floodfill(midx, midy, WHITE);
while (!kbhit()) {
current_time = time (NULL);
strcpy ( strftime , ctime (¤t_time));
setcolor(RED);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 3);
moveto(midx, midy);
outtext( strftime );
}
getch();
closegraph();
}
|
Output :

References :
cprogramforbeginners