954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Slow Clock

The code I wrote for a digital clock always falls behind the clock in my windows 7 taskbar after being run for like 2-3 mins. Why?
Here's the code:

#include <stdio.h>
#include <time.h>
#include <windows.h>

int main() 
{   
    int h,m,s;
    time_t epoch_time;
    struct tm *tm_p;
    epoch_time = time( NULL );
    tm_p = localtime( &epoch_time );

h=tm_p->tm_hour%12;
m=tm_p->tm_min;
s=tm_p->tm_sec;
while(1)
{

if(s>59)
{
m=m+1;
s=0;
}
if(m>59)
{
h=h+1;
m=0;
}
if(h>11)
{
h=0;
m=0;
s=0;
}
Sleep(1000);
s++; // trying something I learned (s=s+1)
system ("cls");
printf(" %d:%d:%d",h,m,s);
}
}
sketchiii
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

I didn't look over your code because its not enclosed within code tags.

However have you considered that your program does not run continuously since its scheduled ran and rescheduled again.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

I didn't look over your code because its not enclosed within code tags.

However have you considered that your program does not run continuously since its scheduled ran and rescheduled again.


:sad:

#include <stdio.h>
#include <time.h>
#include <windows.h>

int main() 
{   int h,m,s;
    time_t epoch_time;
    struct tm *tm_p;
    epoch_time = time( NULL );
    tm_p = localtime( &epoch_time );

h=tm_p->tm_hour%12;
m=tm_p->tm_min;
s=tm_p->tm_sec;
while(1)
{

if(s>59)
{
m=m+1;
s=0;
}
if(m>59)
{
h=h+1;
m=0;
}
if(h>12)
{
h=0;
m=0;
s=0;
}
Sleep(1000);
s++; // trying something I learned
system ("cls");
printf("%d:%d:%d",h,m,s);
}
}


I don't understand isn't that the only way to do this?

sketchiii
Newbie Poster
8 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Clocks aren't as easy as they look. For instance, Sleep(N) gives you AT LEAST N periods of time - but note the "at least" part of that. ;)

Because other programs and lots of Windows services (programs without a terminal window for output), are running, many times you'll get more sleep time than you bargained for.

Also, you have left zero time for your program to actually run - agreed it won't need much, but surely it will need some time.

So, what to do? One way is to subtract the amount of time you get back from the system, from either a minute, or the amount that you asked for. Then adjust how much time you ask for on the next loop, to account for it (basically, subtract it).

You ask for 60 seconds, and you got 60.5 seconds. So you ask for 59.5 seconds, on your next loop. You asked for 59.5 seconds, but you got 59.7 seconds. You ask for 59.8 seconds on your next loop (because it was .2 seconds behind what you asked for last time). And continue with that type of "carry over" logic.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: