Why is printf before exevp not running?

2019-02-11 02:02发布

I get an output of "hi!". Why is this not also printing "something"?

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

int main(int argc, char** argv) {
    char* program_name = "echo";
    char* args[]= {program_name,"hi!",NULL};

    printf("something");
    execvp(program_name,args);
    return 0;
}

I know I'm not creating a child process first. If I take out the execvp line, it works as expected. Weird. (Note: "echo" refers to https://en.wikipedia.org/wiki/Echo_(command))

1条回答
迷人小祖宗
2楼-- · 2019-02-11 02:51

The string is in the io buffer - so pull the chain and flush that buffer

i.e. add

fflush(stdout)

after the printf (or add \n to the printf)

查看更多
登录 后发表回答