1 00:00:00,000 --> 00:00:00,330 2 00:00:00,330 --> 00:00:03,230 >> SPEAKER: Up until now, in programs we've written, we've declared main as 3 00:00:03,230 --> 00:00:04,940 int main void. 4 00:00:04,940 --> 00:00:07,600 And all this time, that void has simply been specifying that the 5 00:00:07,600 --> 00:00:10,870 program does not take any command line arguments. 6 00:00:10,870 --> 00:00:14,180 In other words, when a user runs a program, he or she can provide command 7 00:00:14,180 --> 00:00:18,130 line arguments by writing additional words or phrases after the program's 8 00:00:18,130 --> 00:00:19,420 name at the prompt. 9 00:00:19,420 --> 00:00:22,980 >> Well, if you do want your program to take command line arguments, one or 10 00:00:22,980 --> 00:00:26,960 more such words, we need to replace void with a couple of arguments. 11 00:00:26,960 --> 00:00:28,630 So let's do that. 12 00:00:28,630 --> 00:00:32,170 Include CS50.h. 13 00:00:32,170 --> 00:00:35,760 Include standard io.h. 14 00:00:35,760 --> 00:00:40,830 Int, main, and now instead of void, I'm going to specify an int called 15 00:00:40,830 --> 00:00:45,800 argc, and an array of strings called argv. 16 00:00:45,800 --> 00:00:48,240 >> Now, argc and argv are simply conventions. 17 00:00:48,240 --> 00:00:50,530 We could've call these arguments most anything we want. 18 00:00:50,530 --> 00:00:54,050 But what is important is that argc is an int, because by definition, it is 19 00:00:54,050 --> 00:00:57,810 going to contain the argument counts, the number of words in total that the 20 00:00:57,810 --> 00:00:59,830 user has typed at his or her prompt. 21 00:00:59,830 --> 00:01:04,310 >> Argv, meanwhile, argument vector, is going to actually be an array storing 22 00:01:04,310 --> 00:01:08,460 all of the words that the user has typed at his or her prompt. 23 00:01:08,460 --> 00:01:10,920 Let's proceed to do something now with one or more of these 24 00:01:10,920 --> 00:01:12,090 command line arguments. 25 00:01:12,090 --> 00:01:16,440 In particular, let's go ahead and print whatever word the user types 26 00:01:16,440 --> 00:01:20,380 after the program's name at the prompt. 27 00:01:20,380 --> 00:01:26,940 >> Open bracket, closed bracket, print f, percent s, backslash n, comma. 28 00:01:26,940 --> 00:01:30,930 And now I need to tell print f what value to plug into that placeholder. 29 00:01:30,930 --> 00:01:35,030 I want the first word that the user has typed after the program's name. 30 00:01:35,030 --> 00:01:39,550 And so I'm going to specify argv bracket 1, closed 31 00:01:39,550 --> 00:01:41,250 parenthesis, semicolon. 32 00:01:41,250 --> 00:01:43,710 >> Now, why bracket 1 and not bracket 0? 33 00:01:43,710 --> 00:01:47,410 Well, it turns out, automatically stored in argv 0 is going to be the 34 00:01:47,410 --> 00:01:49,040 program's actual name. 35 00:01:49,040 --> 00:01:52,600 So the first word that the user types after the program's name is, by 36 00:01:52,600 --> 00:01:55,410 convention, going to be stored in argv 1. 37 00:01:55,410 --> 00:01:58,800 >> Let's now compile and run this program. 38 00:01:58,800 --> 00:02:08,080 Make argv 0 dot slash argv 0, and now a word like hello, enter. 39 00:02:08,080 --> 00:02:09,330 And there we have it, hello. 40 00:02:09,330 --> 00:02:11,332