assignment-s-24.c ​
Metadata ​
- Author — Amit Dutta (amitdutta4255@gmail.com)
- Last updated — 18 Jan 2026
- License — MIT
Problem Statement ​
Problem Statement
Write a program to calculate the difference between two time periods using structures.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <stdlib.h>
struct time_dif
{
int hour;
int min;
int sec;
};
int main()
{
struct time_dif tpA, tpB;
long int tfA, tfB, tdif;
printf("Enter the start time in this 24 hours clock format (HH MM SS): ");
scanf("%d %d %d", &tpA.hour, &tpA.min, &tpA.sec);
printf("Enter the end time in this 24 hours clock format (HH MM SS): ");
scanf("%d %d %d", &tpB.hour, &tpB.min, &tpB.sec);
tfA = tpA.hour * 3600 + tpA.min * 60 + tpA.sec;
tfB = tpB.hour * 3600 + tpB.min * 60 + tpB.sec;
tdif = tfB - tfA;
if (tdif < 0)
{
printf("\nWrong information, End time should be later than Start time.");
return 1;
}
printf("\nTime difference: %ld Hour(s) %ld Minutes(s) %ld Second(s).", tdif / 3600, (tdif % 3600) / 60, (tdif % 3600) % 60);
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31