Using Call by reference I have made a C program to calculate time difference between start time and end time. First we declare three integers in a structure ‘sec, min, hrs’ to store the timings in hours:minutes:seconds format. Then we declare a function Difference to which we are going to call later in the program.
void Difference (struct TIME t1, struct TIME t2, struct TIME *diff);
Then variables ‘t1,t2,diff’ are to be declared to store the values of start time and end time. First we will ask the user to enter the start time and then store it in t1 variables. Then we ask the user the end time and store it in variable t2. When we have got both the times we then call the function Difference.
Difference (t1,t2,&diff);
In difference function we use If…else condition to check which one of the time is greater so as to subtract smaller value from greater one by one. The value of hours is checked to find the greatest amongst it and then subtracted. Similarly rest of the time difference is calculated and the difference is displayed on the command prompt.
Here is the code of Difference function:
void Difference (struct TIME t1, struct TIME t2, struct TIME *differ){
if(t2.hrs>t1.hrs){
differ->hrs=t2.hrs-t1.hrs;}
else differ->hrs=t1.hrs-t2.hrs;
if(t2.min>t1.min){
differ->min=t2.min-t1.min;}
else differ->min=t1.min-t2.min;
if (t2.sec>t1.sec){
differ->sec=t2.sec-t1.sec;}
else differ->sec=t1.sec-t2.sec;}
Output:-
Download the Source Code here:-
Leave a Reply to Anonymous Cancel reply