Skip to content

Commit 44728be

Browse files
committed
time struct
1 parent 7c34192 commit 44728be

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

prog.9.5.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Program to update the time by one second
2+
3+
#include <stdio.h>
4+
5+
struct time
6+
{
7+
int hour;
8+
int minutes;
9+
int seconds;
10+
};
11+
12+
int main (void)
13+
{
14+
struct time timeUpdate (struct time now);
15+
struct time currentTime, nextTime;
16+
17+
printf ("Enter the time (hh:mm:ss): ");
18+
scanf ("%i:%i:%i", &currentTime.hour,
19+
&currentTime.minutes, &currentTime.seconds);
20+
21+
nextTime = timeUpdate (currentTime);
22+
23+
printf ("Updated time is %.2i:%.2i:%.2i\n", nextTime.hour,
24+
nextTime.minutes, nextTime.seconds);
25+
26+
return 0;
27+
}
28+
29+
// Function to update the time by one second
30+
31+
struct time timeUpdate (struct time now)
32+
{
33+
++now.seconds;
34+
35+
if ( now.seconds == 60 ) { // next minute
36+
now.seconds = 0;
37+
++now.minutes;
38+
39+
if ( now.minutes == 60 ) { // next hour
40+
now.minutes = 0;
41+
++now.hour;
42+
43+
if ( now.hour == 24 ) // midnight
44+
now.hour = 0;
45+
}
46+
}
47+
48+
return now;
49+
}

0 commit comments

Comments
 (0)