c - 无法理解 C 程序中 "system"函数调用的行为

标签 c system call

当我运行以下程序时,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/

相关文章:

c - 文件内的值不会被替换

c - C 运行时对于任何其他程序(如 lua、haskell、java 等)来说是必不可少的吗?

java - 为什么Java中System类声明为 "final"?

c - 使用 printf 格式化列

c++ - System() 的替代方案,用于在程序中运行批处理文件

Perl - 需要一遍又一遍地调用系统 ('diff' )

python - 如何使用 | 调用多个 bash 函数在 python

c++ - 创建库调用层次结构的工具

java - 当特定通知到达时调用电话

objective-c - 将 Objective C 转换为 C 矩阵操作