{"id":3187,"date":"2018-07-21T00:01:44","date_gmt":"2018-07-21T07:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3187"},"modified":"2018-07-14T11:51:30","modified_gmt":"2018-07-14T18:51:30","slug":"stupid-main-function-tricks","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3187","title":{"rendered":"Stupid <em>main()<\/em> Function Tricks"},"content":{"rendered":"<p>The <em>main()<\/em> function is the first C language function you learn to write. It&#8217;s required. And you probably know that it has arguments, which are culled from the command prompt and made available to the program. So what stupid stuff can you do with <em>main()<\/em>?<br \/>\n<!--more--><br \/>\nFirst, the argument list need not be specified for the <em>main()<\/em> function if you&#8217;re not using those arguments. Traditionally, they&#8217;re coded as <code>argc<\/code> for the argument count and <code>argv<\/code> for the list of items typed after the program name at the command prompt:<\/p>\n<p><code>int main(int argc, char *argv[])<\/code><\/p>\n<p>You don&#8217;t have to use these argument names, but it&#8217;s a good idea. That way you&#8217;re code is consistent with other C programs, but both must be specified in the proper order: <em>argument count<\/em> and then <em>argument values<\/em>.<\/p>\n<p>The <code>argc<\/code> integer is always greater than 0. That&#8217;s because the first argument is the program&#8217;s name. Even when you run a program in a GUI, it has a command line available for use and the first item is the command used to run the program.<\/p>\n<p>The <code>argv<\/code> argument is an array of strings representing the arguments typed at the command prompt or presented to the operating system in a GUI. The list is parsed, so each item is referenced separately, as a string in the array. (Items enclosed in double quotes are treated as a single string.) The first element in the array is the program name or command issued to run the code.<\/p>\n<p>The <code>argv<\/code> argument can be specified as either <code>*argv[]<\/code> or <code>**argv<\/code>.<\/p>\n<p>The following code demonstrates how to use the <em>main()<\/em> function&#8217;s arguments to display the name used to launch the program:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    printf(\"This program is named %s\\n\",argv[0]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<p><code>This program is named .\/a.out<\/code><\/p>\n<p>The name is <code>a.out<\/code> because I compile my examples at the command prompt without specifying the program name output option. Therefore, the default filename, <code>a.out<\/code> is used. The <code>.\/<\/code> directs the shell to run the code from the current directory.<\/p>\n<p>The second nifty thing you should know about the <em>main()<\/em> function is that it has an address, a memory location, just like any other variable or function in your code.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"The main() function is at %p\\n\",&amp;main);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>&amp;<\/code> operator pulls the address of the <em>main()<\/em> function; the parentheses need not be specified. The result is output by the <em>printf()<\/em> function, thanks to the <code>%p<\/code> placeholder. Here&#8217;s a sample run:<\/p>\n<p><code>The main() function is at 0x10b27df40<\/code><\/p>\n<p>I&#8217;m not sure how this information is useful, but you can collect it.<\/p>\n<p>Finally, you can call the <em>main()<\/em> function just like any other function in your code. When you do, however, you must remember that <em>main()<\/em> becomes recursive at this point. To avoid the program continually calling itself, overflowing the stack and crashing, you must add a tripwire to ensure it eventually stops.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    printf(\"Greetings from the main() function!\\n\");\r\n    if(argc&gt;1) return(0);\r\n    main(2,NULL);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this code, the <em>main()<\/em> function is called at Line 7. The arguments 2 and <code>NULL<\/code> are specified. When the function runs again (recursively), the <em>if<\/em> statement at Line 6 tests the argument count and returns without re-calling the <em>main()<\/em> function a third time. Here&#8217;s the output:<\/p>\n<p><code>Greetings from the main() function!<br \/>\nGreetings from the main() function!<\/code><\/p>\n<p>This output assumes that you didn&#8217;t type a command line argument the first time you ran the code. If you do, then the value of <code>argc<\/code> is greater than 1, and the output appears only once.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>main()<\/em> function is required in all C programs, but that doesn&#8217;t mean you can&#8217;t mess with it. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3187\">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-3187","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\/3187","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=3187"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3187\/revisions"}],"predecessor-version":[{"id":3204,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3187\/revisions\/3204"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}