1 00:00:00,000 --> 00:00:00,610 2 00:00:00,610 --> 00:00:02,960 >> DAVID J. MALAN: So I'm still pretty underwhelmed by this program. 3 00:00:02,960 --> 00:00:06,980 All I've done is go from saying hello world to hello David. 4 00:00:06,980 --> 00:00:08,940 But there's still no dynamism in this program. 5 00:00:08,940 --> 00:00:10,530 There's no user input. 6 00:00:10,530 --> 00:00:12,520 There's just hard coded output. 7 00:00:12,520 --> 00:00:13,760 >> Well, we can do better than this. 8 00:00:13,760 --> 00:00:16,750 Let's go back into the inside of my main function. 9 00:00:16,750 --> 00:00:22,740 And this time, rather than hard code David as my name, let me replace this 10 00:00:22,740 --> 00:00:25,470 line with, first, an instruction to the user. 11 00:00:25,470 --> 00:00:29,090 Printf state your name. 12 00:00:29,090 --> 00:00:32,790 So we've now informed the user what we expect of him or her, to state their 13 00:00:32,790 --> 00:00:35,840 name, but we now need to actually get the user's name. 14 00:00:35,840 --> 00:00:40,020 >> Well, as before, I can store the user's name, ultimately, in a variable 15 00:00:40,020 --> 00:00:40,920 of type string. 16 00:00:40,920 --> 00:00:43,420 And I'm going to call it, as before, name. 17 00:00:43,420 --> 00:00:47,850 But I now need to assign, as with the assignment operator, some value to 18 00:00:47,850 --> 00:00:48,760 that variable. 19 00:00:48,760 --> 00:00:51,990 But this time, I don't want to hard code it like I did before with quote, 20 00:00:51,990 --> 00:00:53,150 unquote David. 21 00:00:53,150 --> 00:00:57,260 Rather, I want to dynamically get the users input from their keyboard and 22 00:00:57,260 --> 00:01:00,670 then store that value inside of this variable called name. 23 00:01:00,670 --> 00:01:04,530 >> Well, to do this, I can invoke a function, a function that happens to 24 00:01:04,530 --> 00:01:06,640 be called get string. 25 00:01:06,640 --> 00:01:12,020 But this function, much like string, itself, does not actually come with C. 26 00:01:12,020 --> 00:01:17,380 Rather, the CS50 staff has declared, in a file called CS50.h, a function 27 00:01:17,380 --> 00:01:21,150 called Get String whose purpose in life is to allow the user to type his 28 00:01:21,150 --> 00:01:24,850 or her name at the keyboard, and then this function, get string, returns 29 00:01:24,850 --> 00:01:28,540 that value, so to speak, from the right hand side of this expression 30 00:01:28,540 --> 00:01:31,020 into the left hand side of this expression. 31 00:01:31,020 --> 00:01:34,340 >> Let's save the file and compile this program. 32 00:01:34,340 --> 00:01:37,510 Make hello-2, Enter. 33 00:01:37,510 --> 00:01:38,950 We're back at our blinking prompt. 34 00:01:38,950 --> 00:01:42,340 Now I'm going to go ahead and run ./hello-2. 35 00:01:42,340 --> 00:01:46,500 I'm indeed prompted to state my name, and I'll do just that, D-A-V-I-D. 36 00:01:46,500 --> 00:01:50,230 Enter, and there we have a program that, again, prints hello David. 37 00:01:50,230 --> 00:01:54,250 >> But this time I claim that David is dynamically outputted to the screen, 38 00:01:54,250 --> 00:01:56,430 based on what I, myself, just typed. 39 00:01:56,430 --> 00:02:01,000 So let's test that hypothesis and re-run this program with hello-2. 40 00:02:01,000 --> 00:02:04,030 Let's state my name, this time, as Alice. 41 00:02:04,030 --> 00:02:07,590 And indeed, I'm greeted with hello Alice or any other 42 00:02:07,590 --> 00:02:08,840 name, for that matter. 43 00:02:08,840 --> 00:02:09,760