【问题标题】:Unable to understand behaviour of "system" function call in C program无法理解 C 程序中“系统”函数调用的行为
【发布时间】:2014-07-31 05:42:34
【问题描述】:

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

谢谢。

【问题讨论】:

  • system 是一个库函数,而不是系统调用。为什么要在行首而不是行尾打印'\n'

标签: c system call


【解决方案1】:

printf 被缓冲。 AFAIK 仅当存在 \n 或显式刷新它(通过 fflush(3))时,缓冲区才会写入输出。

所以发生的情况是,printf\n 写入输出,然后缓冲字符串的其余部分。然后ls -l 被执行,当你的程序完成时,缓冲区会自动刷新。

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    相关资源
    最近更新 更多