user10621144
user10621144

Reputation:

How do you make a function that "waits" an x amount of seconds in C?

I'm trying to make the terminal wait an x numbers of seconds before printing something on screen. I literally copied the code from somewhere else online but my terminal just doesn't wait any time at all and executes everything altogether as it normally would. Do you guys know why this happens?


for(int i = 0; i < 5; i++){
    delay(5);
    printf(". ");
}

void delay(int number_of_seconds)
{
    // Converting time into milli_seconds
    int milli_seconds = 1000 * number_of_seconds;

    // Stroing start time
    clock_t start_time = clock();

    // looping till required time is not acheived
    while (clock() < start_time + milli_seconds)
        ;
}

Upvotes: 0

Views: 337

Answers (1)

Julio P.C.
Julio P.C.

Reputation: 330

There's a "sleep" function, on unistd.h

#include <unistd.h>

//something your code

sleep(seconds);

Hope that helps

Upvotes: 3

Related Questions