Why is printf before exevp not running?

折月煮酒 提交于 2019-11-29 23:20:47

问题


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:


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)



来源:https://stackoverflow.com/questions/32547540/why-is-printf-before-exevp-not-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!