C Reference function difftime()
Usage of difftime():
double difftime ( time_t time2, time_t time1 );
Parameters:
Time1 and time2 are both time_t objects.
Return Value:
The difference in seconds between time2-time1 will be returned as a floating point double.
Explanation:
Calculates the difference in seconds between time1 and time2.
Source code example of difftime():
#include <stdio.h>
#include <time.h>
int main ()
{
time_t time1,time2;
char get_input [256];
double dif_sec;
time (&time1);
printf ("Please enter the name of your pet: ");
gets (get_input);
time (&time2);
dif_sec = difftime (time2,time1);
printf ("It took you %.2lf seconds to type the name.\n", dif_sec );
return 0;
}
Output example of the program above:
Please enter the name of your pet: wodan
It took you 2.00 seconds to type the name.
This entry was posted in C Reference time.h Functions.
You can follow any responses to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.
Tweet This! or use
to share this post with others.
Thanks for the example.
Some C programmer struggling to produce a simple time delay (in seconds).
Here my example:
#include
void delay(int sec); /* Function declaration */
int main()
{
….
delay (3) /* sec = 3 seconds delay */
….
return 0;
}
/* Time delay function definition */
void delay(int s)
{
time_t time1,time2; /* declare timer */
time (&time1); /* initialize both timer */
time (&time2);
while (difftime (time2,time1) <= s) /* Loop until time difference is equal to s seconds */
time (&time2);
}
Regards
Lutz Pansegrau