{"id":1371,"date":"2015-05-23T00:01:50","date_gmt":"2015-05-23T07:01:50","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1371"},"modified":"2016-05-07T16:21:10","modified_gmt":"2016-05-07T23:21:10","slug":"before-you-say-goodbye","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1371","title":{"rendered":"Before You Say Goodbye . . ."},"content":{"rendered":"<p>The standard C library contains a lot of interesting and unusual routines. Some can really put the <em>fun<\/em> into function. One of them I&#8217;ve rarely used, but which can be extremely handy, is <em>atexit()<\/em>.<br \/>\n<!--more--><br \/>\nThe <em>atexit()<\/em> function is one of those weirdo functions that uses another function as its argument. (I&#8217;ll explain how that works in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1517\">a future Lesson<\/a>.) The function that <em>atexit()<\/em> references is run automatically whenever your program exits, either by returning from the <em>main()<\/em> function or at any point by calling <em>exit()<\/em>. Either way, whichever function <em>atexit()<\/em> registered is called before the program terminates.<\/p>\n<p>Sounds fun, right?<\/p>\n<p>Here&#8217;s a silly example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nvoid bye(void);\r\n\r\nint main()\r\n{\r\n    atexit(bye);\r\n\r\n    puts(\"Happy Little Program\");\r\n    puts(\"Massive processing completed!\");\r\n\r\n    return(0);\r\n}\r\n\r\nvoid bye(void)\r\n{\r\n    puts(\"So long, sailor!\");\r\n}<\/pre>\n<p>The <em>atexit()<\/em> function (Line 8) registers the <em>bye()<\/em> function to be run when the program quits. So when the <em>return<\/em> statement is hit at Line 13, the <em>bye()<\/em> function is called. Here&#8217;s sample output:<\/p>\n<pre><code>Happy Little Program\r\nMassive processing completed!\r\nSo long, sailor!<\/code><\/pre>\n<p>The <em>atexit()<\/em> can be called multiple times to have a spate of functions run when the program quits. The functions are run in the reverse order they&#8217;re called; so the first <em>atexit()<\/em> specifies the last function run. Here&#8217;s a modification to the sample code that illustrates that behavior:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nvoid bye(void);\r\nvoid solong(void);\r\n\r\nint main()\r\n{\r\n    atexit(bye);\r\n    atexit(solong);\r\n\r\n    puts(\"Happy Little Program\");\r\n    puts(\"Massive processing completed!\");\r\n\r\n    return(0);\r\n}\r\n\r\nvoid bye(void)\r\n{\r\n    puts(\"So long, sailor!\");\r\n}\r\n\r\nvoid solong(void)\r\n{\r\n    puts(\"Whew! That was rough.\");\r\n}<\/pre>\n<p>The <em>bye()<\/em> function is registered first by <em>atexit()<\/em> in Line 9. It&#8217;s called last.<\/p>\n<p>The <em>solong()<\/em> function is registered next, so it&#8217;s called before <em>bye()<\/em>.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>Happy Little Program\r\nMassive processing completed!\r\nWhew! That was rough.\r\nSo long, sailor!<\/code><\/pre>\n<p>A more practical example of how to use <em>atexit()<\/em> would be in a large program where some necessary cleanup would be performed. Rather than try to snake all the <em>return<\/em> calls and <em>exit()<\/em> routines together, simply register the cleanup functions by using <em>atexit()<\/em>. Then no matter where the program chooses to bail, you&#8217;ll know that things are taken care of.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The wonders of the <em>atexit()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1371\">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-1371","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\/1371","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=1371"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1371\/revisions"}],"predecessor-version":[{"id":1934,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1371\/revisions\/1934"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}