How to write a sleep function in milliseconds with C

1 Answer

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

void sleep(unsigned int milliseconds) {
    clock_t counter = milliseconds + clock();
    while (counter > clock()) ;
}

int main(int argc, char **argv)
{
	printf("abc\n");

	sleep(2000);

    printf("xyz\n");

    return 0;
}

/*
run:

abc
xyz

*/

 



answered Dec 22, 2018 by avibootz
edited Dec 23, 2018 by avibootz

Related questions

1 answer 168 views
1 answer 1,184 views
1 answer 174 views
1 answer 137 views
137 views asked Oct 5, 2021 by avibootz
...