{"id":1381,"date":"2015-05-30T00:01:23","date_gmt":"2015-05-30T07:01:23","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1381"},"modified":"2015-06-06T07:38:43","modified_gmt":"2015-06-06T14:38:43","slug":"hello-system","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1381","title":{"rendered":"Hello, System"},"content":{"rendered":"<p>One thing that continues to puzzle me about learning the C language is how frequently beginners use the <em>system()<\/em> function. Is it a crutch? Is it a necessity? What&#8217;s the allure of this function that makes it show up in beginner code?<br \/>\n<!--more--><br \/>\nThe <em>system()<\/em> function is an old and necessary function. It spawns a new shell and sends a command to that shell. The whole effect is that you can run one program from within another. In fact, the terminal window uses the <em>system()<\/em> function to run other programs. Even graphical operating systems use <em>system()<\/em> (or some variation) to launch programs and apps.<\/p>\n<p>As an example, suppose your code needs run the <code>blorf<\/code> program. If so, you&#8217;d use the following statement:<\/p>\n<pre><code>    system(\"blorf\");<\/code><\/pre>\n<p>At that point in the code, your program snoozes and program <code>blorf<\/code> starts. When <code>blorf<\/code> terminates, control returns back to your code.<\/p>\n<p>Beginning C programmers typically use the <em>system()<\/em> function to clear the screen. All too often, I&#8217;d see the following example either written by a beginner or presented as an example in some oddball C programming text:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"Press any key to clear the screen:\");\r\n    getchar();\r\n    system(\"cls\");    <span class=\"comments\">\/* use \"clear\" for Unix *\/<\/span>\r\n    printf(\"Thanks!\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>system()<\/em> function is prototyped in the <code>stdlib.h<\/code> header file. Beyond that, its argument is a command line string, something you&#8217;d type at the prompt, such as <code>cls<\/code> (DOS) or <code>clear<\/code> (Unix).<\/p>\n<p>C is stream-oriented, so clearing the screen isn&#8217;t something offered by the C library; to clear the screen you must either program the terminal directly or use a screen manipulation library, such as NCurses. The other way you can clear the screen is to call the terminal (or operating system in the case of DOS) and issue the clear-screen command.<\/p>\n<p>I&#8217;ve used the <em>system()<\/em> function to write a DOS menu system, as well as in some of my programs so that the user could &#8220;shell to DOS.&#8221; <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1389\">Next week<\/a>, I&#8217;ll provide a practical example of how to use the <code>system()<\/code> function, one that I frequently use when testing my code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Running one program from within another. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1381\">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-1381","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\/1381","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=1381"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1381\/revisions"}],"predecessor-version":[{"id":1415,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1381\/revisions\/1415"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}