2 years ago

#22976

test-img

Φίλ λιπ

time.h and printf(). My code does not print the correct time

The code below actually does wait for the specified amount of time given by the variable float sec, but the terminal print behavior is incorrect. It prints: start: 0.000000 s, end: 0.000000 s, ticker: -1.#QNAN0 s ...Done waiting for: 0 s. You can test that it works against your phone's stopwatch like I have. What is wrong with my code that is making the print behavior incorrect?

Also, if there is a better implementation of what I am trying to do using standard libraries, that would be appreciated.

Thank you for your time.

/* time example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */
#include <stdlib.h> 

void main ()
{
time_t timer;


float sec = 20;
float stopwatch;

timer = time(NULL);

do {
    time_t now;
    now = time(NULL);
    stopwatch = difftime(now,timer);
    system("clear");
    printf("start: %f s, end: %f s, ticker: %f s ...",0,sec,stopwatch);
}
while( stopwatch < sec
 );

printf("Done waiting for: %d s",stopwatch);

}

c

terminal

io

do-while

time.h

0 Answers

Your Answer

Accepted video resources