``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135``` ```1 00:00:00,000 --> 00:00:00,270 2 00:00:00,270 --> 00:00:02,870 >> DAVID J. MALAN: Suppose now that we want to print all of the command line 3 00:00:02,870 --> 00:00:05,900 arguments that a user types at the prompt and not just the first such 4 00:00:05,900 --> 00:00:08,710 word that he or she types after the program's name. 5 00:00:08,710 --> 00:00:12,480 Well, to do this we simply need a familiar construct, a loop, and A 6 00:00:12,480 --> 00:00:14,070 familiar printf statement. 7 00:00:14,070 --> 00:00:16,750 So let's combine the two-- 8 00:00:16,750 --> 00:00:17,670 for. 9 00:00:17,670 --> 00:00:22,210 >> And now I want to iterate over all of the command line arguments in ARGV. 10 00:00:22,210 --> 00:00:25,300 Now fortunately, I have access to the total number in ARGC. 11 00:00:25,300 --> 00:00:26,830 So let's start there. 12 00:00:26,830 --> 00:00:35,140 >> int i get 0; i is less than argc ; i++. 13 00:00:35,140 --> 00:00:38,170 Now the looping construct I've set up here is simply going to integrate from 14 00:00:38,170 --> 00:00:42,800 zero on up to the total number of arguments in ARGV. 15 00:00:42,800 --> 00:00:45,580 And now we need to something within each iteration of this loop. 16 00:00:45,580 --> 00:00:50,430 Let's, quite simply, print out the i-th such argument in ARGV. 17 00:00:50,430 --> 00:00:57,960 >> Open bracket close bracket printf %s backslash n close quote comma. 18 00:00:57,960 --> 00:00:59,830 And now I need to plug in the value. 19 00:00:59,830 --> 00:01:04,430 So if I want the i-th argument in ARGV, that can be expressed as ARGV 20 00:01:04,430 --> 00:01:08,370 bracket i , close parenthesis, semicolon. 21 00:01:08,370 --> 00:01:11,930 Let's save the file, compile it, and run it. 22 00:01:11,930 --> 00:01:15,980 >> Make ARGV1 dot slash ARGV1. 23 00:01:15,980 --> 00:01:19,150 But before I hit enter, I should probably provide some additional words 24 00:01:19,150 --> 00:01:20,320 at the command prompt. 25 00:01:20,320 --> 00:01:22,710 So I'm going to something like [? foo, ?] 26 00:01:22,710 --> 00:01:24,050 bar, and baz. 27 00:01:24,050 --> 00:01:26,570 And now I'm going to hit Enter. 28 00:01:26,570 --> 00:01:30,340 As expected, I see not only the program's name, which is in ARGV0. 29 00:01:30,340 --> 00:01:32,880 I also see [? foo, ?] bar and baz. 30 00:01:32,880 --> 00:01:34,597 ```