Search code examples
csystempopen

what is the difference between popen() and system() in C


I want to execute a binary within my C code. Which is better to execute with? popen() or system()

EDIT: I tried to use system, but the process executing seems to get stuck in the end and does not return to my code.

Any suggestions on what to do?

Thanks


Solution

  • popen() gives you control over the process's input or output file streams. system() doesn't. If you don't need to access the process's I/O, you can use system() for simplicity.

    system() is in C89 and C99; popen() is Posix only (though the Windows API also has one).

    Both functions invoke some form of a shell to execute the command (e.g. /bin/sh on Linux, and probably cmd.exe on Windows). If you want to execute an executable file directly and you are on Posix, you can also look at the exec*-family of functions in conjuction with fork() (since exec() replaces the current process).