1 00:00:00,000 --> 00:00:00,440 2 00:00:00,440 --> 00:00:00,860 >> SPEAKER 1: All right. 3 00:00:00,860 --> 00:00:03,260 That last program was a little boring. 4 00:00:03,260 --> 00:00:05,510 Let's go ahead and personalize this a little bit. 5 00:00:05,510 --> 00:00:07,710 Rather than to say hello world, why don't we go ahead 6 00:00:07,710 --> 00:00:09,680 and say, hello David? 7 00:00:09,680 --> 00:00:12,500 Well, to do this, I could approach this in a couple of ways. 8 00:00:12,500 --> 00:00:15,760 I could, of course, just hard code David, but that's not all that 9 00:00:15,760 --> 00:00:17,650 fundamentally different from the last version. 10 00:00:17,650 --> 00:00:21,430 So let's take a leap forward, undo that change, and instead insert a new 11 00:00:21,430 --> 00:00:23,980 line of code where line five once was. 12 00:00:23,980 --> 00:00:28,970 >> Specifically, what I'm going to do here is add in string name, thereby 13 00:00:28,970 --> 00:00:30,920 declaring a variable. 14 00:00:30,920 --> 00:00:34,240 Notice that in C, not only do you have to give a variable a name, which in 15 00:00:34,240 --> 00:00:38,110 this case is name, you also have to specify what type it is. 16 00:00:38,110 --> 00:00:41,970 We'll see other types before long, among them integers and floating point 17 00:00:41,970 --> 00:00:46,480 values and more, but for now, string is just a word or a phrase, more 18 00:00:46,480 --> 00:00:49,340 formally, a sequence of zero or more characters. 19 00:00:49,340 --> 00:00:53,760 So at the moment, this line five is saying, give me a variable called name 20 00:00:53,760 --> 00:00:56,560 whose data type is string. 21 00:00:56,560 --> 00:01:00,460 >> Now, I want to store a value in there, and to do that in C, we use the equal 22 00:01:00,460 --> 00:01:03,030 sign, otherwise known as the assignment operator. 23 00:01:03,030 --> 00:01:04,860 So I'm going to proceed as follows. 24 00:01:04,860 --> 00:01:10,220 Equals quote, unquote, D-A-V-I-D, closed quote, semicolon. 25 00:01:10,220 --> 00:01:15,595 The net effect in line five now is to declare a string, D-A-V-I-D, storing 26 00:01:15,595 --> 00:01:19,480 it from the right hand side of this expression into the variable on the 27 00:01:19,480 --> 00:01:21,480 left hand side of this expression. 28 00:01:21,480 --> 00:01:25,970 So by the end of this line, we have D-A-V-I-D stored inside of a variable 29 00:01:25,970 --> 00:01:27,450 called name. 30 00:01:27,450 --> 00:01:28,910 >> Now let's use this variable. 31 00:01:28,910 --> 00:01:35,010 On line six, I'm going to replace world with a placeholder. 32 00:01:35,010 --> 00:01:39,960 %S is defined according to the documentation for printf as being a 33 00:01:39,960 --> 00:01:41,690 placeholder for a string. 34 00:01:41,690 --> 00:01:44,620 In other words, it's a way of informing printf that I'm going to put 35 00:01:44,620 --> 00:01:47,930 some other value here, but let me tell you in just a moment what 36 00:01:47,930 --> 00:01:49,180 it's going to be. 37 00:01:49,180 --> 00:01:52,800 Now, how do I proceed to tell printf what that value is going to be? 38 00:01:52,800 --> 00:01:56,850 >> Well, I need to provide printf with a second argument, and to do that, I 39 00:01:56,850 --> 00:02:00,460 need to put a comma after the first such argument, which was this quoted 40 00:02:00,460 --> 00:02:04,990 string, and then I need to specify what value I want to plug into that 41 00:02:04,990 --> 00:02:07,010 first argument's placeholder. 42 00:02:07,010 --> 00:02:10,880 So the name of my variable is, of course, name, so it suffices to type 43 00:02:10,880 --> 00:02:14,000 "name" as the second argument to printf. 44 00:02:14,000 --> 00:02:15,070 Now don't be misled. 45 00:02:15,070 --> 00:02:20,440 Even though there is a comma inside of the quoted string, that is indeed 46 00:02:20,440 --> 00:02:24,370 inside of the string so it does not separate one argument from another. 47 00:02:24,370 --> 00:02:28,740 Only this comma that's outside of that quoted string actually separates 48 00:02:28,740 --> 00:02:31,700 printf's first argument from its second argument. 49 00:02:31,700 --> 00:02:34,270 >> Let's now compile this program. 50 00:02:34,270 --> 00:02:36,500 Make hello one. 51 00:02:36,500 --> 00:02:39,570 Oh my goodness, five errors generated, and my program's 52 00:02:39,570 --> 00:02:41,100 only seven lines long. 53 00:02:41,100 --> 00:02:44,720 Well, as always, don't look at the last of the errors on your screen. 54 00:02:44,720 --> 00:02:47,960 Look at the very first because perhaps there is a cascading effect whereby 55 00:02:47,960 --> 00:02:51,600 just one error up top created the appearance of more errors than there 56 00:02:51,600 --> 00:02:52,570 actually are. 57 00:02:52,570 --> 00:02:56,620 >> So let me scroll up, and the first such boldfaced error here is use of 58 00:02:56,620 --> 00:02:59,140 undeclared identifier string. 59 00:02:59,140 --> 00:03:00,630 Did you mean standard in? 60 00:03:00,630 --> 00:03:01,990 Well no, I didn't. 61 00:03:01,990 --> 00:03:06,700 I did mean string, but it turns out I forgot that string doesn't exist as a 62 00:03:06,700 --> 00:03:10,830 data type technically in C. Conceptually it exists, but the word 63 00:03:10,830 --> 00:03:16,210 "string" only exists because the CS50 staff have declared it, so to speak, 64 00:03:16,210 --> 00:03:18,160 in a file that we ourselves wrote. 65 00:03:18,160 --> 00:03:22,540 In fact, just as someone long ago declared the printf function in a file 66 00:03:22,540 --> 00:03:28,310 called Standard IO.H, so did we the staff declare string as a data type in 67 00:03:28,310 --> 00:03:32,470 a file that, no surprise, is called CS50.H. 68 00:03:32,470 --> 00:03:36,340 >> So let's go back to the top of my program and inform the compiler that 69 00:03:36,340 --> 00:03:40,130 not only do I want to use the symbol string, I also want to educate the 70 00:03:40,130 --> 00:03:42,160 compiler on what that symbol means. 71 00:03:42,160 --> 00:03:47,010 And to do that quite simply, I can go back up to line one, insert a new line 72 00:03:47,010 --> 00:03:54,070 here, and add, "include CS50.H," also between angled brackets. 73 00:03:54,070 --> 00:03:58,170 This instruction, much like that now on line two, is going to inform the 74 00:03:58,170 --> 00:04:02,460 compiler that it should include the contents of CS50.H wherever they are 75 00:04:02,460 --> 00:04:06,620 in my hard drive inside of my program, thereby educating the compiler as to 76 00:04:06,620 --> 00:04:09,050 what is meant by string. 77 00:04:09,050 --> 00:04:13,420 >> Let's recompile my program after saving the changes. 78 00:04:13,420 --> 00:04:15,500 Make hello one. 79 00:04:15,500 --> 00:04:16,440 Enter. 80 00:04:16,440 --> 00:04:17,730 Much better. 81 00:04:17,730 --> 00:04:23,760 Now I have a blinking prompt at which I can type, "./ hello one," and voila. 82 00:04:23,760 --> 00:04:25,010 Hello David. 83 00:04:25,010 --> 00:04:26,402