{"id":5179,"date":"2022-02-05T00:01:58","date_gmt":"2022-02-05T08:01:58","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5179"},"modified":"2022-02-05T09:34:28","modified_gmt":"2022-02-05T17:34:28","slug":"capturing-a-programs-return-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5179","title":{"rendered":"Capturing a Program&#8217;s Return Value"},"content":{"rendered":"<p>At the end of the <em>main()<\/em> function, a <em>return<\/em> statement must surrender an integer value to the operating system. Does a higher value please the OS? Is it offended by zero? What exactly happens with the value returned when your program is done?<br \/>\n<!--more--><br \/>\nFrankly, the operating system doesn&#8217;t give a rip about the return value. The value is retained by the shell, stored as a temporary variable until another program deposits its return value.<\/p>\n<p>For shell script programming in Linux, or batch file programming in Windows, you can access the return value. In Windows, the return value is stored in the batch file variable, <code>ERRORLEVEL<\/code>. In Unix\/Linux, the return value is accessible through the shell wildcard <code>$?<\/code>. Within a program, however, the two operating systems differ in how they can access the return value from a spawned or child program.<\/p>\n<p>To start with, I present the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_02_05-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_02_05-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    puts(\"I return the value 12 to the operating system\");\r\n\r\n    return(12);\r\n}<\/pre>\n<p>This program has one purpose, to return the value 12 to the operating system. The reason for such silliness is to see how another program running <em>twelve<\/em> as a spawned or child process detects this return value.<\/p>\n<p>If you run the program in <em>Code::Blocks<\/em>, the return value is present in the command window output:<\/p>\n<p><code>I return the value 12 to the operating system<br \/>\nProcess returned 12 (0xC)&nbsp;&nbsp;&nbsp;execution time : 0.00 s<br \/>\nPress any key to continue.<\/code><\/p>\n<p>In Linux, run the program at the command prompt and then use the <code>echo<\/code> command to confirm the return value:<\/p>\n<p><code>$ echo $?<br \/>\n12<\/code><\/p>\n<p>At the command prompt in Windows, you must code a batch file to fish out the return value. It&#8217;s a complex task I don&#8217;t need to get into in this Lesson. But if you&#8217;re curious, dig up one of my ancient books on <em>MS-DOS Batch File Programming<\/em>, available in dustbins around the planet.<\/p>\n<p>But what happens with the return value when one program spawns another? Can the first program retrieve the value?<\/p>\n<p>Back when I was coding in DOS, I used the <em>system()<\/em> for one program to run another. It accepts an argument as a string you would type at the command prompt. The return value from <em>system()<\/em> is the program&#8217;s return value. This approach doesn&#8217;t work in Linux.<\/p>\n<p>The following code runs the <code>twelve<\/code> program, which is created from the source code shown earlier.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_02_05-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2022_02_05-Lesson-b.c<\/a><\/h3>\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 r;\r\n\r\n    r = system(\".\/twelve\");\r\n    printf(\"Child process exit status = %d\\n\",r);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In Windows, the <em>system()<\/em> function (Line 8) must be changed to read: <code>system(\"twelve\")<\/code><\/p>\n<p>Assuming that the <em>twelve<\/em> program exists in the same directory where you run the above code&#8217;s program, here is the output in Windows:<\/p>\n<p><code>I return the value 12 to the operating system<br \/>\nChild process exit status = 12<\/code><\/p>\n<p>And here is the output from Linux:<\/p>\n<p><code>I return the value 12 to the operating system<br \/>\nChild process exit status = 3072<\/code><\/p>\n<p>The value you see in Windows is the program&#8217;s actual return value. The value appearing in Linux is the return value of the shell &mdash; not the program run.<\/p>\n<p>It&#8217;s possible to obtain a child program&#8217;s return value in Linux. But to do so you must do more than use the <em>system()<\/em> function. I cover this topic in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A good C programming knows that the operating system requires its return value. But what can be done with this value? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5179\">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":[32,33,30,29,34,27,28,31],"class_list":["post-5179","post","type-post","status-publish","format-standard","hentry","category-main","tag-execl","tag-fork","tag-linux","tag-operating-system-return-value","tag-process","tag-return-value","tag-system-call","tag-wait-function"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5179","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=5179"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5179\/revisions"}],"predecessor-version":[{"id":5202,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5179\/revisions\/5202"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}