当我运行以下程序时,system("ls -l")
的输出显示在 printf
的输出之前。为什么会这样?
#include<stdio.h>
int main()
{
printf("\nHello world");
system("ls -l"); // output of this statement is displayed before that of the preceding
// printf statement
return 0;
}
谢谢。
最佳答案
printf
被缓冲。据我所知,仅当存在 \n
或您显式刷新它(通过 fflush(3)
)时,缓冲区才会写入输出。
那么发生的事情是,printf
将 \n
写入输出,然后缓冲字符串的其余部分。然后 ls -l
被执行,当你的程序完成时缓冲区被自动刷新。
关于c - 无法理解 C 程序中 "system"函数调用的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25051566/