1 00:00:00,000 --> 00:00:02,000 [Command-Line Arguments] 2 00:00:02,000 --> 00:00:04,000 [Christopher Bartholomew - Harvard University] 3 00:00:04,000 --> 00:00:07,000 [This is CS50 - CS50.TV] 4 00:00:07,000 --> 00:00:11,000 A useful feature for a program is to accept user input. 5 00:00:11,000 --> 00:00:15,000 Thus far, we've explored some functions within the CS50 library 6 00:00:15,000 --> 00:00:18,000 to accept user input, such as "get string," 7 00:00:18,000 --> 00:00:23,000 which prompts the user, while the application is running, for a string. 8 00:00:23,000 --> 00:00:28,000 >> However, there are cases where you want to provide your program input 9 00:00:28,000 --> 00:00:30,000 before it is actually running. 10 00:00:30,000 --> 00:00:34,000 This way, you don't need to ask additional information from your user 11 00:00:34,000 --> 00:00:38,000 while executing a simple task. 12 00:00:38,000 --> 00:00:42,000 Take, for example, the mv or move command in UNIX. 13 00:00:42,000 --> 00:00:49,000 This command allows the user to move a file from one location to another. 14 00:00:49,000 --> 00:00:55,000 According to the manual pages, mv accepts two command line arguments: 15 00:00:55,000 --> 00:01:00,000 the file that is being moved and the location the file is being moved to. 16 00:01:00,000 --> 00:01:06,000 So this example has a command with two arguments. 17 00:01:06,000 --> 00:01:14,000 So how do we tell our C program to utilize these command-line arguments? 18 00:01:14,000 --> 00:01:20,000 >> Well, it turns out that main, which we use in all C programs, has a secret. 19 00:01:20,000 --> 00:01:26,000 Main accepts two parameters: argc and argv. 20 00:01:26,000 --> 00:01:28,000 Let's go over these terms. 21 00:01:28,000 --> 00:01:33,000 >> The first parameter, argc, which stands for argument count, 22 00:01:33,000 --> 00:01:36,000 has a data type of integer. 23 00:01:36,000 --> 00:01:42,000 The argc parameter contains the number of arguments, including the command. 24 00:01:42,000 --> 00:01:47,000 In our move command, although we only have two arguments displayed, 25 00:01:47,000 --> 00:01:50,000 argc's value will be 3. 26 00:01:50,000 --> 00:01:56,000 The second parameter, argv, which stands for argument vector, 27 00:01:56,000 --> 00:02:01,000 is an array of char pointers that point to strings. 28 00:02:01,000 --> 00:02:06,000 >> This means that each element in argv, starting from zero, 29 00:02:06,000 --> 00:02:09,000 contains the command and arguments. 30 00:02:09,000 --> 00:02:16,000 For example, argv[0], which I'll refer to as argv zero, 31 00:02:16,000 --> 00:02:20,000 will always contain the command that is being run-- 32 00:02:20,000 --> 00:02:22,000 in this case, mv. 33 00:02:22,000 --> 00:02:28,000 argv[1] will contain the first argument, file.txt, 34 00:02:28,000 --> 00:02:37,000 and argv[2] will contain the second argument, ~/cs50/. 35 00:02:37,000 --> 00:02:42,000 The last argument of argv will always be null. 36 00:02:42,000 --> 00:02:46,000 So let's implement these command-line arguments. 37 00:02:46,000 --> 00:02:53,000 In previous exercises, we placed void, meaning nothing, as main's parameter. 38 00:02:53,000 --> 00:02:57,000 However, in order for us to use command-line arguments, 39 00:02:57,000 --> 00:03:12,000 we need to remove void and place inside of main int argc, char* argv[]. 40 00:03:12,000 --> 00:03:17,000 Now, to access the entire element from argv, which are your arguments, 41 00:03:17,000 --> 00:03:21,000 you can simply iterate, or loop, through the array like this. 42 00:03:21,000 --> 00:03:27,000 So, inside of main's body, we're going to go ahead and type a for loop: 43 00:03:27,000 --> 00:03:37,000 for (int i = 0; i < argc; i++). 44 00:03:37,000 --> 00:03:41,000 >> We don't need a curly brace here because we're only executing one line of code 45 00:03:41,000 --> 00:03:44,000 within the body of this loop. 46 00:03:44,000 --> 00:03:47,000 We'll go ahead and hit tab once, 47 00:03:47,000 --> 00:03:57,000 then type printf("argv[%d], to represent an integer value, 48 00:03:57,000 --> 00:04:06,000 is %s, for string, then the new line character. 49 00:04:06,000 --> 00:04:12,000 Then we provide printf i for the current iteration of the loop 50 00:04:12,000 --> 00:04:18,000 and argv[i] for the string representation of the current command-line argument. 51 00:04:18,000 --> 00:04:25,000 When we run it with two arguments, we'll see the arguments being displayed in the terminal. 52 00:04:34,000 --> 00:04:38,000 Earlier we said that the argv held an array of char pointers. 53 00:04:38,000 --> 00:04:45,000 >> So, if this is the case, how do we then access individual characters in each argument? 54 00:04:45,000 --> 00:04:51,000 For example, what if I wanted to look for a specific character in the first argument? 55 00:04:51,000 --> 00:04:55,000 Well, the answer is that we need to apply a nested loop 56 00:04:55,000 --> 00:04:59,000 that will then iterate through each of the elements in the argument string. 57 00:04:59,000 --> 00:05:02,000 This is how you do it. 58 00:05:02,000 --> 00:05:10,000 >> First, we're going to make a copy of example2.c. 59 00:05:10,000 --> 00:05:13,000 Then, inside of the first for loop, 60 00:05:13,000 --> 00:05:15,000 we're going to add an additional for loop. 61 00:05:15,000 --> 00:05:28,000 So for (int j = 0, n = strlen(argv[i]), 62 00:05:28,000 --> 00:05:32,000 which then gives us the length of the current argument, 63 00:05:32,000 --> 00:05:39,000 ; j < n; j++) 64 00:05:39,000 --> 00:05:43,000 We're going to print the location of each character 65 00:05:43,000 --> 00:05:47,000 inside of the current argument by using printf. 66 00:05:47,000 --> 00:05:57,000 So, printf("argv[%d], to represent the index of the current argument, 67 00:05:57,000 --> 00:06:05,000 then [%d] once again, to represent the current character of the current argument, 68 00:06:05,000 --> 00:06:13,000 is: %c, for the current character in the argument. 69 00:06:13,000 --> 00:06:20,000 Lastly, we provide printf with the index of the outer loop, i, 70 00:06:20,000 --> 00:06:22,000 then the index of the inner loop. 71 00:06:22,000 --> 00:06:28,000 >> And our last argument to printf is the actual character from the argument provided 72 00:06:28,000 --> 00:06:31,000 at the command line. 73 00:06:31,000 --> 00:06:37,000 Now, because I used the string function strlen to obtain the length of a string, 74 00:06:37,000 --> 00:06:43,000 I must also add the string.h library to the top of our includes. 75 00:06:43,000 --> 00:06:50,000 So, to do that, we'll go up, and just under stdio.h, we're going to do 76 00:06:50,000 --> 00:06:57,000 #include . 77 00:06:57,000 --> 00:07:02,000 >> So, let's compile and run and provide it an actual argument. 78 00:07:09,000 --> 00:07:18,000 >> And, as we can see, we now have the exact location of each individual char in the argument. 79 00:07:18,000 --> 00:07:23,000 So that's it. I'm Christopher Bartholomew; this is CS50. 80 00:07:23,000 --> 00:07:26,000 [CS50.TV]