Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to run while loop for N seconds in C

1 Answer

0 votes
#include <stdio.h>
#include <time.h>
#include <unistd.h>

int main(void)
{
    time_t starttime = time(NULL);
    time_t seconds = 3; 
	time_t endtime = starttime + seconds;

    while (starttime < endtime) {
        sleep(1);   
        starttime = time(NULL);
        printf("%s", ctime(&starttime));
    }


    return 0;
}
	 
	 
/*
run:
     
Sat Nov 30 22:19:08 2019
Sat Nov 30 22:19:09 2019
Sat Nov 30 22:19:10 2019
     
*/

 





answered Nov 30, 2019 by avibootz

Related questions

2 answers 78 views
2 answers 197 views
2 answers 94 views
2 answers 120 views
...