1. 介绍一下。这部分比较简单。
- 强制休眠。与 Python 一样。
- 注意导包, 包的名称。unistd, uni -- std
- unistd, 即 Unix standard input/output, 这样好记一些。
- unistd, 包含一些系统相关的命令
2. 基本使用:
#include <stdio.h>
#include <unistd.h> // sleep()
// 强制休眠。与 Python 一样。
// 注意导包, 和包的名称。
// unistd, 即 unix standard input/output, 这样好记一些。
// 包含一些系统相关的命令
int main() {
printf("Please Wait.......\n");
fflush(stdout); //刷新缓冲区
sleep(2); //休眠2秒
printf("ok!\n");
return 0;
}
3. 倒计时, 计时器。
#include <stdio.h>
#include <unistd.h>
// 倒计时函数
void countdown()
{
int num = 5;
while (num > 0) {
printf("Time remaining: %d seconds\n", num);
sleep(1);
num--;
}
printf("Countdown finished.\n");
}
int main() {
printf("do more!\n");
countdown();
return 0;
}