Create a timer in C

Solved
Anonymous user -  
 Anonymous user -
Hello,
I'm trying to create a timer in C. I found one on Google, but my timer is consuming 100% of the CPU (it's an infinite loop).

Please, how can I create a timer in C (for console) that doesn't use all my CPU?

PS: I'm on Windows.

Thanks in advance.

2 answers

  1. Anonymous user
     
    Thank you!!

    And for those who want an example, smile, here it is:

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

    int main()
    {
    long sec; //initialize sec for the seconds to wait

    printf("Enter the number of seconds to wait:\n"); //display the message on screen

    scanf("%d", &sec); //store the user's choice in the variable sec

    while (sec > 0) //create the loop. Don’t worry, this doesn’t occupy your CPU
    {
    printf("\rSeconds remaining: %03i", sec); //display the remaining time. %03i means display 3 digits (055 instead of 55 for example)
    sec--; //decrement the value of sec
    sleep(1000); //wait 1,000 ms (so one second) in each iteration of the loop
    }
    return 0;
    }

    There you go! Now I wish you happy programming!
    13