小编Bl4*_*4z3的帖子

C程序在被告知之前执行命令

我已经开始研究使用C的命令处理,但我遇到了这个C程序的问题.它正在执行ls命令之前.

Gcc信息:

gcc version 6.2.1 20161124 (Debian 6.2.1-5)
Run Code Online (Sandbox Code Playgroud)

这是代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;

    printf("Is command processor available?\n");
    if (system(NULL))
    {
        printf("Command processor available!\n");
    }
    else
    {
        printf("Command processor not available!\n");
        exit(1);
    }

    printf("Executing command ls");
    i=system("ls");

    printf("Returned value is: %d.\n",i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我所说的代码就是这条特定的代码:

    printf("Executing command: ls");
Run Code Online (Sandbox Code Playgroud)

如果使用该段代码运行程序,则输出为:

Is command processor available?
Command processor is available
systemProcessing  systemProcessing.c
Executing command: lsReturned value is: 0.
Run Code Online (Sandbox Code Playgroud)

它在实际被告知之前执行命令

但是当我使用新行"\n"完成代码时,其输出符合预期:

Is command processor available?
Command processor is …
Run Code Online (Sandbox Code Playgroud)

c printf command-execution

3
推荐指数
2
解决办法
103
查看次数

标签 统计

c ×1

command-execution ×1

printf ×1