小编Aho*_*rse的帖子

为什么我的纳米睡眠不起作用......?

我编写了以下代码来逐字符地打印段落,间隔为0.3秒.但是当我编译并运行它时,它会打印出句子中的所有内容.为什么纳秒功能不起作用?

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

int main() {
    int i = 0;
    struct timespec t1, t2;
    t1.tv_sec = 0;
    t1.tv_nsec = 300000000L;

    char story[] = {"I want to print this story / letter by letter on the screen./"};
    while(story[i] != '\0') {
        if(story[i] == '/')
            sleep(1);
        else
            printf("%c", story[i]);
    nanosleep(&t1, &t2);
        i++;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c sleep stdout stream

0
推荐指数
1
解决办法
1149
查看次数

标签 统计

c ×1

sleep ×1

stdout ×1

stream ×1