How to sleep for 3 seconds in C languagerefer to:lautturi.comExample of the millisecond sleep function:
/*
Example: sleep 3 seconds in C language
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>
int msleep(long msec)
{
struct timespec ts;
int res;
if (msec < 0)
{
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
int main(int argc, char *argv[]) {
time_t now = time(0);
printf("the time: %ld\n", now);
msleep(3000); // Sleep for 3 seconds
now = time(0);
printf("the time: %ld\n", now);
return 0;
}