WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:02.480 >> [MUSIC PLAYING] 00:00:06.460 --> 00:00:09.350 >> DOUG LLOYD: All right, so let's talk about command-line arguments. 00:00:09.350 --> 00:00:11.800 So, so far in the course pretty much all of your programs 00:00:11.800 --> 00:00:16.360 have probably started like this-- int main void. 00:00:16.360 --> 00:00:18.310 We've been collecting user input if we need it 00:00:18.310 --> 00:00:21.080 in our programs, such as the Mario program, for example, 00:00:21.080 --> 00:00:22.990 by in-program prompts. 00:00:22.990 --> 00:00:25.190 We haven't needed to modify the declaration of main, 00:00:25.190 --> 00:00:30.684 because instead inside of main we just say, you may call it to get int. 00:00:30.684 --> 00:00:32.350 How large do you want the pyramid to be? 00:00:32.350 --> 00:00:34.455 Or you may call it to get float-- how much change 00:00:34.455 --> 00:00:36.400 should I output to the user? 00:00:36.400 --> 00:00:38.630 >> There is another way though, and if we want our users 00:00:38.630 --> 00:00:40.580 to be able to provide data to our program 00:00:40.580 --> 00:00:43.320 at runtime instead of while the program is running, 00:00:43.320 --> 00:00:45.910 a subtle distinction but sometimes a very useful one, 00:00:45.910 --> 00:00:48.120 we need a new form of declaring main. 00:00:48.120 --> 00:00:53.320 We can't use int main void if we want to collect other data at the command-line 00:00:53.320 --> 00:00:57.540 when the user runs the program, hence command-line arguments. 00:00:57.540 --> 00:00:59.810 >> To collect these command-line arguments from the user, 00:00:59.810 --> 00:01:03.140 change your declaration of main to look like this-- int 00:01:03.140 --> 00:01:10.450 main, open paren, int argc, comma, string argv, square brackets, 00:01:10.450 --> 00:01:12.670 and then open curly brace. 00:01:12.670 --> 00:01:14.415 So what does that mean already? 00:01:14.415 --> 00:01:19.410 Well, we are passing in two parameters, or arguments, or inputs to main. 00:01:19.410 --> 00:01:23.800 One, an integer called argc, and the other is what? 00:01:26.430 --> 00:01:29.640 It's an array of strings, right? 00:01:29.640 --> 00:01:31.140 We see that square bracket notation. 00:01:31.140 --> 00:01:32.181 It's an array of strings. 00:01:32.181 --> 00:01:35.110 It's not an individual string, it's an array of strings. 00:01:35.110 --> 00:01:39.640 And these two arguments, argc and argv, enable you to know what data the user 00:01:39.640 --> 00:01:42.912 has provided at the command-line and how many things they 00:01:42.912 --> 00:01:44.120 provided at the command-line. 00:01:44.120 --> 00:01:46.570 Pretty useful things to work with. 00:01:46.570 --> 00:01:50.310 >> Argc stands for argument count, and you should know, by the way, 00:01:50.310 --> 00:01:52.600 that you could call argc whatever you want it. 00:01:52.600 --> 00:01:54.710 You can call argv whatever you wanted. 00:01:54.710 --> 00:01:58.740 These are just conventional names that we use for them-- argument count, 00:01:58.740 --> 00:02:00.907 and as we'll see in a second, argument vector, argv. 00:02:00.907 --> 00:02:03.698 But you don't have to call them argc and argv if you don't want to, 00:02:03.698 --> 00:02:05.570 but conventionally, that's what we do. 00:02:05.570 --> 00:02:07.500 >> So anyway, argc, the argument count. 00:02:07.500 --> 00:02:11.569 It's an integer-type variable and so, as you might expect, if we have two things 00:02:11.569 --> 00:02:13.860 that we're going to be finding out what these are typed 00:02:13.860 --> 00:02:16.070 and how much stuff the user typed, argc is 00:02:16.070 --> 00:02:18.559 going to tell us how much stuff the user typed. 00:02:18.559 --> 00:02:20.850 So it gives you a number of command-line arguments user 00:02:20.850 --> 00:02:22.470 typed when the program was executed. 00:02:22.470 --> 00:02:25.780 So if your program is run dot slash greedy, 00:02:25.780 --> 00:02:28.670 and inside of your greedy program your main function 00:02:28.670 --> 00:02:34.800 has the declaration int main int argc, string argv square brackets, then 00:02:34.800 --> 00:02:37.950 argc in that case is one. 00:02:37.950 --> 00:02:40.200 Now notice we don't count how many things 00:02:40.200 --> 00:02:42.590 the user typed after the program name. 00:02:42.590 --> 00:02:46.710 The program name itself counts as a command-line argument. 00:02:46.710 --> 00:02:51.770 >> So dot slash greedy, in that case, argc is one. 00:02:51.770 --> 00:02:57.910 If the user typed slash greedy 1024 CS50 at the command-line, argc in that case 00:02:57.910 --> 00:02:59.520 would be three. 00:02:59.520 --> 00:03:03.720 And we know this because the way that the division between the strings 00:03:03.720 --> 00:03:06.030 is detected is whether there is a space, or a tab, 00:03:06.030 --> 00:03:08.230 or something like that between them. 00:03:08.230 --> 00:03:13.860 So any amount of white space, so-called, between the values typed command-line 00:03:13.860 --> 00:03:15.720 indicates how many there are. 00:03:15.720 --> 00:03:24.040 So dot slash greedy space 1024 space CS50, argc, in that case, is three. 00:03:24.040 --> 00:03:26.600 >> Argv is the argument vector. 00:03:26.600 --> 00:03:29.240 Vector, by the way, is just another word for an array, 00:03:29.240 --> 00:03:31.510 and this is an array that stores strings. 00:03:31.510 --> 00:03:35.540 One string per element, which is the strings that the user actually 00:03:35.540 --> 00:03:39.230 typed at the command-line when the program was executed. 00:03:39.230 --> 00:03:40.990 Now, as is the case with any array, if you 00:03:40.990 --> 00:03:44.380 recall from our discussion of arrays, the first element of argv 00:03:44.380 --> 00:03:49.150 is always going to be found at argv square bracket zero. 00:03:49.150 --> 00:03:51.800 That's the first index of the argv array. 00:03:51.800 --> 00:03:55.720 So that will-- and in fact, that will always be the name of the program, 00:03:55.720 --> 00:03:59.730 will always be located at argv square bracket zero. 00:03:59.730 --> 00:04:08.590 >> The last element of argv is always found at argv square brackets argc minus one. 00:04:08.590 --> 00:04:10.300 Do you see why? 00:04:10.300 --> 00:04:14.180 Remember how many elements exist in this array. 00:04:14.180 --> 00:04:16.660 Well, we know that-- it's argc number of elements. 00:04:16.660 --> 00:04:21.279 If the user typed three things at the command-line, argc is three. 00:04:21.279 --> 00:04:27.070 But because in c, when we're working with arrays, each element of the array, 00:04:27.070 --> 00:04:30.190 or rather the indices of the array, start at zero. 00:04:30.190 --> 00:04:34.000 If we have three elements in our array, we have an element at argv zero, 00:04:34.000 --> 00:04:37.930 an element at argv one, and an element at argv two. 00:04:37.930 --> 00:04:41.700 There is no element at argv three, and an array of size three. 00:04:41.700 --> 00:04:43.990 So that's why the last element of argv can always 00:04:43.990 --> 00:04:49.510 be found at argv square brackets argc minus one. 00:04:49.510 --> 00:04:52.420 >> So let's assume the user executes the greedy program as follows-- 00:04:52.420 --> 00:04:57.970 if they type in the command-line dot slash greedy space 1024 space CS50, 00:04:57.970 --> 00:05:00.720 and for whatever reason we've already prepared our greedy program 00:05:00.720 --> 00:05:04.050 to know and work with these command-line arguments. 00:05:04.050 --> 00:05:07.030 We didn't previously when we worked on it for the greedy problem, 00:05:07.030 --> 00:05:09.660 but let's say we've now modified it so that we do process 00:05:09.660 --> 00:05:11.480 the command-line arguments in some way. 00:05:11.480 --> 00:05:15.720 In this case, argv zero is dot slash greedy. 00:05:15.720 --> 00:05:17.042 What's argv one? 00:05:17.042 --> 00:05:19.030 Well, it's 1024, right? 00:05:19.030 --> 00:05:22.620 It is 1024, but here's a really important distinction-- 00:05:22.620 --> 00:05:26.410 do you remember the data type of argv? 00:05:26.410 --> 00:05:31.020 >> It stores strings, right? 00:05:31.020 --> 00:05:34.140 But it looks like 1024 is an integer value. 00:05:34.140 --> 00:05:36.530 This is a really important distinction, and is actually 00:05:36.530 --> 00:05:40.200 going to become something that you might encounter in later problems. 00:05:40.200 --> 00:05:43.770 Everything in argv is stored as a string. 00:05:43.770 --> 00:05:48.775 So argv one's contents are the string one, zero, two, four, 00:05:48.775 --> 00:05:50.275 consisting of those four characters. 00:05:50.275 --> 00:05:53.870 It's as if the user typed one, zero, two, four 00:05:53.870 --> 00:05:56.680 as individual letters or characters. 00:05:56.680 --> 00:06:00.730 It is not the integer 1024, and so you can't directly 00:06:00.730 --> 00:06:11.580 work with it by saying int 1,000, or rather int x equals argv one minus 24. 00:06:11.580 --> 00:06:15.550 >> Intuitively, you might think of that as, OK, well it's 1,024 minus 24, 00:06:15.550 --> 00:06:17.920 so x is equal to 1,000. 00:06:17.920 --> 00:06:22.020 But in fact, that's not the case, because argv one is a string. 00:06:22.020 --> 00:06:23.940 The string 1024. 00:06:23.940 --> 00:06:28.739 Now there is a function that can be used to convert strings to integers. 00:06:28.739 --> 00:06:30.780 I won't spoil it for you now, but I'm sure Zamyla 00:06:30.780 --> 00:06:34.380 will be happy to tell you about it in the walkthrough for a future problem. 00:06:34.380 --> 00:06:36.390 But you can also find problems like-- excuse 00:06:36.390 --> 00:06:39.921 me, functions that would do this in reference 50, 00:06:39.921 --> 00:06:42.462 if you go to the reference guide you can find a function that 00:06:42.462 --> 00:06:43.820 will make this conversion for you. 00:06:43.820 --> 00:06:45.980 But again, in the walkthrough for a future problem, 00:06:45.980 --> 00:06:48.290 Zamyla will be happy to tell you what function 00:06:48.290 --> 00:06:53.280 it is that will convert the string 1024 to the integer 1024. 00:06:53.280 --> 00:06:54.560 >> All right, so moving on. 00:06:54.560 --> 00:06:56.726 We've covered our argv zero, we've covered argv one. 00:06:56.726 --> 00:06:58.000 What's in argv two? 00:06:58.000 --> 00:06:58.620 CS50. 00:06:58.620 --> 00:07:01.350 That one's probably pretty self-explanatory. 00:07:01.350 --> 00:07:02.950 What's in argv three? 00:07:02.950 --> 00:07:04.970 Well again, we don't really know, right? 00:07:04.970 --> 00:07:08.060 We have an array of size three, that's how many elements the user 00:07:08.060 --> 00:07:11.610 typed at the command-line, so if we go to argv three, 00:07:11.610 --> 00:07:14.660 we're now overstepping the bounds of our array. 00:07:14.660 --> 00:07:18.570 The compiler will let us do this, there's no intuitive problem with it, 00:07:18.570 --> 00:07:22.890 but in terms of actually what's going to happen, we don't really know. 00:07:22.890 --> 00:07:25.380 It depends on what is located at the memory where 00:07:25.380 --> 00:07:28.060 argv three would be expected to be. 00:07:28.060 --> 00:07:30.716 And so we could end up getting away scot free. 00:07:30.716 --> 00:07:33.840 More likely than not, particularly when you're working with argv as opposed 00:07:33.840 --> 00:07:35.548 to any other array that's in our program, 00:07:35.548 --> 00:07:38.660 we're probably going to suffer a segmentation fault. So again, 00:07:38.660 --> 00:07:42.756 be sure not to overstep the bounds of your arrays, particularly argv, 00:07:42.756 --> 00:07:47.170 given its high degree of importance in your programs. 00:07:47.170 --> 00:07:48.100 >> I'm Doug Lloyd. 00:07:48.100 --> 00:07:50.170 This is CS50.