{"id":3508,"date":"2019-02-23T00:01:25","date_gmt":"2019-02-23T08:01:25","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3508"},"modified":"2019-02-16T10:24:24","modified_gmt":"2019-02-16T18:24:24","slug":"exit-status-defined-constants","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3508","title":{"rendered":"Exit Status Defined Constants"},"content":{"rendered":"<p>The tradition for successfully exiting a command line program is to use <code>return 0;<\/code> or, as I write it, <code>return(0);<\/code>. Zero is the OK value, meaning a program exited cleanly. Other values returned represent specific conditions, not necessarily errors, but information that can be communicated to the operating system or some other program.<br \/>\n<!--more--><br \/>\nThe same value is used as the <em>exit()<\/em> function&#8217;s argument. Unlike <em>return<\/em> (which is a keyword), you can use <em>exit()<\/em> beyond the <em>main()<\/em> function to bail out. And if you want to get fancy, you can use one of two defined constants available in the <code>stdlib.h<\/code> header file: <code>EXIT_SUCCESS<\/code> and <code>EXIT_FAILURE<\/code>. Here&#8217;s some code:<\/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    int d;\r\n\r\n    printf(\"Type a positive number: \");\r\n    scanf(\"%d\",&amp;d);\r\n\r\n    if( d &gt; 0 )\r\n    {\r\n        puts(\"Thanks!\");\r\n        return(EXIT_SUCCESS);\r\n    }\r\n    else\r\n    {\r\n        puts(\"Wrong!\");\r\n        return(EXIT_FAILURE);\r\n    }\r\n}<\/pre>\n<p>You don&#8217;t need to look up the defined constants&#8217; assigned values because I already did: zero for <code>EXIT_SUCCESS<\/code> and 1 for <code>EXIT_FAILURE<\/code>, but that&#8217;s not the point of using them.<\/p>\n<p>Like many things in C, it&#8217;s a good idea to use constants wherever possible. That way you can universally change a value by making a single modification to your code. The same thinking holds true for the exit constants. Though it&#8217;s highly unlikely, at some point in the future an operating system may accept a value such as -1 meaning &#8220;exit successful.&#8221; If such a thing came to pass (and it won&#8217;t), you would have to update all your legacy code, scanning for <em>return<\/em> and <em>exit()<\/em> points and changing the values as necessary.<\/p>\n<p>On the other hand, if you create code that uses the exit constants, compiling on the new operating system wouldn&#8217;t present an issue; the <code>EXIT_SUCCESS<\/code> constant would be updated for the operating system&#8217;s C compiler and no changes would be necessary.<\/p>\n<p>You can also define these constants in code that doesn&#8217;t require the <code>stdlib.h<\/code> header file. The following preprocessor directives do the trick:<\/p>\n<p><code>#ifndef EXIT_SUCCESS<br \/>\n#define EXIT_SUCCESS 0<br \/>\n#endif<br \/>\n#ifndef EXIT_FAILURE<br \/>\n#define EXIT_FAILURE 1<br \/>\n#endif<\/code><\/p>\n<p>The above directives, either at the start of the source code file or in a user-defined header file, are all that&#8217;s needed to create the constants. And when that operating system that requires a -1 return value for a program comes around (and it won&#8217;t), you&#8217;re ready to compile your compatible code.<\/p>\n<p>For assistance on the preprocessor directives, see my <a href=\"https:\/\/c-for-dummies.com\/blog\/?page_id=2\">Preprocessor Directives<\/a> page here on the <em>C For Dummies<\/em> Blog.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to be fancy, you can use a defined constant instead of zero or some other number as the <em>main()<\/em> function&#8217;s return value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3508\">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-3508","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\/3508","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=3508"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3508\/revisions"}],"predecessor-version":[{"id":3515,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3508\/revisions\/3515"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}