简体   繁体   English

为什么printf()在睡眠前没有打印任何东西()?

[英]Why does printf() not print anything before sleep()?

I'm just learning C with Kernighan and Ritchie's book; 我只是用Kernighan和Ritchie的书学习C语言; I'm in the basics of the fourth chapter (functions stuff). 我在第四章的基础知识(函数)。 The other day I became curious about the sleep() function, so tried to use it like this: 前几天我对sleep()函数感到好奇,所以试着像这样使用它:

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

int main(void)
{
  printf(" I like cows.");
  sleep(5);
  return 0;
}

The problem is the output of the program, it looks like it does the sleep() first and then the printf() , in other words, it waits five seconds and then prints the string. 问题是程序的输出,看起来它首先是sleep()然后是printf() ,换句话说,它等待五秒然后打印字符串。 So I thought, maybe the program gets to sleep() so fast that it doesn't let printf() have his work done like I want, that is print the string and then sleep. 所以我想,也许程序会如此快速地sleep() ,以至于它不会让printf()像我想要的那样完成他的工作,即打印字符串然后再睡觉。

How can I show the string and then put the program to sleep? 如何显示字符串然后让程序进入睡眠状态? The compiler is GCC 3.3.5 (propolice) in OpenBSD 4.3. 编译器是OpenBSD 4.3中的GCC 3.3.5(propolice)。

PS I don't know how you put here the preprocessor lines correctly. PS我不知道你如何正确地放置预处理器线。

printf() writes to stdout (the default output stream) which is usually line buffered. printf()写入stdout (默认输出流),它通常是行缓冲的。 The buffer isn't flushed by the time sleep is called so nothing is displayed, when the program exits all streams are automatically flushed which is why it prints right before exiting. 调用sleep不会刷新缓冲区,因此当程序退出所有流时会自动刷新,这就是为什么它在退出之前打印的原因。 Printing a newline will usually cause the stream to be flushed, alternatively you could use the fflush function: 打印换行符通常会导致流刷新,或者您可以使用fflush功能:

int main(void)
{
  printf(" I like cows.\n");
  sleep(5);
  return 0;
}

or: 要么:

int main(void)
{
  printf(" I like cows.");
  fflush(stdout);
  sleep(5);
  return 0;
}

If you are printing to a stream that is not line buffered, as may be the case if stdout is redirected or you are writing to a file, simply printing a newline probably won't work. 如果要打印到不是行缓冲的流,如果重定向stdout或者您正在写入文件,可能就是这样,只是打印换行可能不起作用。 In such cases you should use fflush if you want the data written immediately. 在这种情况下,如果您想立即写入数据,则应使用fflush

Your problem is that printf (and anything else that uses the stdio library to write to stdout (standard output)) is buffered - line buffered if it goes to the console, and size buffered if it goes to a file. 您的问题是printf(以及使用stdio库写入stdout(标准输出)的任何其他内容)被缓冲 - 如果它进入控制台则行缓冲,如果它转到文件则缓冲大小。 If you do a fflush(stdout); 如果你做fflush(stdout); after the printf , it will do what you want. printf ,它会做你想要的。 You could try just adding a newline ('\\n') to your string, and that would do the right thing as long as you don't redirect standard output to a file. 您可以尝试在字符串中添加换行符('\\ n'),只要不将标准输出重定向到文件,这就行了。

I'm not 100% sure, but I think stderr isn't buffered, which can cause confusion because you might see output you made to stderr before output you previously made to stdout . 我不是百分百肯定,但我认为stderr没有缓冲,这可能会引起混淆,因为你可能会在stderr之前输出到stdout之前看到你对stderr做的stdout

Buffering means that all the output is stored in a place (called buffer) and is output after a certain amount of data is present in it. 缓冲意味着所有输出都存储在一个位置(称为缓冲区),并在其中存在一定量的数据后输出。 This is done for efficiency reasons. 这是出于效率原因而完成的。

Some (most?) implementations clear the buffer after a newline when writing to the console, so you can also try 一些(大多数?)实现在写入控制台的换行符后清除缓冲区,因此您也可以尝试

printf(" I like cows.\n");

instead of the call to fflush() 而不是调用fflush()

I implemented time encounter as following; 我实施了时间遇到如下;

for (int i = 1; i <= 60; i++) {
    printf("%02d", i);
    fflush(stdout);
    sleep(1);
    printf("\b\b");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM