0

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));
    }
}
Marc B
  • 356,200
  • 43
  • 426
  • 500

1 Answers1

0

Your routine time() will not remember rtimeunless you specify rtime as static:

static double rtime;

And: It's unclear why you specify those numbers as doubles.

Arno
  • 4,994
  • 3
  • 39
  • 63
  • Thank You...It helped "double" was for some another experiment i was doing. In my original application i used integer only. – user3340162 Apr 03 '14 at 10:45