{"id":630,"date":"2014-04-12T00:01:11","date_gmt":"2014-04-12T07:01:11","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=630"},"modified":"2014-04-28T07:11:43","modified_gmt":"2014-04-28T14:11:43","slug":"reading-command-line-options","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=630","title":{"rendered":"Reading Command Line Options"},"content":{"rendered":"<p>You might think that the command line is a relic of the past. For a mortal user, that&#8217;s correct: The only people I know who still dwell at the command prompt are power users. I keep a command prompt (terminal) window open on my computers, just because using the terminal is fast and I happen to know the commands. Yet, internally, all graphical operating systems still reference the command line.<br \/>\n<!--more--><br \/>\nIn fact, knowing a few command line tricks is one of those treasured &#8220;Windows secrets&#8221; you might read about. That&#8217;s because even Windows programs use command line options and arguments. So if your dream is to become a Windows programmer, you will eventually have to deal with the <em>main()<\/em> function&#8217;s twin arguments, <code>argc<\/code> and <code>*argv[]<\/code>.<\/p>\n<p><code>argc<\/code> is the <em>arg<\/em>ument <em>c<\/em>ount<\/p>\n<p><code>argv<\/code> are the <em>arg<\/em>ument <em>v<\/em>alues, strings<\/p>\n<p>Yes, <code>*argv[]<\/code> can be written as <code>**argv<\/code>. To do so is pure evil.<\/p>\n<p>The <code>**argv<\/code> variable references a string of pointers. Each pointer holds a single argument specified at the command prompt that ran the program. Even Windows programs use those arguments, so this variable isn&#8217;t a quaint antique from the days when computers ran only in text-mode.<\/p>\n<blockquote><p>While you don&#8217;t see a command prompt when a Windows program launches, the <em>system()<\/em> or GUI equivalent command is fully capable of specifying command line arguments.<\/p><\/blockquote>\n<p>The <code>argc<\/code> variable is an <em>int<\/em> value that describes how many <code>**argv<\/code> pointers are available. Even when <code>argc<\/code> is zero, the <code>**argv<\/code> pointer still references a single item: the name of the program or command being run.<\/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    int x;\r\n\r\n    for(x=0;x&lt;argc;x++)\r\n        printf(\"Argument %d: %s\\n\",x,*(argv+x));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>main()<\/em> function declares its arguments at Line 3. A <em>for<\/em> loop at Line 7 inches through each of the arguments; even when <code>argc<\/code> equals zero, the loop spins once, displaying the argument zero, which is the program&#8217;s name.<\/p>\n<p>The <code>*(argv+x)<\/code> construction in Line 8 uses variable <code>x<\/code> to offset each of the pointers referenced by <code>argv<\/code>. Here&#8217;s a sample run:<\/p>\n<p><code><span style=\"color:black\">blog$<\/span> .\/a.out roses are red \"violets are blue\"<br \/>\nArgument 0: .\/a.out<br \/>\nArgument 1: roses<br \/>\nArgument 2: are<br \/>\nArgument 3: red<br \/>\nArgument 4: violets are blue<\/code><\/p>\n<p>Above, the program name is <code>a.out<\/code>. See how the argument enclosed in double quotes is treated as one item? Figure 1 illustrates Code::Block&#8217;s output on a Windows PC:<\/p>\n<div id=\"attachment_640\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0412-figure1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-640\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0412-figure1-300x151.png\" alt=\"Figure 1. Output to a DOS window for Code::Blocks. (Click to embiggen.)\" width=\"300\" height=\"151\" class=\"size-medium wp-image-640\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0412-figure1-300x151.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0412-figure1-500x252.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0412-figure1.png 677w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-640\" class=\"wp-caption-text\">Figure 1. Output to a DOS window for Code::Blocks. (Click to embiggen.)<\/p><\/div>\n<p>In Code::Blocks, you must specify the command line arguments by using the Project&gt;Set programs&#8217; arguments command. Type the arguments into the Select Target dialog box, shown in Figure 2.<\/p>\n<div id=\"attachment_641\" style=\"width: 301px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-641\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0412-figure2.png\" alt=\"Figure 2. Setting command line arguments in Code::Blocks.\" width=\"291\" height=\"450\" class=\"size-full wp-image-641\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0412-figure2.png 291w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/0412-figure2-194x300.png 194w\" sizes=\"auto, (max-width: 291px) 100vw, 291px\" \/><p id=\"caption-attachment-641\" class=\"wp-caption-text\">Figure 2. Setting command line arguments in Code::Blocks.<\/p><\/div>\n<blockquote><p>Yes, I know that the apostrophe after <em>programs<\/em> is improper English. I suppose some programmers have a &#8220;stick the apostrophe anywhere&#8221; attitude about English like other programmers have a &#8220;stick the asterisk anywhere&#8221; attitude about pointers.<\/p><\/blockquote>\n<p>This code could have also been written by using array notation, which &#8212; honestly &#8212; is how most C programmers would write it:<\/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    int x;\r\n\r\n    for(x=0;x&lt;argc;x++)\r\n        printf(\"Argument %d: %s\\n\",x,argv[x]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>You&#8217;ll find one other location where the <code>**<\/code> type of variable is encountered: the computer&#8217;s environment. My Lesson from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=17\">April 2013<\/a> briefly covered how it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <code>**<\/code> type of variable rears its ugly head is when your code deals with command line arguments. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=630\">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-630","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\/630","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=630"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":693,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/630\/revisions\/693"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}