I was trying to create a timer using gettimeofday. A function "time" has been called for resetting and getting the current time for the timer. But the program is giving garbage values when i tried to get second tine value. Please help
#include<stdio.h>
#include <sys/time.h>
#include <stdlib.h>
struct timeval cur;
double time(int a);
main() {
time(0);
printf("%lf\n",time(1));
sleep(3);
printf("%lf\n",time(1));
}
double time(int a) {
double rtime;
if(a==0) {
gettimeofday(&cur,NULL);
rtime=(double)(cur.tv_sec);
printf("%lf\n",rtime);
} else if(a==1) {
gettimeofday(&cur,NULL);
return((double)(cur.tv_sec-rtime));
}
}