Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to intilize a timer in ansi C

Status
Not open for further replies.

maltobji

Programmer
Feb 13, 2001
31
JO
dear all,
is there a way to start a timer using Ansi C .. or do i have to make an emultion for the timer .. and how....


thanx in advance
 
dear rbobbitt,
to be more specific....i want to make a doc program.. using VC 1.52..(it is a must to use this compiler...:)..)
the program should show the a Clock showing the current time .... it should be updated every minute....
(i.e 12:09)...how one can do it unsing this C enviroment

thanx alot for ur concern...
 
Now I don't know what you mean by a "Doc" program :)

At any rate, you can write the "timer" part and the part that gets the time in ANSI C:

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

void displayTime(time_t time)
{
printf(&quot;%s&quot;,ctime(&time));
fflush(stdout);
}
int main(void)
{
time_t now;
time_t last;

last=time(NULL);
for (;;) {
now=time(NULL);
if (difftime(now,last)>0) {
displayTime(now);
last=now;
}
}
return 0;

}

This just prints the time to stdout every second and polls the time on every iteration. If you need to display the time somewhere else (like in a status bar in a GUI program), how you do it depends on your compiler as ANSI C doesn't cover GUI interfaces.

Hopefully this will help you some. I've never used your compiler, but your compiler should give you access to the Win32 API and all of its monstrosity if you need to create GUI interfaces.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top