{"id":1410,"date":"2015-06-13T00:01:04","date_gmt":"2015-06-13T07:01:04","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1410"},"modified":"2021-03-29T09:26:55","modified_gmt":"2021-03-29T16:26:55","slug":"execute-and-leave","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1410","title":{"rendered":"Execute and Leave"},"content":{"rendered":"<p>The <em>system()<\/em> function allows you to run one program from within another. If it&#8217;s your desire to launch another program and have your program quit, you can immediately follow <em>system()<\/em> with an <em>exit()<\/em> function. Or you can go out of your way and use the oddball <em>execl()<\/em> function.<br \/>\n<!--more--><br \/>\nI&#8217;ll admit that the <em>execl()<\/em> function isn&#8217;t common or even admired. That&#8217;s because most coders simply do this:<\/p>\n<pre><code>system(\"cc -v\");\r\nexit();<\/code><\/pre>\n<p>The above statements direct the terminal to run the command <em>cc -v<\/em> and then the current program quits. That&#8217;s pretty simple and you won&#8217;t endure any punishment for using this technique.<\/p>\n<p>The <em>execl()<\/em> function very specific. It requires more setup because the function&#8217;s first argument is the command&#8217;s full pathname. On the upside, the function offers more flexibility when it comes to dealing with the command line arguments, which are specified separately.<\/p>\n<p>To use the <em>execl()<\/em> function, include the <code>unistd.h<\/code> header file. Here&#8217;s the function&#8217;s argument list:<\/p>\n<ul>\n<li>The first argument is the command&#8217;s full pathname.<\/li>\n<li>The second argument is the command name. This may seem redundant, and it is.<\/li>\n<li>The third through <em>n<\/em>th arguments are any command line options, each of which is specified separately.<\/li>\n<li>The final argument is always a null character pointer.<\/li>\n<\/ul>\n<p>Here&#8217;s a quick example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    puts(\"Compiler Info:\");\r\n    execl(\"\/usr\/bin\/cc\",\"cc\",\"-v\",(char *)NULL);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>execl()<\/em> function at Line 7 runs the <em>cc<\/em> command with the <code>-v<\/code> option. The output is version information for the system&#8217;s C compiler.<\/p>\n<p>On a PC, you could use the following statement for Line 7:<\/p>\n<pre><code>    execl(\"C:\\\\MinGW\\\\bin\\\\gcc.exe\",\"gcc\",\"-v\",(char *)NULL);<\/code><\/pre>\n<p>Above, I&#8217;m assuming that <em>gcc<\/em> is installed in the <code>\\MinGW\\bin<\/code> folder. Two backslashes are required for a Windows pathname, which is chronically stupid, but that&#8217;s Microsoft.<\/p>\n<p>If Code::Blocks is installed, try using this statement instead:<\/p>\n<pre><code>    execl(\"C:\\\\Program Files (x86)\\\\CodeBlocks\\\\MinGW\\\\bin\\\\gcc.exe\",\"gcc\",\"-v\",(char *)NULL);<\/code><\/pre>\n<p>Based on the complexity of the <em>execl()<\/em> statement, you can see how <em>system()<\/em> remains a far more popular alternative. In fact, the only time I would consider using <em>execl()<\/em> is when the command&#8217;s arguments must be customized. It would be easier to specify them one at a time than trying to concatenate a customized command string for the <em>system()<\/em> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chain one program to another with the <em>execl()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1410\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1410","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1410"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1410\/revisions"}],"predecessor-version":[{"id":4705,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1410\/revisions\/4705"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}