while语句中的sleep函数

rog*_*ogi 2 c

我无法理解为什么这段代码不会每隔一秒打印当前时间.这里有什么问题 ?

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


int main(int argc, const char * argv[])
{
    while (1) {
        sleep(1);
        time_t tm;
        struct tm *t_struct;
        tm = time(NULL);
        t_struct = localtime(&tm);

        printf("%.2d:%.2d:%.2d", t_struct->tm_hour, t_struct->tm_min, t_struct->tm_sec);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

eq-*_*eq- 11

stdout可能是行缓冲,因此您可能需要fflush在输出文本后使用它,或者打印换行以使更改可见.