Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- difference_between_two_time_periods_v1.c
- https://www.programiz.com/c-programming/examples/time-structure
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- struct TIME
- {
- int hours;
- int minutes;
- int seconds;
- };
- // Computes difference between time periods
- void differenceBetweenTimePeriod(struct TIME start,
- struct TIME stop,
- struct TIME *diff )
- {
- while (stop.seconds > start.seconds)
- {
- --start.minutes;
- start.seconds += 60;
- }
- diff->seconds = start.seconds - stop.seconds;
- while (stop.minutes > start.minutes)
- {
- --start.hours;
- start.minutes += 60;
- }
- diff->minutes = start.minutes - stop.minutes;
- diff->hours = start.hours - stop.hours;
- }
- int main(void)
- {
- struct TIME startTime, stopTime, diff;
- /*
- printf("\n Enter the start time. \n");
- printf("Enter hours, minutes and seconds: ");
- scanf("%d %d %d", &startTime.hours,
- &startTime.minutes,
- &startTime.seconds);
- printf("\n Enter the stop time. \n");
- printf("Enter hours, minutes and seconds: ");
- scanf("%d %d %d", &stopTime.hours,
- &stopTime.minutes,
- &stopTime.seconds);
- */
- startTime.hours = 12;
- startTime.minutes = 34;
- startTime.seconds = 55;
- stopTime.hours = 8;
- stopTime.minutes = 12;
- stopTime.seconds = 15;
- // Difference between start and stop time
- differenceBetweenTimePeriod(startTime, stopTime, &diff);
- printf("\nTime Difference: %d:%d:%d - ",
- startTime.hours,
- startTime.minutes,
- startTime.seconds);
- printf("%d:%d:%d ",
- stopTime.hours,
- stopTime.minutes,
- stopTime.seconds);
- printf("= %d:%d:%d \n",
- diff.hours,
- diff.minutes,
- diff.seconds);
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment