C语言的usleep函数怎么使用

小亿
186
2023-06-28 18:37:29
栏目: 编程语言

C语言中的usleep函数用于暂停程序的执行一段时间,单位为微秒(1秒=1000000微秒)。

该函数的原型为:

int usleep(useconds_t microseconds);

参数microseconds表示要暂停的时间,类型为useconds_t,即无符号整型。

使用该函数需要包含头文件<unistd.h>

下面是一个使用usleep函数的示例:

#include <stdio.h>
#include <unistd.h>
int main() {
printf("Start\n");
usleep(2000000); // 暂停2秒
printf("End\n");
return 0;
}

运行上述代码,输出结果为:

Start
(等待2秒)
End

在上述示例中,程序在执行到usleep函数时会暂停2秒,然后继续执行后面的代码。

0
看了该问题的人还看了