How to achieve microsecond-level delay in C language?

In the C language, the usleep() function can be used to achieve microsecond-level delays. The prototype of the usleep() function is as follows:

#include <unistd.h>

int usleep(useconds_t usec);

The usleep() function suspends the process for a specified number of microseconds, essentially creating a delay at the microsecond level. The parameter usec represents the number of microseconds to delay.

Here is an example of implementing microsecond-level delay using the usleep() function.

#include <stdio.h>
#include <unistd.h>

int main() {
    int delayTime = 1000000; // 延时1秒
    usleep(delayTime);
    printf("延时结束\n");

    return 0;
}

In the example above, suspending the process for 1 second using the usleep() function, and then outputting “Delay End”.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds