{"id":3445,"date":"2019-01-12T00:01:48","date_gmt":"2019-01-12T08:01:48","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3445"},"modified":"2019-01-05T15:11:00","modified_gmt":"2019-01-05T23:11:00","slug":"im-outta-here","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3445","title":{"rendered":"I&#8217;m Outta Here"},"content":{"rendered":"<p>A specific command to terminate a program was popular with programming languages long ago. For example, in BASIC you had the <code>END<\/code> command, which exited a program. For unusual circumstances, you could use the <code>STOP<\/code> command. It was the abnormal termination command, which stopped the program and also generated a &#8220;Break in <em>line<\/em>&#8221; message (in GW Basic). I&#8217;m sure other early languages also had commands to bail out of a program, and modern languages are the same.<br \/>\n<!--more--><br \/>\nThe C language lacks a keyword to end a program. Most users today would believe that the <em>return<\/em> keyword does so, but this thought isn&#8217;t exactly true.<\/p>\n<p>In the original <em>C Programming Language<\/em> (K&#038;R) book, the <code>Hello, world!<\/code> program lacks a <em>return<\/em> statement. That&#8217;s because declaring the <em>main()<\/em> function as an <em>int<\/em> wasn&#8217;t required until later in the C standard. In fact, many early programs used the <em>void<\/em> data type for <em>main()<\/em>, which cemented that it didn&#8217;t need a <em>return<\/em> statement; the program stopped after the last statement in the function was executed. Today it&#8217;s different.<\/p>\n<p>You can quit any program by placing a <em>return<\/em> statement in the <em>main()<\/em> function. So that&#8217;s one way to leave, but doing so works only in the <em>main()<\/em> function.<\/p>\n<p>When you must bail out from another function, say somewhere deep in the bowels of complex code, use the <em>exit()<\/em> function. It&#8217;s prototyped in the <code>stdlib.h<\/code> header file and accepts an integer argument, which is the same value a <em>return<\/em> statement passes to the operating system when a program quits.<\/p>\n<p>The <em>exit()<\/em> function has a benefit in that it performs a few clean-up chores before it releases the program&#8217;s life essence: It calls any functions specified by the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1371\"><em>atexit()<\/em> function<\/a>; it flushes all output streams (sending output to a file or standard output); closes all open streams; and it removes any temporary files created. Especially for larger, complex programs, using <em>exit()<\/em> is a wise choice.<\/p>\n<p>When you must abandon your program for some hot reason, a good choice is to use the <em>abort()<\/em> function. Prototyped in the <code>stdlib.h<\/code> header file (same as <em>exit()<\/em>), the <em>abort()<\/em> function it requires no argument: Calling it generates an abnormal program termination condition. It does, however, flush and close any open streams.<\/p>\n<p>The following code demonstrates how effective and rude the <em>abort()<\/em> function can be:<\/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    char c;\r\n\r\n    printf(\"Type a letter: \");\r\n    scanf(\"%c\",&amp;c);\r\n    if( c &gt; 'M' )\r\n        abort();\r\n    puts(\"Whew!\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code prompts for input and compares the letter typed with M. If the character is greater (all the lowercase letters are), the program aborts. Otherwise, the code displays &#8220;Whew!&#8221; and terminates normally.<\/p>\n<p>Here&#8217;s a sample run:<br \/>\n<code>Type a letter: A<br \/>\nWhew!<\/code><\/p>\n<p>And another:<br \/>\n<code>Type a letter: i<br \/>\nAbort trap 6<\/code><\/p>\n<p>On my Ubuntu system, the error message is <code>Aborted (core dumped)<\/code>. On the PC in Code::Blocks, the program stalled for a few seconds and then terminated without any supplemental message. Regardless, the point is made: You&#8217;re program has exited abruptly. This type of message can be handy for you as a programmer, though I would caution against using it casually in that such an abrupt exit requires more explanation for a user.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ways to leave your program, gracefully or not. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3445\">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-3445","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\/3445","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=3445"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3445\/revisions"}],"predecessor-version":[{"id":3455,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3445\/revisions\/3455"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}