<p>How do you run an external program and pass it command line parameters using C? If you have to use operating system API, include a solution for Windows, Mac, and Linux.</p>

[{'answer_id': 17148, 'author': 'wilhelmtell', 'author_id': 456, 'author_profile': 'https://Stackoverflow.com/users/456', 'pm_score': 4, 'selected': False, 'text': '<pre><code>#include &lt;stdlib.h&gt;\n\nint main()\n{\n    system("echo HAI");\n\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 17150, 'author': 'Rippy', 'author_id': 1791, 'author_profile': 'https://Stackoverflow.com/users/1791', 'pm_score': 2, 'selected': False, 'text': '<p>One solution is the system function defined in stdlib.h </p>\n\n<pre><code>int system(const char *string);\n</code></pre>\n\n<p><a href="http://www.cplusplus.com/reference/clibrary/cstdlib/system.html" rel="nofollow noreferrer">system api example</a></p>\n'}, {'answer_id': 17226, 'author': 'fulmicoton', 'author_id': 446497, 'author_profile': 'https://Stackoverflow.com/users/446497', 'pm_score': 2, 'selected': False, 'text': '<p>On UNIX, I think you basically need to fork it if you want the spawned process to run detached from your the spawing one : For instance if you don\'t want your spawned process to be terminate when you quit your spawning process.</p>\n\n<p><a href="http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html" rel="nofollow noreferrer">Here is a</a> page that explains all the subtle differences between Fork, System, Exec.</p>\n\n<p>If you work on Win,Mac and linux, I can recommend you the <a href="https://doc.qt.io/qt-5/qprocess.html" rel="nofollow noreferrer">Qt Framework and its QProcess object</a>, but I don\'t know if that\'s an option for you. The great advantages is that you will be able to compile the same code on windows linux and mac :</p>\n\n<pre><code> QString program = "./yourspawnedprogram";\n QProcess * spawnedProcess = new QProcess(parent);\n spawnedProcess-&gt;start(program);\n // or spawnedProcess-&gt;startDetached(program);\n</code></pre>\n\n<p>And for extra, you can even kill the child process from the mother process,\nand keep in communication with it through a stream.</p>\n'}, {'answer_id': 17311, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 4, 'selected': False, 'text': '<p>If you want to perform more complicated operations, like reading the output of the external program, you may be better served by the <a href="http://man.he.net/man3/popen" rel="noreferrer">popen</a> system call. For example, to programmatically access a directory listing (this is a somewhat silly example, but useful <em>as</em> an example), you could write something like this:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nint main()\n{\n  int entry = 1;\n  char line[200];\n  FILE* output = popen("/usr/bin/ls -1 /usr/man", "r");\n  while ( fgets(line, 199, output) )\n  {\n    printf("%5d: %s", entry++, line);\n  }\n}\n</code></pre>\n\n<p>to give output like this</p>\n\n<pre><code>1: cat1\n2: cat1b\n3: cat1c\n4: cat1f\n5: cat1m\n6: cat1s\n...\n</code></pre>\n'}, {'answer_id': 20752, 'author': 'FreeMemory', 'author_id': 2132, 'author_profile': 'https://Stackoverflow.com/users/2132', 'pm_score': 5, 'selected': False, 'text': '<p>It really depends on what you\'re trying to do, exactly, as it\'s:</p>\n\n<ol>\n<li>OS dependent</li>\n<li>Not quite clear what you\'re trying to do.</li>\n</ol>\n\n<p>Nevertheless, I\'ll try to provide some information for you to decide.<br>\nOn UNIX, <code>fork()</code> creates a clone of your process from the place where you called fork. Meaning, if I have the following process:</p>\n\n<pre><code>#include &lt;unistd.h&gt;\n#include &lt;stdio.h&gt;\n\nint main()\n{\n    printf( "hi 2 u\\n" );\n    int mypid = fork();\n\n    if( 0 == mypid )\n        printf( "lol child\\n" );\n    else\n        printf( "lol parent\\n" );\n\n    return( 0 );\n}\n</code></pre>\n\n<p>The output will look as follows:</p>\n\n<blockquote>\n  <blockquote>\n    <p>hi 2 u<br>\n    lol child<br>\n    lol parent  </p>\n  </blockquote>\n</blockquote>\n\n<p>When you <code>fork()</code> the pid returned in the child is 0, and the pid returned in the parent is the child\'s pid. Notice that "hi2u" is only printed once... by the <strong>parent</strong>.</p>\n\n<p><code>execve()</code> and its family of functions are almost always used with <code>fork().</code> <code>execve()</code> and the like overwrite the current stackframe with the name of the application you pass to it. <code>execve()</code> is almost always used with <code>fork()</code> where you fork a child process and if you\'re the parent you do whatever you need to keep doing and if you\'re the child you exec a new process. <code>execve()</code> is also almost always used with <code>waitpid()</code> -- waitpid takes a pid of a child process and, quite literally, <em>waits</em> until the child terminates and returns the child\'s exit status to you.  </p>\n\n<p>Using this information, you should be able to write a very basic shell; one that takes process names on the command line and runs processes you tell it to. Of course, shells do more than that, like piping input and output, but you should be able to accomplish the basics using <code>fork()</code>, <code>execve()</code> and <code>waitpid()</code>.</p>\n\n<p><strong>NOTE: This is *nix specific! This will NOT work on Windows.</strong>  </p>\n\n<p>Hope this helped.</p>\n'}, {'answer_id': 389288, 'author': 'mouviciel', 'author_id': 45249, 'author_profile': 'https://Stackoverflow.com/users/45249', 'pm_score': 1, 'selected': False, 'text': '<p>If you need to check/read/parse the output of your external command, I would suggest to use popen() instead of system().</p>\n'}, {'answer_id': 389352, 'author': 'Constantin', 'author_id': 20310, 'author_profile': 'https://Stackoverflow.com/users/20310', 'pm_score': 1, 'selected': False, 'text': '<p>Speaking of platform-dependent recipes, on Windows use <a href="http://msdn.microsoft.com/en-us/library/ms682425.aspx" rel="nofollow noreferrer">CreateProcess</a>, on Posix (Linux, Mac) use <code>fork</code> + <code>execvp</code>. But <code>system()</code> should cover your basic needs and is part of standard library.</p>\n'}, {'answer_id': 14074084, 'author': 'Lothar', 'author_id': 155082, 'author_profile': 'https://Stackoverflow.com/users/155082', 'pm_score': 3, 'selected': False, 'text': '<p>I want to give a big warning to not use system and 100% never use system when you write a library. It was designed 30 years ago when multithreading was unknown to the toy operating system called Unix. And it is still not useable even when almost all programs are multithreaded today. </p>\n\n<p>Use popen or do a fork+execvp, all else is will give you hard to find problems with signal handling, crashs in environment handling code etc. It\'s pure evil and a shame that the selected and most rated answer is promoting the use of "system". It\'s more healthy to promote the use of Cocain on the workplace.</p>\n'}]
['https://Stackoverflow.com/questions/17140', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']