-1
 #include <stdio.h>
#include <time.h>   
int main (void)
{
double diff = 0.0;
time_t start;
time_t stop;
time(&start);
print("Enter millisecond to stop"); 

so what is code should add to stop timer after given interval of timer

Satender346
  • 352
  • 1
  • 3
  • 15
  • `time` returns in seconds use `gettimeofday`. u cannot achieve millisecond precision with `time`. man 2 time – Sakthi Kumar Feb 26 '14 at 07:05
  • Your question doesn't make any sense without specifying a given system. If you are programming on a standard desktop OS (Windows, Linux, Mac etc) then there is no such thing as millisecond accuracy, because you need a RTOS for that. And even if the intended target is a RTOS, you can probably not use `time`, because it is intended to give calendar time, rather than high resolution timing. – Lundin Feb 26 '14 at 07:26
  • I'd put `time(&start);` **after** the user entered the interval. And: `time()` returns seconds. So trying to measure milliseconds with it does not make sense. – alk Feb 26 '14 at 08:15
  • You might like to look around on SO, there are dozens of questions like yours. – alk Feb 26 '14 at 08:18
  • Also decide: C or C++? – alk Feb 26 '14 at 08:19

1 Answers1

1
int seconds;
scanf ("%d",&seconds);
stop = 0;
while (stop < start + seconds)
    time(&stop);
Blaz Bratanic
  • 2,279
  • 12
  • 17
  • Your loop is a busy loop. It will consume one of your CPU cores at 100%. Try using a sleep (nanosleep probably) instead to "pause" your process. – jackrabbit Feb 26 '14 at 07:19
  • @jackrabbit Sleep functions cannot be used for accurate millisecond timing purposes, [read this](http://www.flounder.com/badprogram.htm#Sleep). – Lundin Feb 26 '14 at 07:22
  • You're pointing me at a page from 10 years ago on the use of sleeping for thread synchronisation being defective? Threads are nowhere to be seen in the above code. I'm just saying that, for waiting a significant amount of time, you probably don't want to write a busy-loop. – jackrabbit Feb 26 '14 at 07:37