1 00:00:00,506 --> 00:00:09,416 [ Silence ] 2 00:00:09,916 --> 00:00:14,526 >> This is CS50, Week 1, thought we'd begin 3 00:00:14,876 --> 00:00:17,056 with a cookie love story. 4 00:00:22,516 --> 00:01:35,956 [ Music ] 5 00:01:36,456 --> 00:01:38,726 >> So this was a Scratch project submitted by one 6 00:01:38,726 --> 00:01:40,356 of last year's students. 7 00:01:40,356 --> 00:01:42,996 And what's nice is because not only is it rather cute, 8 00:01:42,996 --> 00:01:44,996 it also draws in all of these fundamentals 9 00:01:44,996 --> 00:01:46,666 that we started talking about last week. 10 00:01:46,666 --> 00:01:48,596 And if you haven't yourself dived 11 00:01:48,596 --> 00:01:52,516 into your own problem set zero, whose PDFs are available online, 12 00:01:52,796 --> 00:01:56,496 realize that if something like this, given that it's a minute 13 00:01:56,496 --> 00:01:59,476 or so long, kind of looks you know, despite the cutesiness 14 00:01:59,476 --> 00:02:01,796 of it, kind of overwhelming in that you don't really know 15 00:02:01,796 --> 00:02:03,826 where to begin, how could you possibly sketch 16 00:02:03,826 --> 00:02:04,956 out this whole story line. 17 00:02:05,206 --> 00:02:07,756 Well, realize one of the themes in this course and really one 18 00:02:07,756 --> 00:02:09,956 of the themes in programming and solving any problem 19 00:02:09,956 --> 00:02:11,766 with a machine is going to be really 20 00:02:11,766 --> 00:02:14,806 to just take small bites one at a time out of these problems 21 00:02:15,026 --> 00:02:17,066 until the end result ultimately is 22 00:02:17,066 --> 00:02:19,726 that you've actually bitten off a fairly large fairly 23 00:02:19,726 --> 00:02:21,586 interesting problem and in the aggregate, 24 00:02:21,906 --> 00:02:24,556 you've actually implemented something pretty sophisticated. 25 00:02:24,556 --> 00:02:27,566 But if you set out trying to implement this whole story line 26 00:02:27,566 --> 00:02:28,816 at once, if you set out trying 27 00:02:28,816 --> 00:02:32,086 to implement a cookie love story, it's just not gonna fly. 28 00:02:32,086 --> 00:02:34,606 You're gonna run into innumerable bugs most likely, 29 00:02:34,606 --> 00:02:35,956 by bugs we mean mistakes, 30 00:02:36,196 --> 00:02:38,786 behaviors that you didn't quite intend and yet they seem to be-- 31 00:02:39,126 --> 00:02:43,006 and yet your program seems to be misbehaving in some sense. 32 00:02:43,246 --> 00:02:46,256 And so with this example here, I thought I'd propose 33 00:02:46,256 --> 00:02:47,766 that we consider exactly how you'd go 34 00:02:47,766 --> 00:02:49,486 about tackling something like this. 35 00:02:49,486 --> 00:02:52,196 Let me restart it but in the smaller scope of this window. 36 00:02:52,516 --> 00:02:54,286 And there's a few things going on here, 37 00:02:54,286 --> 00:02:57,376 so clearly there is multiple sprite, multiple characters, 38 00:02:57,596 --> 00:03:00,086 and recall from last week that each sprite has its own set 39 00:03:00,086 --> 00:03:01,816 of strips or programs. 40 00:03:02,116 --> 00:03:04,126 The scripts are in the middle, the sprites are 41 00:03:04,126 --> 00:03:07,126 on the right hand side, so when I have the stage selected 42 00:03:07,126 --> 00:03:10,296 at the moment, I'm seeing the script that are associated 43 00:03:10,446 --> 00:03:13,116 with sort of the whole program itself, the so called stage. 44 00:03:13,326 --> 00:03:15,406 But I could home in on sprite 1 there 45 00:03:15,636 --> 00:03:18,046 and just see this particular sprite script. 46 00:03:18,356 --> 00:03:19,566 So now let me actually reverse this 47 00:03:19,566 --> 00:03:20,606 and go back to the beginning. 48 00:03:21,136 --> 00:03:22,696 So it's a lot just happened here. 49 00:03:23,266 --> 00:03:23,896 But how do you go 50 00:03:23,896 --> 00:03:25,466 about implementing something like this. 51 00:03:25,466 --> 00:03:26,626 Well, honestly start easy. 52 00:03:26,626 --> 00:03:28,856 The first thing you probably want to accomplish 53 00:03:28,856 --> 00:03:31,766 or at least one thing that seems easy is find the graphic 54 00:03:31,766 --> 00:03:34,256 of the cookie sheet that you happen to want for this project. 55 00:03:34,466 --> 00:03:36,746 Lay it down and make sure it's in the right position, right? 56 00:03:36,936 --> 00:03:38,656 Check. So, we're making progress already. 57 00:03:38,656 --> 00:03:39,506 It doesn't do anything 58 00:03:39,506 --> 00:03:41,546 but at least that's one bite out of this problem. 59 00:03:41,836 --> 00:03:43,946 Now, focus on just one of these sprites. 60 00:03:43,946 --> 00:03:46,966 For instance the cookie, the gingerbread man, 61 00:03:46,966 --> 00:03:48,516 the gingerbread woman or whichever one 62 00:03:48,516 --> 00:03:52,236 of the cookies you want to introduce first and focus just 63 00:03:52,236 --> 00:03:53,896 on getting that cookie to dance. 64 00:03:54,206 --> 00:03:55,546 And what do we mean by dance? 65 00:03:55,546 --> 00:03:57,446 Figure out how you can make it move a little to the left, 66 00:03:57,856 --> 00:04:00,516 a little to the right, up and down and then repeat 67 00:04:00,816 --> 00:04:02,396 in some kind of looping structure. 68 00:04:02,396 --> 00:04:04,166 And then after a few minutes or maybe an hour, 69 00:04:04,376 --> 00:04:06,726 you hopefully have at least one dancing cookie. 70 00:04:06,726 --> 00:04:09,096 Maybe there is no music, so it's just kind of dancing 71 00:04:09,096 --> 00:04:11,306 in the middle of a cookie sheet all by himself. 72 00:04:11,306 --> 00:04:12,696 But now at least you have a template 73 00:04:12,946 --> 00:04:16,866 that you can base cookies number 2 and 3 and 4 and 5. 74 00:04:17,066 --> 00:04:18,536 And if you haven't realized already, 75 00:04:18,536 --> 00:04:21,066 do realize per the tutorials that we've linked to online, 76 00:04:21,326 --> 00:04:23,376 you can do things like duplicating sprites, 77 00:04:23,376 --> 00:04:24,966 you don't have to re-implement the whole thing. 78 00:04:25,336 --> 00:04:26,556 And so you can really-- 79 00:04:26,766 --> 00:04:29,086 the process starts to pick up traction pretty quickly. 80 00:04:29,336 --> 00:04:31,776 Now on the next screen, once this thing resumes, 81 00:04:32,606 --> 00:04:34,846 realize that-- come on. 82 00:04:34,846 --> 00:04:36,686 [ Background Music ] 83 00:04:36,686 --> 00:04:38,266 >> Once we changes phases, 84 00:04:38,306 --> 00:04:40,186 realize that a whole lot more was happening 85 00:04:40,186 --> 00:04:41,066 on the next screen. 86 00:04:41,066 --> 00:04:43,776 In just a moment the hearts are gonna start flowing from bottom 87 00:04:43,776 --> 00:04:46,546 to top and the cookies are gonna start moving left and right 88 00:04:46,546 --> 00:04:48,716 and they seem to be somewhat synchronized with the music, 89 00:04:48,766 --> 00:04:51,116 perhaps based on trial and error by the student 90 00:04:51,116 --> 00:04:52,166 who implemented this one. 91 00:04:52,346 --> 00:04:55,696 Well, how do you get this hearts to move up and own and seemingly 92 00:04:55,696 --> 00:04:57,426 in these random patterns? 93 00:04:57,626 --> 00:05:00,126 Well, recall that very basic building block we introduced 94 00:05:00,126 --> 00:05:03,636 last week which allowed you to grab a random number between 1 95 00:05:03,846 --> 00:05:05,806 and 10 or 1 or a hundred. 96 00:05:05,936 --> 00:05:08,036 And if you haven't realized already, the whole point 97 00:05:08,036 --> 00:05:11,326 of this stage being a rectangle is that ask him to buy the X 98 00:05:11,326 --> 00:05:14,366 and Y coordinates there, it's essentially a grid of dots, 99 00:05:14,366 --> 00:05:17,236 of so-called pixels that go up, down, left, right, 100 00:05:17,236 --> 00:05:19,026 and each of them is a specific address, 101 00:05:19,196 --> 00:05:22,476 X comma Y. So if you wanna pick a random location for one 102 00:05:22,476 --> 00:05:24,436 of those hearts so it starts from a different place, 103 00:05:24,646 --> 00:05:28,756 we'll pick a random X of random number between 1 and 200 or 300, 104 00:05:28,756 --> 00:05:32,016 or however wide the stage is, and then use the move block 105 00:05:32,206 --> 00:05:35,016 from the appropriate palette to put that's right there, 106 00:05:35,016 --> 00:05:36,626 and then start its movement. 107 00:05:36,756 --> 00:05:39,126 In other words, when you bite off your own project 108 00:05:39,126 --> 00:05:42,586 or when you start off by looking at someone else's projects, 109 00:05:42,586 --> 00:05:44,306 either that we looked at last week or some 110 00:05:44,306 --> 00:05:46,326 of the innumerable links we included in the specs, 111 00:05:46,746 --> 00:05:49,706 focus on just one piece of it, get that working, 112 00:05:49,936 --> 00:05:51,506 and then move on to the next. 113 00:05:51,566 --> 00:05:53,966 And this really is going to be a recurring theme throughout 114 00:05:53,966 --> 00:05:54,346 the course. 115 00:05:54,346 --> 00:05:57,676 You can very quickly drive yourself nuts by sitting down, 116 00:05:57,916 --> 00:06:00,136 thinking, oh, I can write this program, no sweat, 117 00:06:00,136 --> 00:06:02,836 and then you write it and then there's a tiny little mistake 118 00:06:02,906 --> 00:06:05,556 here, but then there's a tiny little mistake here and here, 119 00:06:05,556 --> 00:06:07,566 and all of these stupid little things cascade 120 00:06:07,726 --> 00:06:09,896 until you try running your program, 121 00:06:10,136 --> 00:06:11,386 and the thing just doesn't work. 122 00:06:11,386 --> 00:06:12,746 And then it's the situation 123 00:06:12,746 --> 00:06:14,136 where you just don't know where to begin. 124 00:06:14,356 --> 00:06:17,106 So honestly, one of the best strategies in this course 125 00:06:17,106 --> 00:06:21,096 and in programming in general is-- generally speaking design, 126 00:06:21,096 --> 00:06:23,776 but it's these little tricks of the trade 127 00:06:23,996 --> 00:06:27,686 that will honestly make things much more fun, much more easier, 128 00:06:27,686 --> 00:06:30,196 and really allow you to focus on the juicy parts of the problem 129 00:06:30,196 --> 00:06:32,136 and not little tiny bugs 130 00:06:32,136 --> 00:06:33,896 that you may have started tripping over already. 131 00:06:33,896 --> 00:06:35,746 So hopefully, approach if you haven't already, 132 00:06:35,746 --> 00:06:37,216 problem set zero with that mindset. 133 00:06:37,436 --> 00:06:39,156 Take quite literally baby steps. 134 00:06:39,156 --> 00:06:41,916 And from each of these individual parts, well, 135 00:06:41,916 --> 00:06:45,306 you very likely come out with a very impressive whole. 136 00:06:45,456 --> 00:06:49,686 So, with that said, we like to Google ourselves sometimes 137 00:06:49,926 --> 00:06:53,856 and so we discovered that FlyBy last night mentioned CS50 among 138 00:06:53,856 --> 00:06:56,886 some other courses in its last minute shopping list. 139 00:06:56,886 --> 00:06:59,246 So this is to say, for those of you who are joining us today 140 00:06:59,246 --> 00:07:01,146 for the first time, that's totally fine. 141 00:07:01,376 --> 00:07:02,586 Nothing has been due yet. 142 00:07:02,586 --> 00:07:05,016 That's not till Friday, and last week's two lectures are both 143 00:07:05,016 --> 00:07:06,846 available online at cs50.net. 144 00:07:07,186 --> 00:07:08,126 I didn't quite know how 145 00:07:08,126 --> 00:07:11,286 to interpret the tag line that was up here. 146 00:07:11,526 --> 00:07:13,966 If you zoom in up top, which I can't quite do 147 00:07:13,966 --> 00:07:15,676 with a keynote here, it says, 148 00:07:15,756 --> 00:07:17,946 "This last minute shopping, bargain hunting." 149 00:07:18,496 --> 00:07:20,666 So, I'm not quite sure what the takeaway there is 150 00:07:20,666 --> 00:07:23,676 but realize you are perfectly welcome to shop 151 00:07:23,676 --> 00:07:24,956 and then still take the course. 152 00:07:25,176 --> 00:07:26,926 If you decide to opt in by a pass fail, 153 00:07:26,926 --> 00:07:28,566 I just need to sign your white study card 154 00:07:28,566 --> 00:07:30,266 or if you wanna decide before the 5th Monday, 155 00:07:30,486 --> 00:07:32,666 just grab the pink slip and I can sign that and you can turn 156 00:07:32,666 --> 00:07:36,856 that in at any point and I'm happy to chat during break today 157 00:07:36,856 --> 00:07:39,476 or after class if you have any questions or concerns. 158 00:07:39,746 --> 00:07:41,886 So, where is the support structure thus far? 159 00:07:41,886 --> 00:07:42,846 We haven't sectioned yet. 160 00:07:42,846 --> 00:07:44,456 That will officially start this Friday. 161 00:07:44,456 --> 00:07:47,226 So more details on sectioning on Friday, but do realize 162 00:07:47,226 --> 00:07:49,416 that office hours are very much in progress. 163 00:07:49,416 --> 00:07:53,256 Our 60 plus person staff has been hanging out in a couple 164 00:07:53,256 --> 00:07:54,576 of dining halls on campus, 165 00:07:54,576 --> 00:07:56,536 namely the Cabot and Quincy House. 166 00:07:56,606 --> 00:07:59,886 Moving forward, we'll actually move some place other 167 00:07:59,886 --> 00:08:01,756 than the dining halls but we thought this is a nice way 168 00:08:01,756 --> 00:08:04,996 to start the term in a more casual communal environment. 169 00:08:05,236 --> 00:08:07,836 But just go to cs50.net, click office hours 170 00:08:07,836 --> 00:08:09,466 and you'll see the Google Calendar with all 171 00:08:09,466 --> 00:08:11,506 of the office hours that are still happening tonight, 172 00:08:11,856 --> 00:08:14,426 as well as tomorrow and these are just an opportunity to chat, 173 00:08:14,816 --> 00:08:17,366 to ask questions one on one with the teaching fellows 174 00:08:17,366 --> 00:08:19,766 or course assistants or frankly just a way to hang 175 00:08:19,766 --> 00:08:21,596 out during brain break and work on projects 176 00:08:21,876 --> 00:08:23,756 and then see what some of your friends might be 177 00:08:23,756 --> 00:08:24,906 tackling themselves. 178 00:08:25,216 --> 00:08:29,146 As for sections, there will be what we call super sections this 179 00:08:29,146 --> 00:08:30,806 coming Sunday, Monday, Tuesday. 180 00:08:30,916 --> 00:08:33,996 CS50 sections are standardized on Sundays, Mondays and Tuesdays 181 00:08:34,246 --> 00:08:37,126 but we won't officially assign you to sections for a week or so 182 00:08:37,476 --> 00:08:40,746 but unofficially, well, there'll be open sections this coming 183 00:08:40,746 --> 00:08:41,996 Sunday, Monday, Tuesday. 184 00:08:42,106 --> 00:08:44,916 The schedule is also on the course's website under sections. 185 00:08:45,226 --> 00:08:46,836 And these are super sections in just 186 00:08:46,836 --> 00:08:48,196 that they are superset of the class. 187 00:08:48,196 --> 00:08:50,496 You can go to any one of these times more comfortable, 188 00:08:50,496 --> 00:08:52,906 less comfortable, somewhere in between, and the focus 189 00:08:52,906 --> 00:08:55,956 of these sections will be on the material cover this week as well 190 00:08:55,956 --> 00:08:59,026 as potentially the start of next week and with a focus 191 00:08:59,026 --> 00:09:01,896 on problem set 1, which is the second problem set, 192 00:09:01,966 --> 00:09:05,106 0 index which will go out via PDF this weekend. 193 00:09:05,106 --> 00:09:08,136 So realize the support structure is now in place and in progress 194 00:09:08,176 --> 00:09:11,346 so do just let us know at help@cs50.net 195 00:09:11,346 --> 00:09:13,386 if you have questions that can't be addressed during 196 00:09:13,546 --> 00:09:14,156 office hours. 197 00:09:14,596 --> 00:09:17,726 So, we looked last week at Scratch, very briefly, 198 00:09:17,726 --> 00:09:19,736 but hopefully you found if you've dived in already 199 00:09:19,736 --> 00:09:21,776 that the interface is fairly intuitive 200 00:09:21,926 --> 00:09:24,076 and it really just allows you to implement ideas 201 00:09:24,076 --> 00:09:25,596 that you conjure up in your mind. 202 00:09:25,776 --> 00:09:26,946 Well today we're gonna transition 203 00:09:26,946 --> 00:09:29,446 to a more traditional programming language, 204 00:09:29,446 --> 00:09:32,046 a lower level programming language if you will 205 00:09:32,376 --> 00:09:34,806 where the ideas are pretty much identical 206 00:09:34,806 --> 00:09:36,956 to what we've seen last week in what you might have seen 207 00:09:36,956 --> 00:09:39,356 in prior courses, but what I thought I'd do is try 208 00:09:39,356 --> 00:09:44,106 to set your minds on the equivalence 209 00:09:44,496 --> 00:09:46,216 of the ideas we talked about last week, 210 00:09:46,266 --> 00:09:47,636 the ideas we'll talk about this week. 211 00:09:47,636 --> 00:09:50,216 And even though the syntax, the characters you are about to see 212 00:09:50,216 --> 00:09:53,336 on the screen and in programs today onward is a little more 213 00:09:53,336 --> 00:09:56,356 arcane, it's a little uglier looking, you've got semicolons 214 00:09:56,356 --> 00:09:58,926 and parentheses and fairly arcane syntax, 215 00:09:59,166 --> 00:10:00,096 realize that at the end 216 00:10:00,096 --> 00:10:02,626 of the day these are just arbitrary human conventions, 217 00:10:02,726 --> 00:10:04,976 the ideas are identical to this thing here. 218 00:10:05,046 --> 00:10:08,146 >> So this thing here, when green flag clicked say, oh, 219 00:10:08,146 --> 00:10:11,436 hi world, this was perhaps the simplest program we could write 220 00:10:11,696 --> 00:10:12,146 in Scratch. 221 00:10:12,356 --> 00:10:15,016 It's sort of the canonical hello world program 222 00:10:15,256 --> 00:10:17,676 as computer scientists call this very simple example. 223 00:10:17,836 --> 00:10:20,406 Well, starting today in a language called C, 224 00:10:20,866 --> 00:10:22,886 this is going to look like this. 225 00:10:23,066 --> 00:10:25,676 So again immediately on first glance, a little more arcane, 226 00:10:25,676 --> 00:10:28,146 there are some distracting details, these curly braces, 227 00:10:28,146 --> 00:10:29,716 the semicolon, the quotation marks. 228 00:10:29,996 --> 00:10:33,116 But if you avoid all of these minutia and focus really just 229 00:10:33,116 --> 00:10:37,616 on the idea, it looks like what was a blue say block, 230 00:10:37,706 --> 00:10:39,646 a so called statement, is now going 231 00:10:39,646 --> 00:10:42,196 to be something called printf where the F happens 232 00:10:42,236 --> 00:10:43,416 to stand for formatted. 233 00:10:43,686 --> 00:10:46,776 But the string, the sentence or the words that we're displaying 234 00:10:46,776 --> 00:10:48,466 on the screen, previously you clicked 235 00:10:48,466 --> 00:10:50,576 on a little white text field and typed it in, 236 00:10:50,766 --> 00:10:52,676 well now you're gonna do the same thing at the keyboard 237 00:10:52,676 --> 00:10:53,846 but you're gonna have to sandwich it 238 00:10:53,846 --> 00:10:55,856 in between double quotation marks. 239 00:10:55,856 --> 00:10:57,486 So it's these little differences 240 00:10:57,486 --> 00:10:59,886 that you'll very quickly acclimate to 241 00:11:00,066 --> 00:11:02,156 but it's these stupid little syntactic details 242 00:11:02,156 --> 00:11:04,456 that trips off, especially those less comfortable early 243 00:11:04,456 --> 00:11:07,586 on because if you leave out a single character, 244 00:11:07,586 --> 00:11:08,786 namely that semicolon 245 00:11:08,786 --> 00:11:10,866 on the right hand side, nothing will work. 246 00:11:11,096 --> 00:11:14,436 Which is an unfortunate design decision made many years ago 247 00:11:14,736 --> 00:11:15,966 with this particular language. 248 00:11:15,966 --> 00:11:18,066 More modern languages as we'll see toward the tail end 249 00:11:18,066 --> 00:11:19,866 of the semester are a little more generous 250 00:11:19,866 --> 00:11:20,976 with some of these details. 251 00:11:21,206 --> 00:11:23,686 Well, this statement here say "oh hi world" is going 252 00:11:23,686 --> 00:11:26,846 to map very specifically to this line here. 253 00:11:26,846 --> 00:11:28,746 What about some of the other constructs we looked at? 254 00:11:28,746 --> 00:11:30,666 Well loops are something we talked briefly about. 255 00:11:30,866 --> 00:11:32,266 There is something you perhaps employed. 256 00:11:32,266 --> 00:11:35,036 Certainly in the cookie love story was there a looping 257 00:11:35,036 --> 00:11:35,566 going on. 258 00:11:35,566 --> 00:11:37,796 The cookies were going back and forth and back and forth 259 00:11:37,796 --> 00:11:40,396 and the hearts up and down, well, this thing that we had 260 00:11:40,396 --> 00:11:42,806 that called a loop last week with a forever block 261 00:11:42,996 --> 00:11:45,586 with a statement inside of it is simply gonna look a little 262 00:11:45,636 --> 00:11:46,606 something like this. 263 00:11:47,226 --> 00:11:51,336 So while. And thankfully the jargon that the authors 264 00:11:51,336 --> 00:11:54,596 of this language chose early on conjures up the right ideas 265 00:11:54,596 --> 00:11:57,556 in your mind while something is true. 266 00:11:57,896 --> 00:12:00,076 So here I've hardcoded the word true 267 00:12:00,426 --> 00:12:03,206 which is actually deliberately inducing what we called what 268 00:12:03,206 --> 00:12:03,706 last week. 269 00:12:05,146 --> 00:12:08,856 So a loop or an infinite loop, because true is always true, 270 00:12:08,856 --> 00:12:10,816 there is no-- nothing on the screen that seems 271 00:12:10,816 --> 00:12:12,366 to change this keyword true, 272 00:12:12,616 --> 00:12:14,766 so this is actually a deliberate infinite loop, 273 00:12:15,026 --> 00:12:17,916 but so was that thing up top to forever say hi 274 00:12:17,916 --> 00:12:19,406 or whatever it is you wanted to do. 275 00:12:19,686 --> 00:12:22,176 So the translation here is that whereas last week 276 00:12:22,176 --> 00:12:25,086 and in Scratch, the looping structures are yellow 277 00:12:25,086 --> 00:12:27,006 and have kind of this brace structure to them. 278 00:12:27,186 --> 00:12:30,016 Well in C and many similar languages, you have to implement 279 00:12:30,016 --> 00:12:31,516 that same bracing structure 280 00:12:31,516 --> 00:12:33,746 with your keyboard using an open curly brace 281 00:12:34,056 --> 00:12:36,556 and a closed curly brace, so a tiny little detail. 282 00:12:36,826 --> 00:12:38,456 On last week too we saw-- 283 00:12:39,026 --> 00:12:40,516 and in your projects you might have seen a so-called 284 00:12:40,516 --> 00:12:41,206 repeat loop. 285 00:12:41,516 --> 00:12:42,186 Same idea. 286 00:12:42,436 --> 00:12:44,886 But whereas the other one was obviously forever, 287 00:12:45,106 --> 00:12:47,896 this allows you to specify a finite number of times 288 00:12:47,896 --> 00:12:48,906 that you wanna do something. 289 00:12:48,906 --> 00:12:50,636 Here I arbitrarily chose 10. 290 00:12:50,916 --> 00:12:52,546 How can you implement that same idea? 291 00:12:52,796 --> 00:12:55,486 Well in C and other languages that we'll see this semester, 292 00:12:55,706 --> 00:12:57,146 you use what's called the for loop, 293 00:12:57,396 --> 00:12:59,596 and this as syntax isn't quite as straightforward 294 00:12:59,596 --> 00:13:00,916 but once you know what to look for, 295 00:13:00,916 --> 00:13:03,066 it's very easy to understand. 296 00:13:03,066 --> 00:13:05,916 So for, so for the following definition. 297 00:13:06,176 --> 00:13:08,416 Now you have a set of parentheses as we'll see 298 00:13:08,416 --> 00:13:12,306 in actual code and then I have int I equal 0. 299 00:13:12,566 --> 00:13:15,236 So int is gonna stand for integer, it's just a number. 300 00:13:15,236 --> 00:13:17,906 I is the generic variable that you use 301 00:13:17,906 --> 00:13:19,366 when you really just wanna count something 302 00:13:19,366 --> 00:13:21,026 and don't really care what its name is. 303 00:13:21,356 --> 00:13:23,546 The semicolon just separates this from the rest. 304 00:13:23,846 --> 00:13:27,186 The I less than 10 says do the following 305 00:13:27,186 --> 00:13:28,506 as long as I is less than 10. 306 00:13:28,506 --> 00:13:31,916 And then on the right hand side I++, we saw that in pseudocode 307 00:13:31,916 --> 00:13:35,026 with our socks example last week that just says increment I. 308 00:13:35,286 --> 00:13:38,036 So, long story short after this fairly arcane syntax, 309 00:13:38,036 --> 00:13:40,426 the end result is completely straightforward, 310 00:13:40,566 --> 00:13:42,256 do the following 10 times. 311 00:13:42,546 --> 00:13:44,396 But because we're now programming a computer 312 00:13:44,396 --> 00:13:47,046 at a lower level and because as we said last week, 313 00:13:47,276 --> 00:13:49,976 you really have to be precise and then careful 314 00:13:49,976 --> 00:13:52,856 to instruct this machine, this fairly dumb machine 315 00:13:52,856 --> 00:13:54,146 that will only do what you tell it 316 00:13:54,146 --> 00:13:56,286 to do very specific instructions, 317 00:13:56,556 --> 00:13:58,636 do we have to use a more precise syntax 318 00:13:58,856 --> 00:14:02,016 than just a puzzle piece might have previously allowed. 319 00:14:02,186 --> 00:14:03,326 Well what about variables. 320 00:14:03,326 --> 00:14:05,186 Well, this block here on the left that I came 321 00:14:05,186 --> 00:14:08,106 up with arbitrarily sets a variable called counter equal 322 00:14:08,106 --> 00:14:11,556 to 0 and then it forever says that counters value 323 00:14:11,776 --> 00:14:13,506 and then it changes the counter by 1. 324 00:14:13,686 --> 00:14:15,096 So this is like counter ++. 325 00:14:15,096 --> 00:14:17,516 Well, what is code like that from the world 326 00:14:17,516 --> 00:14:18,586 of Scratch gonna look like? 327 00:14:18,806 --> 00:14:20,756 Well, you can implement this in a few different ways 328 00:14:20,756 --> 00:14:22,506 but here's one approach that uses some 329 00:14:22,506 --> 00:14:26,426 of the syntax we just looked at, int counter equals 0. 330 00:14:26,646 --> 00:14:28,856 So declare as we'll say a variable, 331 00:14:28,856 --> 00:14:30,066 a placeholder called counter. 332 00:14:30,306 --> 00:14:32,726 It's gonna hold integers so I have to say int as we'll see 333 00:14:32,926 --> 00:14:34,106 and set it equal to 0. 334 00:14:34,316 --> 00:14:36,336 Then we have to steal the code from earlier 335 00:14:36,336 --> 00:14:38,886 to do a forever loop, a while true loop 336 00:14:38,886 --> 00:14:40,316 and then I'm gonna go ahead and use 337 00:14:40,316 --> 00:14:41,726 that printf thing that we saw. 338 00:14:41,726 --> 00:14:43,446 So everything is kind of coming together now 339 00:14:43,816 --> 00:14:45,256 with then the counter ++. 340 00:14:45,256 --> 00:14:47,716 So again, idea on the left, fairly straightforward, 341 00:14:47,826 --> 00:14:50,896 idea on the right, same but just looks a little different. 342 00:14:51,146 --> 00:14:53,286 And lastly with these things called Boolean expressions 343 00:14:53,316 --> 00:14:56,126 that allow you to check if something is true or false, 344 00:14:56,336 --> 00:14:59,036 well, we've seen this in our loops just a moment ago 345 00:14:59,306 --> 00:15:01,096 but in Scratch we might have asked the question, 346 00:15:01,096 --> 00:15:06,806 is X less than Y or is X less than Y and Y is less than Z. 347 00:15:06,806 --> 00:15:09,176 So you can conjoin these expressions 348 00:15:09,226 --> 00:15:12,036 into ended expressions so to speak. 349 00:15:12,276 --> 00:15:14,416 Well, the syntax is gonna be pretty similar. 350 00:15:14,586 --> 00:15:16,896 The green block is gonna become a pair of parentheses. 351 00:15:17,146 --> 00:15:19,216 The less than side is gonna be a less than sign, 352 00:15:19,426 --> 00:15:22,366 and then the word and is going to become not one 353 00:15:22,436 --> 00:15:25,666 but two ampersands back to back, and we'll see why it's two 354 00:15:25,666 --> 00:15:27,376 and not one before long. 355 00:15:27,376 --> 00:15:29,136 But that's it in terms of the conversion. 356 00:15:29,366 --> 00:15:32,006 And with actually using those Boolean expressions on the left, 357 00:15:32,346 --> 00:15:37,136 if X is less than Y say, "X is less than Y" else 358 00:15:37,366 --> 00:15:39,756 if X is greater than Y say, 359 00:15:39,756 --> 00:15:43,326 "X is greater then Y" else say, "X is equal to Y." 360 00:15:43,326 --> 00:15:44,796 So, fairly simple idea, 361 00:15:44,846 --> 00:15:47,816 who knows why we actually care what the values of X or Y are 362 00:15:47,816 --> 00:15:49,286 at least in this out of context. 363 00:15:49,546 --> 00:15:51,956 But to translate this now to a language C, 364 00:15:52,196 --> 00:15:55,066 you've got some curly braces, you got an if, some parentheses, 365 00:15:55,296 --> 00:15:56,616 but now we're just building on some 366 00:15:56,616 --> 00:15:59,076 of the syntax we introduced before. 367 00:15:59,266 --> 00:16:02,196 So it's all fairly well convertible 368 00:16:02,196 --> 00:16:03,156 from one to the other. 369 00:16:03,156 --> 00:16:06,806 And lastly, you might have recalled the Fruit-craft RPG 370 00:16:06,806 --> 00:16:10,536 game that we played briefly with the little man that moves up 371 00:16:10,536 --> 00:16:13,096 and down, left and right and collects like oranges and apples 372 00:16:13,096 --> 00:16:14,586 and then brings them into the store. 373 00:16:14,836 --> 00:16:16,686 Well, that program was interesting 374 00:16:16,686 --> 00:16:20,166 because it had an inventory which was implemented in Scratch 375 00:16:20,166 --> 00:16:22,326 with a list which is kind of like a container 376 00:16:22,326 --> 00:16:24,356 that you can add things to again and again. 377 00:16:24,546 --> 00:16:27,046 Well, C has things that are similar to list. 378 00:16:27,386 --> 00:16:28,396 They're called arrays. 379 00:16:28,616 --> 00:16:30,796 We'll see they're not quite as versatile 380 00:16:30,796 --> 00:16:33,816 as Scratch's little inventory capability. 381 00:16:33,816 --> 00:16:36,026 You're gonna have to be a little more careful when it comes 382 00:16:36,056 --> 00:16:38,596 to adding stuff to an array and removing it from. 383 00:16:38,856 --> 00:16:41,726 But what looks like this orange block as of now in Scratch, 384 00:16:41,806 --> 00:16:44,926 add orange to your so called inventory, well, it's gonna take 385 00:16:44,926 --> 00:16:46,626 at least two lines of code in C 386 00:16:46,836 --> 00:16:49,916 to actually create the inventory array with this first line 387 00:16:50,146 --> 00:16:53,216 and then put something like the word orange inside of it. 388 00:16:53,216 --> 00:16:55,946 But at the end of the day, the point of the slides is not 389 00:16:55,946 --> 00:16:59,416 to expect you to now go write programs in C but really 390 00:16:59,416 --> 00:17:01,796 to just remind you that, hey, if you ever get distracted 391 00:17:01,796 --> 00:17:04,486 from some of the minutia that is the semicolons, parentheses, 392 00:17:04,486 --> 00:17:07,796 and what is very new to many of you, it's really no different 393 00:17:07,896 --> 00:17:09,786 than the fun and games we implemented 394 00:17:10,076 --> 00:17:11,416 with Scratch last week. 395 00:17:11,536 --> 00:17:13,616 So perhaps take some comfort from that. 396 00:17:13,616 --> 00:17:16,176 So here is the simplest program 397 00:17:16,316 --> 00:17:20,216 which is perhaps an ironic statement that you can implement 398 00:17:20,216 --> 00:17:23,326 with this language called C. So it takes a few lines of code 399 00:17:23,326 --> 00:17:24,876 to actually get something up and running 400 00:17:24,876 --> 00:17:27,146 and as you'll recall last Friday, I didn't even get this 401 00:17:27,146 --> 00:17:29,146 up and running because part of the process 402 00:17:29,146 --> 00:17:31,746 of writing a program is to one, write it. 403 00:17:31,956 --> 00:17:34,366 So write the so called source code which is 404 00:17:34,366 --> 00:17:35,516 in a specific language. 405 00:17:35,856 --> 00:17:38,916 Last week we did the socks thing and that was in pseudocode. 406 00:17:38,916 --> 00:17:41,236 It's just some bogus language we made up that's kind 407 00:17:41,236 --> 00:17:43,386 of English-like, kind of look C-like now 408 00:17:43,386 --> 00:17:44,436 that you've seen this syntax, 409 00:17:44,826 --> 00:17:46,366 but it was just an arbitrary language. 410 00:17:46,596 --> 00:17:49,076 Today, I'm gonna start writing C code with you 411 00:17:49,326 --> 00:17:52,706 but I can't just write it and expect it to do anything. 412 00:17:52,706 --> 00:17:55,876 What was the key step that I said I needed to do on Friday 413 00:17:55,876 --> 00:17:57,856 but failed very publicly to do so? 414 00:17:57,856 --> 00:17:57,923 [ Inaudible Remark ] 415 00:17:57,923 --> 00:18:00,166 >> Okay, I did a lot of things wrong. 416 00:18:00,166 --> 00:18:01,486 Okay, so a compiler. 417 00:18:01,666 --> 00:18:04,276 So I tried running a program in this little black 418 00:18:04,276 --> 00:18:05,326 and white window called GCC 419 00:18:05,326 --> 00:18:07,196 and we'll see it today working properly 420 00:18:07,466 --> 00:18:09,106 that is a so called compiler. 421 00:18:09,326 --> 00:18:11,096 So writing a program at least 422 00:18:11,096 --> 00:18:13,056 in this language is a two-step process. 423 00:18:13,056 --> 00:18:15,626 You write the source code, the stuff that looks like this 424 00:18:15,626 --> 00:18:18,536 and then you save your file, and now you just have a text file. 425 00:18:18,736 --> 00:18:21,356 The convention is not to call it something dot txt 426 00:18:21,606 --> 00:18:23,876 but something dot C, just a convention. 427 00:18:24,166 --> 00:18:26,286 But that's not enough because computers at the end 428 00:18:26,286 --> 00:18:29,056 of the day only understand 0s and 1s and what you [inaudible] 429 00:18:29,056 --> 00:18:31,756 and is clearly not a collection of 0s and 1s. 430 00:18:31,896 --> 00:18:34,726 So you have to somehow convert the source code, 431 00:18:34,866 --> 00:18:37,786 the C code into 0s and 1s and for 432 00:18:37,786 --> 00:18:40,966 that process there exist this tool called a compiler 433 00:18:40,966 --> 00:18:43,726 that someone else wrote that takes this stuff as input 434 00:18:43,726 --> 00:18:45,926 and produces this stuff as output. 435 00:18:46,136 --> 00:18:49,266 And it's the 0s and 1s, that notice they're in patterns 436 00:18:49,266 --> 00:18:51,916 of 8 generally or chunks of 8, the so called byte 437 00:18:52,256 --> 00:18:54,946 that the actual CPU understands. 438 00:18:54,946 --> 00:18:57,156 And later in the semester we will tease apart just a little 439 00:18:57,156 --> 00:19:00,386 bit what these patterns are and why they do what they do. 440 00:19:00,556 --> 00:19:02,066 But for those of you who decide to go 441 00:19:02,066 --> 00:19:04,856 on in computer science realize there is a whole interesting 442 00:19:04,856 --> 00:19:07,196 world that uses details like these in the world 443 00:19:07,196 --> 00:19:08,946 of networking, in hardware, 444 00:19:09,126 --> 00:19:12,666 to actually solve some real world problems. 445 00:19:12,666 --> 00:19:15,986 So with that said, let's write our first program. 446 00:19:16,226 --> 00:19:19,026 Probably I shouldn't have said it's a two-step process 'cause 447 00:19:19,026 --> 00:19:21,206 it's a three-step process to actually write and run it, 448 00:19:21,386 --> 00:19:23,166 and the third step is just going to be to run it. 449 00:19:23,226 --> 00:19:24,496 So, let's go ahead and do this. 450 00:19:24,916 --> 00:19:27,236 How am I gonna go about writing a program? 451 00:19:27,236 --> 00:19:30,676 Well, it turns out you can write a program on most any computer. 452 00:19:30,966 --> 00:19:35,616 For instance I have an Apple Mac here but you can do this 453 00:19:35,616 --> 00:19:37,596 on Windows computers, on Linux computers 454 00:19:37,866 --> 00:19:40,216 if you have a compiler installed. 455 00:19:40,216 --> 00:19:42,736 It's pretty easy to do this on Macintosh computers. 456 00:19:42,736 --> 00:19:43,876 You can just go to Apple's website 457 00:19:43,876 --> 00:19:45,086 and download the appropriate link. 458 00:19:45,396 --> 00:19:48,326 It's a little more involved on a PC but it's all free, 459 00:19:48,326 --> 00:19:49,466 the software you can download, 460 00:19:49,696 --> 00:19:52,366 and Linux in similar operating systems generally come 461 00:19:52,366 --> 00:19:54,546 with a compiler or you run a simple command to get it. 462 00:19:54,796 --> 00:19:57,046 But long story short, all of you who own laptops 463 00:19:57,046 --> 00:19:59,646 or desktops could probably be writing programs, 464 00:19:59,976 --> 00:20:02,986 trivial though this one is up until now even 465 00:20:03,196 --> 00:20:03,946 if you didn't actually know it. 466 00:20:04,656 --> 00:20:05,976 >> So this time I had the foresight 467 00:20:05,976 --> 00:20:08,256 to install this compiler but what I'm gonna do is this. 468 00:20:08,256 --> 00:20:10,026 I'm gonna run the command called Nano 469 00:20:10,386 --> 00:20:11,966 which is a very simple program. 470 00:20:12,246 --> 00:20:14,216 It is similar in spirit to Text Edit 471 00:20:14,216 --> 00:20:16,586 on a Mac or Notepad on a PC. 472 00:20:16,696 --> 00:20:20,416 I'm gonna call my file hello.c. There's a little bit 473 00:20:20,416 --> 00:20:23,326 of distraction here but this is because this is not a GUI, 474 00:20:23,326 --> 00:20:25,836 a graphical user interface, this is a CLI, 475 00:20:26,236 --> 00:20:27,706 command-line interface, 476 00:20:27,996 --> 00:20:30,116 which just means there are no windows and icons. 477 00:20:30,116 --> 00:20:31,786 It's really a text based interface. 478 00:20:32,106 --> 00:20:33,726 Now just to get used to the conventions, 479 00:20:33,756 --> 00:20:36,616 because this is the program you'll use not for problems at 0 480 00:20:36,986 --> 00:20:39,966 but for 1 to start writing programs, notice at the top 481 00:20:40,056 --> 00:20:42,116 in this window just reminds you what you're working on. 482 00:20:42,116 --> 00:20:44,976 The top left tells you what program you're actually using. 483 00:20:45,196 --> 00:20:47,086 The interesting stuff is at the bottom. 484 00:20:47,086 --> 00:20:49,086 And most of these commands you don't actually need 485 00:20:49,296 --> 00:20:51,566 but this little caret symbol means control, 486 00:20:51,776 --> 00:20:54,756 so every keyboard these days has a control key so you hold that 487 00:20:55,096 --> 00:20:57,446 and hit X for instance to exit the program. 488 00:20:57,886 --> 00:21:01,886 You hit control O to write out your program. 489 00:21:01,886 --> 00:21:04,126 I'm not sure why it's not S for save but it's write 490 00:21:04,126 --> 00:21:05,326 out and that means save. 491 00:21:05,576 --> 00:21:07,606 And so frankly this is pretty much all you need to know 492 00:21:07,606 --> 00:21:09,296 with Nano to get up and running. 493 00:21:09,516 --> 00:21:11,756 So now let me recreate this program from memory. 494 00:21:11,756 --> 00:21:12,836 I think the first thing I saw 495 00:21:12,836 --> 00:21:15,686 on the slide was include standard io.h, 496 00:21:16,116 --> 00:21:19,356 then I said int main, some parentheses and curly braces. 497 00:21:19,356 --> 00:21:25,216 And then I said printf "Oh hi world!". 498 00:21:25,216 --> 00:21:28,816 For some reason I had this backslash N, a close quote, 499 00:21:28,816 --> 00:21:31,346 parenthesis, semicolon, close curly brace. 500 00:21:31,626 --> 00:21:33,816 Alright, so maybe a little underwhelming 501 00:21:33,816 --> 00:21:36,066 and we saw already what this program is going to do 502 00:21:36,066 --> 00:21:38,426 but let's just go through this process once before we start 503 00:21:38,426 --> 00:21:40,316 doing more interesting things and then we'll come back 504 00:21:40,686 --> 00:21:42,366 and at least give you a hint of what some 505 00:21:42,366 --> 00:21:44,526 of these syntactic details are doing. 506 00:21:44,826 --> 00:21:48,446 So I hit control X. It's very unnecessarily arcanely saying 507 00:21:48,486 --> 00:21:51,266 save modified buffer, and then answering no will 508 00:21:51,266 --> 00:21:52,106 destroy changes. 509 00:21:52,136 --> 00:21:54,296 So that part is pretty clear, so hit Y for yes. 510 00:21:54,886 --> 00:21:57,016 Then it's saying name filename to write. 511 00:21:57,016 --> 00:21:58,966 It's just asking you if you wanna change the name. 512 00:21:58,966 --> 00:22:00,436 I'm just gonna hit enter to leave it alone, 513 00:22:00,686 --> 00:22:01,916 and now I'm back at the prompt. 514 00:22:02,166 --> 00:22:04,626 So generally, if you don't get an error message 515 00:22:04,726 --> 00:22:07,336 in a command line environment like this, that's good. 516 00:22:07,466 --> 00:22:10,676 If nothing bad happens, it means what didn't happen is good. 517 00:22:10,976 --> 00:22:12,576 So with the fact that I'm now just back 518 00:22:12,576 --> 00:22:14,736 at my prompt is a good thing so now 519 00:22:14,736 --> 00:22:20,196 if I type this command GCC hello.c, 520 00:22:20,456 --> 00:22:23,196 this is gonna run this program that Apple wrote in this case 521 00:22:23,196 --> 00:22:25,136 or someone else wrote called GCC. 522 00:22:25,136 --> 00:22:28,916 It's gonna take as input hello.c and what it's gonna produce 523 00:22:28,916 --> 00:22:31,766 as output is something called a.out. 524 00:22:33,416 --> 00:22:34,066 Nothing happened. 525 00:22:34,326 --> 00:22:35,716 Why? That's good. 526 00:22:35,716 --> 00:22:37,376 That means there is in fact something there. 527 00:22:37,376 --> 00:22:39,446 So this has created a file that just 528 00:22:39,516 --> 00:22:42,476 by convention happens to be called a.out. 529 00:22:42,476 --> 00:22:44,836 Why? Well, out presumably 'cause it's output, 530 00:22:44,836 --> 00:22:46,536 a because it's the first letter of the alphabet. 531 00:22:46,536 --> 00:22:47,736 It was really that creative. 532 00:22:48,106 --> 00:22:50,586 Now I'm typing dot slash as a convention. 533 00:22:50,586 --> 00:22:52,036 So little tricks you'll start to pick up. 534 00:22:52,296 --> 00:22:54,506 Because I know this program was saved by default 535 00:22:54,506 --> 00:22:56,796 into my current folder, my current directory, 536 00:22:57,056 --> 00:23:00,726 so dot slash means run the program called a.out that's 537 00:23:00,726 --> 00:23:03,276 right here in my current directory not in, like, 538 00:23:03,276 --> 00:23:05,226 my applications folder or somewhere else. 539 00:23:05,526 --> 00:23:07,476 So now I hit enter and voila! 540 00:23:07,476 --> 00:23:08,736 I see, oh, hi world. 541 00:23:09,016 --> 00:23:10,476 So how did this actually work? 542 00:23:10,476 --> 00:23:11,656 Well, let's just give a teaser of some 543 00:23:11,656 --> 00:23:12,946 of the details before we move 544 00:23:12,946 --> 00:23:15,316 on to actual interesting functionality. 545 00:23:15,526 --> 00:23:18,126 So, we'll see in a moment that that first line at the very top, 546 00:23:18,286 --> 00:23:22,326 sharp include, says include some code, some source code 547 00:23:22,606 --> 00:23:23,666 that someone else wrote. 548 00:23:24,046 --> 00:23:26,986 Because what you don't see in this file is any code 549 00:23:26,986 --> 00:23:29,716 that actually says put a letter of the alphabet 550 00:23:29,716 --> 00:23:31,616 at the top left hand corner of the screen 551 00:23:31,616 --> 00:23:33,826 and then put the next character to the right in other words, 552 00:23:34,066 --> 00:23:36,216 I'm taking for granted at this moment in time 553 00:23:36,466 --> 00:23:39,986 that my computer knows how to print something to the screen. 554 00:23:39,986 --> 00:23:41,486 But that was not always the case. 555 00:23:41,616 --> 00:23:43,916 Someone-- or someone had to implement, 556 00:23:43,916 --> 00:23:49,716 had to write a tool called printf that itself takes input 557 00:23:49,716 --> 00:23:51,636 between quotes and produces 558 00:23:51,636 --> 00:23:53,696 as output some characters on the screen. 559 00:23:53,926 --> 00:23:56,416 Now, how can I use code that someone else wrote? 560 00:23:56,536 --> 00:23:59,746 Well, I include what's called a header file as we'll call it. 561 00:23:59,986 --> 00:24:03,936 This file here is called standard I/O or stdio.h 562 00:24:04,166 --> 00:24:06,876 and this is just another text file someone else wrote many 563 00:24:06,876 --> 00:24:09,236 years ago and by using that line of code there, 564 00:24:09,236 --> 00:24:12,096 I'm telling the computer, give me acces to this code 565 00:24:12,096 --> 00:24:14,436 that this other person wrote that's in that file. 566 00:24:14,506 --> 00:24:15,376 What's in that file? 567 00:24:15,376 --> 00:24:17,316 Well, a whole bunch of stuff but the only one I care 568 00:24:17,316 --> 00:24:20,176 about right now is a tool called printf. 569 00:24:20,506 --> 00:24:22,636 And henceforth I'm gonna start calling these tools, 570 00:24:22,856 --> 00:24:25,756 these monickers that I can use as building blocks 571 00:24:25,756 --> 00:24:28,776 in my own program, it's gonna be called a function as we'll see. 572 00:24:29,046 --> 00:24:31,296 Now this other stuff here, int, we'll come back to that. 573 00:24:31,476 --> 00:24:33,996 Main is relevant because when you start writing a program 574 00:24:33,996 --> 00:24:36,946 in Scratch, you used what block as the very first usually? 575 00:24:36,946 --> 00:24:38,276 [ Inaudible Remark ] 576 00:24:38,276 --> 00:24:40,166 >> Yeah, so when green flag clicked, and that's kind 577 00:24:40,166 --> 00:24:41,616 of the very explicit way of saying 578 00:24:41,826 --> 00:24:44,436 when you start this program by clicking in the green flag, 579 00:24:44,676 --> 00:24:46,556 here is what's gonna get executed first. 580 00:24:46,676 --> 00:24:49,126 Well in C, the counterpart is main. 581 00:24:49,176 --> 00:24:52,076 And for now I'm just doing main open paren and close paren. 582 00:24:52,296 --> 00:24:54,866 Before long we'll see that you can actually put things inside 583 00:24:54,866 --> 00:24:57,586 those parentheses which make your programs even more useful. 584 00:24:57,856 --> 00:24:59,656 The curly brace is kind of similar 585 00:24:59,656 --> 00:25:01,926 to Scratch's interlocking mechanism 586 00:25:02,136 --> 00:25:04,046 that you really see explicitly with for loops. 587 00:25:04,346 --> 00:25:07,436 Well, for a thing called main which itself is a function 588 00:25:07,756 --> 00:25:11,276 but a function I wrote, a tool I am writing at the moment not 589 00:25:11,276 --> 00:25:14,456 like printf which someone else wrote long ago, I have to say 590 00:25:14,636 --> 00:25:16,516 where does main start, where does main end, 591 00:25:16,696 --> 00:25:18,666 and I do that by using these curly braces. 592 00:25:18,716 --> 00:25:21,696 Everything inside of the curly braces is the code I am writing. 593 00:25:21,966 --> 00:25:23,306 Finally, what code did I write? 594 00:25:23,306 --> 00:25:25,576 Well, just this line of code here. 595 00:25:25,826 --> 00:25:28,596 Now, you can probably guess what this piece of syntax does. 596 00:25:28,596 --> 00:25:30,896 So the double quote says here comes the input. 597 00:25:31,076 --> 00:25:33,186 This is what you typed into the white box in Scratch 598 00:25:33,486 --> 00:25:34,946 and anyone perhaps already familiar, 599 00:25:34,946 --> 00:25:37,626 the backslash N is the shorthand way of saying what? 600 00:25:37,626 --> 00:25:38,516 [ Inaudible Remark ] 601 00:25:38,516 --> 00:25:40,016 >> So it's a new line character. 602 00:25:40,016 --> 00:25:42,536 So things break if you try-- 603 00:25:42,536 --> 00:25:44,666 if the computer tries to take you too literally 604 00:25:44,846 --> 00:25:46,646 so it would not be correct generally 605 00:25:46,646 --> 00:25:48,716 to do something like that. 606 00:25:48,716 --> 00:25:52,376 Even though this looks like what the out-- 607 00:25:52,376 --> 00:25:54,896 this looks like the output you want, "Oh, hi world" 608 00:25:54,896 --> 00:25:57,136 and then everything else should go on the next line. 609 00:25:57,436 --> 00:26:00,106 Things generally break and this is also not very readable. 610 00:26:00,166 --> 00:26:02,536 You can imagine having many lines of code where all 611 00:26:02,536 --> 00:26:03,726 of a sudden your closed quote is 612 00:26:03,726 --> 00:26:05,156 on the next line all the way on the left. 613 00:26:05,156 --> 00:26:06,756 Things very quickly get ugly 614 00:26:06,756 --> 00:26:09,036 and you might even already be feeling this tension in Scratch 615 00:26:09,036 --> 00:26:11,126 if you're dragging and dropping and moving things around. 616 00:26:11,326 --> 00:26:14,446 Once your programs get fairly complex, it becomes harder 617 00:26:14,446 --> 00:26:16,686 and harder to find things and to tweak things 618 00:26:16,686 --> 00:26:18,006 if you're not being organized. 619 00:26:18,006 --> 00:26:20,416 So you have what are called escape characters 620 00:26:20,456 --> 00:26:23,196 like backslash N that is the shorthand notation 621 00:26:23,196 --> 00:26:26,606 of telling the computer put a new line character here. 622 00:26:26,606 --> 00:26:28,366 So I'm gonna go ahead and save this again. 623 00:26:28,366 --> 00:26:30,426 And to recap, we just did three steps. 624 00:26:30,426 --> 00:26:33,236 We did Nano for hello.c or whatever I called it, 625 00:26:33,556 --> 00:26:36,126 GCC to actually compile into 0s and 1, 626 00:26:36,336 --> 00:26:38,566 and then step three is to actually run it. 627 00:26:38,956 --> 00:26:42,136 Well a.out is a pretty stupid name for a program, right? 628 00:26:42,136 --> 00:26:44,336 It would kind of be unfortunate if every program 629 00:26:44,336 --> 00:26:45,896 that Microsoft made and Apple made 630 00:26:45,896 --> 00:26:47,046 by default was called a.out. 631 00:26:47,366 --> 00:26:48,696 You couldn't really distinguish them, 632 00:26:48,986 --> 00:26:51,786 so certainly it has the worlds come up with names for programs. 633 00:26:51,956 --> 00:26:53,776 Well, how do you do this with GCC? 634 00:26:53,776 --> 00:26:57,136 Well, you can specify what's called a switch or a flag. 635 00:26:57,806 --> 00:27:00,056 So in general, when you're running command line programs, 636 00:27:00,326 --> 00:27:02,546 they can take input or arguments 637 00:27:02,546 --> 00:27:04,736 or parameters or switches or flags. 638 00:27:04,916 --> 00:27:06,566 There are so many synonyms for these things. 639 00:27:06,566 --> 00:27:08,076 It's just hard to keep track of them at first 640 00:27:08,326 --> 00:27:10,046 but they all generally mean the same thing, 641 00:27:10,326 --> 00:27:13,696 provide additional input to this program called GCC 642 00:27:13,866 --> 00:27:15,866 by just specifying it on the command line. 643 00:27:15,866 --> 00:27:18,626 So you see two similarities between the first version, 644 00:27:19,026 --> 00:27:21,686 GCC hello.c, and the second version 645 00:27:21,726 --> 00:27:25,306 but obviously I've just added which part for that second line. 646 00:27:25,306 --> 00:27:26,796 [ Inaudible Remark ] 647 00:27:26,796 --> 00:27:28,286 >> Yeah, dash O hello. 648 00:27:28,286 --> 00:27:32,286 And this is arbitrary, but dash O probably means output. 649 00:27:32,436 --> 00:27:34,486 So output, what do you wanna output? 650 00:27:34,736 --> 00:27:37,746 A file called hello and not the default a.out. 651 00:27:37,976 --> 00:27:41,296 So, let's try this, let me go ahead and run GCC again 652 00:27:41,586 --> 00:27:44,436 on hello.c but let me move my cursor over 653 00:27:44,436 --> 00:27:45,906 and say dash little o, 654 00:27:45,906 --> 00:27:48,686 capitalization is usually important, and hello, 655 00:27:48,926 --> 00:27:51,686 then hit enter and now I can do dot slash, 656 00:27:51,686 --> 00:27:54,706 just say run the hello program I put right here by default, 657 00:27:54,706 --> 00:27:58,016 it's not elsewhere on my hard drive, hello, enter and there 658 00:27:58,016 --> 00:27:59,326 in fact is that program. 659 00:27:59,326 --> 00:28:01,836 So, it's getting more interesting but we'll soon see 660 00:28:01,836 --> 00:28:05,146 that things don't always compile as easily 661 00:28:05,146 --> 00:28:06,236 as this command suggests. 662 00:28:06,516 --> 00:28:10,966 The lines of commands we have to write get increasingly complex. 663 00:28:11,246 --> 00:28:14,016 So it turns out a really nice shortcut on most systems, oops, 664 00:28:14,076 --> 00:28:17,066 is to say if you have a file called hello.c 665 00:28:17,306 --> 00:28:19,936 and you wanna turn it into a program called hello, well, 666 00:28:19,936 --> 00:28:22,866 just write in the command makeHello and it will do all 667 00:28:22,866 --> 00:28:24,266 of that for you automatically 668 00:28:24,266 --> 00:28:25,786 and hand you a program called hello. 669 00:28:26,016 --> 00:28:27,806 And we'll see why this is increasingly useful 670 00:28:27,806 --> 00:28:30,836 as our programs get more and more interesting. 671 00:28:31,136 --> 00:28:34,476 So with that said, where are you actually going 672 00:28:34,476 --> 00:28:36,526 to run these programs? 673 00:28:36,526 --> 00:28:38,046 So, I'm doing this on my Mac. 674 00:28:38,096 --> 00:28:39,476 You might have a Mac, you might have a PC, 675 00:28:39,476 --> 00:28:42,456 you might have a Linux machine or any number of other devices 676 00:28:42,456 --> 00:28:45,166 and this very quickly becomes a bit of a nightmare 677 00:28:45,166 --> 00:28:47,316 because everyone has a slightly different configuration. 678 00:28:47,316 --> 00:28:48,926 It's hard to answer questions and whatnot 679 00:28:49,086 --> 00:28:50,566 and you don't necessarily have access 680 00:28:50,566 --> 00:28:53,276 to the same tools unless you yourself go through the process 681 00:28:53,276 --> 00:28:54,996 of configuring your machine manually 682 00:28:55,246 --> 00:28:57,506 by following many long sequences of steps. 683 00:28:57,506 --> 00:29:00,936 So, an alternative to that that is incredibly common 684 00:29:00,936 --> 00:29:03,466 in just the programming world and in industry is 685 00:29:03,466 --> 00:29:06,456 to actually not write programs on your own personal computer 686 00:29:06,656 --> 00:29:08,846 but to actually write and run them on a server, 687 00:29:08,846 --> 00:29:11,106 someplace where the software is all standardized. 688 00:29:11,266 --> 00:29:12,336 It is internet accessible 689 00:29:12,336 --> 00:29:15,606 so you can access it most anywhere anytime 690 00:29:15,606 --> 00:29:18,696 and you can also access tools and software and tricks 691 00:29:18,916 --> 00:29:20,366 that the staff have made available. 692 00:29:20,676 --> 00:29:24,716 So what you'll see in problem set 0 or problem set 1 is 693 00:29:24,716 --> 00:29:28,966 that you'll soon have access to what we call the CS50 cloud 694 00:29:29,026 --> 00:29:32,356 which is a bunch of servers that live on campus and on 695 00:29:32,356 --> 00:29:34,666 which you will all have a username and password. 696 00:29:34,666 --> 00:29:37,616 By default, it's gonna be the same username as you got 697 00:29:37,616 --> 00:29:41,306 for your FAS accounts because FAS also has its own systems 698 00:29:41,306 --> 00:29:43,676 and you use those usernames and passwords to log 699 00:29:43,676 --> 00:29:44,836 into the labs and whatnot. 700 00:29:45,026 --> 00:29:46,596 But just realize as a teaser coming 701 00:29:46,596 --> 00:29:49,176 for problem set 1 will be instructions on how 702 00:29:49,176 --> 00:29:51,216 to activate your so called cloud account and it's 703 00:29:51,216 --> 00:29:53,636 on the CS50 cloud that you'll be writing your programs, 704 00:29:53,636 --> 00:29:56,316 compiling your programs, testing your programs, 705 00:29:56,316 --> 00:29:57,926 running your websites ultimately. 706 00:29:58,156 --> 00:29:59,706 Just to fast forward to the end of the course 707 00:29:59,706 --> 00:30:02,616 with a little teaser, a lot of students for final projects 708 00:30:02,616 --> 00:30:06,336 in the CS50 fair like to host their final projects, 709 00:30:06,336 --> 00:30:08,646 if they're websites, at a real domain name, 710 00:30:08,686 --> 00:30:11,056 for instance isawyouharvard.com or any-- 711 00:30:11,116 --> 00:30:14,606 or shuttleboy.com or whatever name you actually find available 712 00:30:14,606 --> 00:30:17,936 and you can buy for like 9.99 from various websites. 713 00:30:18,256 --> 00:30:20,736 >> Well, what we'll be able to do this fall for you because all 714 00:30:20,736 --> 00:30:22,666 of the code will live centrally on this cluster 715 00:30:22,666 --> 00:30:26,036 of systems called the cloud is if you do decide, mostly for fun 716 00:30:26,036 --> 00:30:27,716 or for entrepreneurial reasons 717 00:30:27,716 --> 00:30:30,406 that you wanna go buy your own domain name, it's very easy 718 00:30:30,406 --> 00:30:33,086 to map that domain name to our servers 719 00:30:33,276 --> 00:30:35,396 so that even though the server itself is gonna be called 720 00:30:35,396 --> 00:30:38,726 cloud.cs50.net, your users, your friends, 721 00:30:38,726 --> 00:30:42,656 your family can actually visit isawyouharvard.com 722 00:30:42,846 --> 00:30:45,786 and the internet will know that isawyouhavard.com 723 00:30:45,786 --> 00:30:48,216 or your own domain name actually lives here. 724 00:30:48,216 --> 00:30:51,906 So, we'll do all of that hosting for you which will be a nice way 725 00:30:51,906 --> 00:30:54,396 of giving a little more personal touch to your final projects 726 00:30:54,486 --> 00:30:56,706 if you decide to implement something web based and would 727 00:30:56,706 --> 00:30:58,716 like your own so called domain. 728 00:30:59,346 --> 00:31:02,736 Alright, so let's now start doing more interesting things 729 00:31:02,736 --> 00:31:05,276 than just saying hello ad nauseam to ourselves. 730 00:31:05,276 --> 00:31:06,836 So, the jargon with that will-- 731 00:31:06,946 --> 00:31:08,076 with which we'll proceed is this. 732 00:31:08,076 --> 00:31:09,846 So a function is just a little tool. 733 00:31:09,846 --> 00:31:12,256 It's like a miniature program that you can write or use 734 00:31:12,256 --> 00:31:14,976 that someone else wrote and the first one we ourselves wrote is 735 00:31:14,976 --> 00:31:17,636 main and henceforth in this language called C just 736 00:31:17,636 --> 00:31:19,176 like every Scratch project begins 737 00:31:19,176 --> 00:31:22,906 with one green flag clicked so our program is gonna be called-- 738 00:31:22,906 --> 00:31:24,076 start with this thing called main. 739 00:31:24,426 --> 00:31:26,526 The standard library, that first line of code, 740 00:31:26,526 --> 00:31:31,376 it's not strictly necessary but any time you use a function, 741 00:31:31,506 --> 00:31:34,526 a tool that someone else wrote that lives in that 742 00:31:34,526 --> 00:31:37,786 so called library, you do need that include line as we'll see. 743 00:31:37,786 --> 00:31:39,816 And if you don't include it, GCC, 744 00:31:39,816 --> 00:31:41,206 the compiler will yell at you. 745 00:31:41,206 --> 00:31:43,816 And we'll see innumerable error messages today and onward 746 00:31:43,816 --> 00:31:46,086 as with you working at home ends 747 00:31:46,086 --> 00:31:48,026 on the problem sets 'cause this is the way you learn. 748 00:31:48,026 --> 00:31:49,736 And it's actually a useful feedback mechanism. 749 00:31:50,136 --> 00:31:54,196 But because C is a fairly old language and fairly low level, 750 00:31:54,376 --> 00:31:56,826 it's actually hard to do easy things. 751 00:31:56,826 --> 00:31:58,996 Even those of you who've taken AP computer science 752 00:31:58,996 --> 00:32:00,766 or something similar perhaps recall 753 00:32:00,766 --> 00:32:02,826 that even just getting a piece of input 754 00:32:02,826 --> 00:32:05,356 from the user is ridiculously not obvious. 755 00:32:05,356 --> 00:32:07,336 You need multiple lines of code, you need these days 756 00:32:07,336 --> 00:32:08,876 to use something called a scanner class. 757 00:32:08,876 --> 00:32:11,436 You have to jump through hoops just to ask the user for a piece 758 00:32:11,436 --> 00:32:13,296 of input which should be really the thing a 759 00:32:13,296 --> 00:32:14,666 language prioritizes. 760 00:32:14,666 --> 00:32:16,856 So, what we do at the start of the semester, 761 00:32:16,856 --> 00:32:18,986 and these are essentially training wheels we very quickly 762 00:32:18,986 --> 00:32:20,616 take off once you get more comfortable, 763 00:32:20,926 --> 00:32:23,096 we provide you with our own library. 764 00:32:23,326 --> 00:32:27,166 It's not called standard io.h, it's called cs50.h. That's 765 00:32:27,166 --> 00:32:29,886 where the code we wrote for you will live and then we'll see how 766 00:32:29,886 --> 00:32:32,576 to do this in examples today onward but we wrote 767 00:32:32,576 --> 00:32:34,896 for you some tools, some functions 768 00:32:35,146 --> 00:32:37,666 that calls GetChar for get a character. 769 00:32:37,666 --> 00:32:40,436 If you just want a single letter from the user, GetDouble. 770 00:32:40,486 --> 00:32:42,236 This is a type of number that we'll see in a bit. 771 00:32:42,276 --> 00:32:44,656 GetFloating-- GetFloat for a floating point number. 772 00:32:44,656 --> 00:32:46,826 GetInt, GetLong long, we'll see what 773 00:32:46,826 --> 00:32:48,626 that redundancy means, and GetString. 774 00:32:48,626 --> 00:32:51,656 And the last is perhaps the most useful at least at first glance. 775 00:32:51,816 --> 00:32:54,156 So, let's go ahead and do a little something with these. 776 00:32:54,186 --> 00:32:56,686 I'm gonna go away from my own Mac and I'm gonna go 777 00:32:56,686 --> 00:32:58,096 to the standard environment. 778 00:32:58,136 --> 00:33:01,826 And the means by which I connect to the so called cloud is 779 00:33:01,826 --> 00:33:04,126 with a command line program or GUI program. 780 00:33:04,546 --> 00:33:07,296 You'll see how to do this via some tutorials online 781 00:33:07,296 --> 00:33:08,186 for problem set 1. 782 00:33:08,466 --> 00:33:11,006 But on the Mac, I'm gonna run this program called Terminal. 783 00:33:11,286 --> 00:33:12,896 It comes with all Macs by default. 784 00:33:12,896 --> 00:33:14,976 PC users will wanna use something called PuTTY 785 00:33:15,036 --> 00:33:18,066 which is free or secureCRT with Harvard has available. 786 00:33:18,066 --> 00:33:19,936 But again, more on that in problem set 1. 787 00:33:20,336 --> 00:33:22,446 I'm gonna go ahead and run this command, SSH, 788 00:33:22,536 --> 00:33:25,086 which stands for Secure Shell. 789 00:33:25,086 --> 00:33:26,756 This just means making encrypted connection 790 00:33:26,756 --> 00:33:27,926 to someone else's server. 791 00:33:28,216 --> 00:33:32,216 Malan@cloud.cs50.net, enter. 792 00:33:32,536 --> 00:33:33,996 It's asking me now for my password. 793 00:33:34,256 --> 00:33:38,136 And now I met a prompt that says who I am as a little reminder. 794 00:33:38,196 --> 00:33:38,646 Where I am? 795 00:33:38,646 --> 00:33:40,896 And then in the parentheses we'll see in a moment, 796 00:33:40,896 --> 00:33:42,886 it tells me what folder I am inside of. 797 00:33:43,226 --> 00:33:45,636 So let's go ahead and write a slightly more 798 00:33:45,636 --> 00:33:46,786 interesting program. 799 00:33:46,786 --> 00:33:48,006 Among your printouts today, 800 00:33:48,236 --> 00:33:50,186 even though I generally won't bother printing slides 801 00:33:50,186 --> 00:33:51,276 since they're fairly minimalist, 802 00:33:51,476 --> 00:33:53,036 we will typically print source codes. 803 00:33:53,036 --> 00:33:54,406 You can actually take notes and don't have 804 00:33:54,406 --> 00:33:56,596 to scribble everything down that you see on the screen. 805 00:33:57,166 --> 00:33:59,376 Every file or program that you see here 806 00:33:59,376 --> 00:34:01,406 in this packet is alphabetically listed. 807 00:34:01,406 --> 00:34:05,286 So if I say hello.c which actually doesn't exist in here 808 00:34:05,416 --> 00:34:08,016 but hi1.c does, you'll find it alphabetically. 809 00:34:08,016 --> 00:34:09,096 So let's take a look at this, 810 00:34:09,646 --> 00:34:14,766 hi2.c. Actually I'm gonna use a different-- 811 00:34:15,256 --> 00:34:17,436 I'm gonna do a small cheat today. 812 00:34:17,436 --> 00:34:19,096 Rather than use nano on this computer, 813 00:34:19,096 --> 00:34:20,586 I'm gonna use a program called Vim, 814 00:34:20,906 --> 00:34:22,596 which is just another text editor. 815 00:34:22,596 --> 00:34:25,646 It's more like a word processor than it is a simple text editor. 816 00:34:26,036 --> 00:34:27,676 So, let's go ahead and open in your printouts 817 00:34:27,676 --> 00:34:30,336 if you would hi2.c. So first of all, 818 00:34:30,546 --> 00:34:31,996 there're a few more lines of code here. 819 00:34:31,996 --> 00:34:34,816 There's a whole bunch of stuff at the top which is wrapping 820 00:34:34,816 --> 00:34:36,986 in an ugly way 'cause I've made my font so big. 821 00:34:37,276 --> 00:34:38,886 But there're a few details to take away. 822 00:34:39,246 --> 00:34:41,386 So when you're writing a program in this language 823 00:34:41,386 --> 00:34:43,916 and many others, if you wanna make a comment to yourself 824 00:34:44,146 --> 00:34:46,546 or for other people who are reading this with their TF 825 00:34:46,546 --> 00:34:48,966 or fellow colleagues, you use what are called comments. 826 00:34:48,966 --> 00:34:51,246 So at top left we have a slash and a star 827 00:34:51,506 --> 00:34:53,946 and that says everything that follows that-- 828 00:34:54,116 --> 00:34:55,906 those symbols is just a comment. 829 00:34:55,906 --> 00:34:58,286 It's not going to get converted into 0s and 1s. 830 00:34:58,496 --> 00:34:59,496 It's just for humans. 831 00:34:59,766 --> 00:35:02,286 The fact that I have this star here, this star here, 832 00:35:02,286 --> 00:35:04,716 this star here, is really just 'cause I'm a little anal 833 00:35:04,716 --> 00:35:06,766 and it looks pretty to make a little box out of everything 834 00:35:06,766 --> 00:35:08,066 but it's not strictly necessary. 835 00:35:08,066 --> 00:35:10,686 The only thing that matters is the slash star here 836 00:35:10,976 --> 00:35:13,196 and the slash star at the bottom of this. 837 00:35:13,196 --> 00:35:14,256 All of those are comments. 838 00:35:14,256 --> 00:35:16,936 So let's actually scroll down to the interesting stuff here. 839 00:35:17,216 --> 00:35:18,976 And as an aside, those in the room 840 00:35:18,976 --> 00:35:20,536 who are among those more comfortable, 841 00:35:20,686 --> 00:35:22,556 even if we're not quite filling in blanks today, 842 00:35:22,556 --> 00:35:26,446 realize we will very quickly entice you with details 843 00:35:26,476 --> 00:35:27,726 that you don't already know. 844 00:35:27,726 --> 00:35:30,316 Today is meant to level the playing field so to speak. 845 00:35:30,606 --> 00:35:33,426 So here we have the code you have printed before you. 846 00:35:33,426 --> 00:35:36,336 I'm gonna zoom in a little more and notice it's pretty much 1, 847 00:35:36,336 --> 00:35:41,516 2, 3, 4, 5, 6, 7, 8, 9 lines of code and that's kind of rounding 848 00:35:41,516 --> 00:35:42,796 up because of the white space. 849 00:35:43,116 --> 00:35:44,076 So what's going on? 850 00:35:44,076 --> 00:35:47,296 Well, the first line of code, I'll fix the purple next time 851 00:35:47,296 --> 00:35:48,716 around so it's more legible. 852 00:35:48,966 --> 00:35:52,676 But this says include and this says include a file called 853 00:35:52,816 --> 00:35:56,586 cs50.h that lives in some special place on the server 854 00:35:56,856 --> 00:35:58,626 that the cs50 staff wrote. 855 00:35:59,006 --> 00:36:02,776 The next line of code says include standard io.h which just 856 00:36:02,806 --> 00:36:05,306 as a little sanity check contains among other things, 857 00:36:06,166 --> 00:36:06,426 printf. 858 00:36:06,806 --> 00:36:09,436 So not the actual function as we'll see printf. 859 00:36:09,846 --> 00:36:13,376 Just the .h file as we'll see is obviously named different 860 00:36:13,376 --> 00:36:14,326 than a .c file. 861 00:36:14,586 --> 00:36:16,616 In a .h file, it's generally the names 862 00:36:16,616 --> 00:36:17,966 of functions other people wrote. 863 00:36:18,146 --> 00:36:20,166 In .c files are the actual co-- 864 00:36:20,166 --> 00:36:21,966 is the actual code that other people wrote. 865 00:36:22,196 --> 00:36:24,786 All you have to include in your own programs is the so-called 866 00:36:24,786 --> 00:36:25,526 header file. 867 00:36:25,526 --> 00:36:27,266 And that's what cs50.h and-- 868 00:36:28,096 --> 00:36:30,456 standard io.h are but we'll see those 869 00:36:30,456 --> 00:36:31,706 in more detail before long. 870 00:36:31,956 --> 00:36:34,056 So for now, just take it on faith that you'll have 871 00:36:34,056 --> 00:36:36,166 to say int before you say main. 872 00:36:36,426 --> 00:36:38,176 Then you say main, open parenthesis, 873 00:36:38,176 --> 00:36:40,216 close parenthesis, open curly brace. 874 00:36:40,616 --> 00:36:43,676 So now after all these stupid setup, we can finally dive 875 00:36:43,676 --> 00:36:45,066 in to the actual program. 876 00:36:45,376 --> 00:36:47,006 Now this program's a little different 877 00:36:47,006 --> 00:36:49,666 from what we saw before because it's obviously not gonna say oh, 878 00:36:49,666 --> 00:36:50,366 hi world. 879 00:36:50,366 --> 00:36:53,706 It's going to say oh, hi David presumably. 880 00:36:53,706 --> 00:36:55,986 That part is uninteresting but how is it doing this? 881 00:36:55,986 --> 00:36:57,046 Well, a few details, 882 00:36:57,046 --> 00:36:58,756 a few building blocks we can start using 883 00:36:58,756 --> 00:36:59,826 in more compelling ways. 884 00:37:00,186 --> 00:37:03,906 So previously in the slides, I said there's gonna be things 885 00:37:03,906 --> 00:37:05,286 like ints or integers. 886 00:37:05,486 --> 00:37:07,056 Well, ints are only so useful. 887 00:37:07,056 --> 00:37:09,476 Sometimes you actually want words and characters. 888 00:37:09,476 --> 00:37:11,976 Fast forward mid-semester when we talk about cryptography 889 00:37:11,976 --> 00:37:14,266 and scrambling information, we need the ability 890 00:37:14,266 --> 00:37:16,406 to express ourselves in words, not just numbers, 891 00:37:16,616 --> 00:37:17,766 and we have that already. 892 00:37:18,096 --> 00:37:22,636 So if I want to use a string as we'll call it which is a word 893 00:37:22,636 --> 00:37:25,556 or phrase generally, they always go between double quotes, 894 00:37:25,646 --> 00:37:27,136 hence the quotes around David, 895 00:37:27,136 --> 00:37:29,686 and they are double quotes not single quotes intentionally. 896 00:37:30,126 --> 00:37:33,146 On the left hand side string name equals, well, 897 00:37:33,236 --> 00:37:36,496 what would we call this thing called name, in general, 898 00:37:36,496 --> 00:37:38,116 using last week's nomenclature? 899 00:37:38,736 --> 00:37:40,036 That's just a variable, right? 900 00:37:40,036 --> 00:37:42,586 It's more interestingly named an X or Y or Z. 901 00:37:42,626 --> 00:37:45,376 If we named it based on what it is, it's a name. 902 00:37:45,826 --> 00:37:48,086 So it's-- the name of the variable is name. 903 00:37:48,286 --> 00:37:50,016 Now, what type of variable is this? 904 00:37:50,016 --> 00:37:53,056 Well, unfortunately in C, you have to be ever so specific 905 00:37:53,056 --> 00:37:54,816 as to what you wanna put in the variable. 906 00:37:55,076 --> 00:37:56,646 So whereas before I mentioned int, 907 00:37:56,896 --> 00:37:59,266 turns out if you wanna put a string in a variable, 908 00:37:59,306 --> 00:38:01,986 you have to say this is a string. 909 00:38:02,246 --> 00:38:05,786 Now at this point in the story, I have a variable called name, 910 00:38:06,096 --> 00:38:09,666 stored inside of that is the string, literally David. 911 00:38:09,916 --> 00:38:11,786 What do I now want to do with this variable? 912 00:38:11,786 --> 00:38:15,206 Well, here is the F in printf for formatted printing. 913 00:38:15,546 --> 00:38:18,436 Well, before I just hardcoded a string inside 914 00:38:18,436 --> 00:38:19,746 of printf's double quotes. 915 00:38:20,056 --> 00:38:21,746 This time, I've done the same thing 916 00:38:21,936 --> 00:38:23,876 but there's a little curiosity here. 917 00:38:24,146 --> 00:38:25,946 There's this percent S sign. 918 00:38:26,236 --> 00:38:28,196 So this is a so-called format string. 919 00:38:28,246 --> 00:38:31,296 It's just a placeholder that says I don't 920 00:38:31,296 --> 00:38:33,666 yet know what I wanna put here but I wanna put something, 921 00:38:33,666 --> 00:38:35,116 let me get back to you in just a moment. 922 00:38:35,366 --> 00:38:37,816 Well, in just a moment, it happens right after the comma. 923 00:38:38,136 --> 00:38:41,056 Notice I close the quotes after I'm putting a new line-- 924 00:38:41,296 --> 00:38:42,676 after I'm putting this new line, 925 00:38:42,886 --> 00:38:45,056 but then to the right hand side is comma name. 926 00:38:45,356 --> 00:38:48,906 So before we use printf, a function, and we passed 927 00:38:48,906 --> 00:38:51,396 in one input and input henceforth we're gonna call 928 00:38:51,546 --> 00:38:53,106 arguments or parameters. 929 00:38:53,146 --> 00:38:54,636 It's just an input to a function, 930 00:38:54,846 --> 00:38:56,086 the thing inside parentheses. 931 00:38:56,366 --> 00:38:58,796 But sometimes you can pass multiple things inside those 932 00:38:58,796 --> 00:39:01,056 parentheses, and so you separate them by commas. 933 00:39:01,256 --> 00:39:02,696 What do I wanna pass in name? 934 00:39:02,976 --> 00:39:05,716 And so now if you follow the intuition here, 935 00:39:05,936 --> 00:39:07,436 percent S is the placeholder, 936 00:39:07,686 --> 00:39:09,406 name is what I want to place there. 937 00:39:09,646 --> 00:39:12,106 So the end result, because someone wrote this function 938 00:39:12,106 --> 00:39:15,526 years ago is that printf takes this thing, takes this thing, 939 00:39:15,786 --> 00:39:19,326 plops David inside the middle of that formatted string 940 00:39:19,326 --> 00:39:21,276 and then renders the whole result. 941 00:39:21,626 --> 00:39:23,776 So let me go ahead and quit from this program. 942 00:39:24,576 --> 00:39:30,786 And let me go ahead and run-- how do I compile this? 943 00:39:31,236 --> 00:39:32,796 GCC, hi2.c, enter. 944 00:39:32,796 --> 00:39:34,076 It seems to work. 945 00:39:34,076 --> 00:39:36,246 A.out enter and oh, hi David. 946 00:39:36,456 --> 00:39:37,826 Alright, so now let's make things a little 947 00:39:37,826 --> 00:39:38,456 more interesting. 948 00:39:38,456 --> 00:39:42,716 Let's go into hi3.c. So again I have some comments up top. 949 00:39:42,716 --> 00:39:44,986 As a matter of convention and good style 950 00:39:44,986 --> 00:39:47,126 which is another theme of the course that we'll focus more 951 00:39:47,126 --> 00:39:49,786 on on Friday onward, it's generally good to-- 952 00:39:50,116 --> 00:39:52,196 good style to actually describe 953 00:39:52,196 --> 00:39:54,616 in English even succinct English, 954 00:39:54,876 --> 00:39:56,166 what your program is doing. 955 00:39:56,166 --> 00:39:58,236 Because as you'll soon find, when you're writing programs 956 00:39:58,236 --> 00:40:01,736 that aren't just four lines long but are 40 or 400 957 00:40:01,736 --> 00:40:04,366 which very quickly becomes easy, 958 00:40:04,366 --> 00:40:07,076 you'll find that even you the next morning have no idea what 959 00:40:07,076 --> 00:40:09,156 this code does for whatever reason, 960 00:40:09,156 --> 00:40:12,356 and just having some English comments is a huge boon 961 00:40:12,356 --> 00:40:13,976 to comprehension for yourself and others. 962 00:40:14,546 --> 00:40:17,476 >> So now we can finally do something more interactive. 963 00:40:17,536 --> 00:40:20,666 Right now, we're finally getting to the point of scratch 964 00:40:20,666 --> 00:40:22,506 which can take some input from the user. 965 00:40:22,506 --> 00:40:23,766 It's not nearly as graphical 966 00:40:24,036 --> 00:40:26,096 but at least we're not hardcoding programs 967 00:40:26,356 --> 00:40:28,066 that are otherwise uninteresting. 968 00:40:28,066 --> 00:40:28,716 So here we go. 969 00:40:28,976 --> 00:40:30,666 The top of this program is the same. 970 00:40:30,666 --> 00:40:33,126 Include our library, include the standard library. 971 00:40:33,416 --> 00:40:35,546 Here's my pro-- here's my function called main. 972 00:40:35,786 --> 00:40:36,856 What do I now wanna do? 973 00:40:36,856 --> 00:40:38,836 Well, I don't have to call printf just once. 974 00:40:38,886 --> 00:40:41,076 If I wanna say something, do something, 975 00:40:41,296 --> 00:40:43,776 then say something else, I just call printf twice. 976 00:40:44,156 --> 00:40:45,636 So notice here I'm printing 977 00:40:45,636 --> 00:40:48,686 out state your name, a colon and a space. 978 00:40:48,686 --> 00:40:50,936 No new line just 'cause I made an aesthetic call 979 00:40:50,936 --> 00:40:51,906 that this looks kinda nice 980 00:40:51,906 --> 00:40:53,496 if the prompt stays on the same line. 981 00:40:53,926 --> 00:40:55,066 Now, I'm using this thing. 982 00:40:55,366 --> 00:40:58,056 So unlike printf which someone else wrote and is 983 00:40:58,056 --> 00:41:00,586 in the standard library, as we-- I mentioned a moment ago, 984 00:41:00,586 --> 00:41:03,536 GetString is in the-- says 50 library. 985 00:41:03,536 --> 00:41:05,966 So this is again part of the package we provide you 986 00:41:05,966 --> 00:41:09,596 within the server for the first few weeks to just make this kind 987 00:41:09,596 --> 00:41:12,936 of intuitively easy stuff, mechanically easy as well. 988 00:41:13,196 --> 00:41:14,566 So let's see what's going on here. 989 00:41:14,566 --> 00:41:17,416 Well, printf's job in like is to print something 990 00:41:17,416 --> 00:41:18,776 in the screen-- to the screen. 991 00:41:19,166 --> 00:41:21,846 But GetString as its name implies is supposed 992 00:41:21,846 --> 00:41:23,766 to get something from the user. 993 00:41:24,046 --> 00:41:27,336 But odds are it's not sufficient just to get it from the user, I, 994 00:41:27,336 --> 00:41:31,306 the human need to be handed whatever this function got 995 00:41:31,306 --> 00:41:32,856 from the user in some form. 996 00:41:33,216 --> 00:41:36,826 Odds are when this function called GetString gets a string 997 00:41:36,826 --> 00:41:39,546 from the user by expecting them to manipulate their keyboard 998 00:41:39,546 --> 00:41:41,236 with their fingers, I need to-- 999 00:41:41,236 --> 00:41:44,126 initiatively I'm gonna get back a sentence or a word 1000 00:41:44,126 --> 00:41:45,436 or a phrase or something. 1001 00:41:45,806 --> 00:41:46,866 And the reason that we now 1002 00:41:46,866 --> 00:41:49,036 on the second line have the equal sign is 1003 00:41:49,036 --> 00:41:51,696 because this function GetString has what's called a 1004 00:41:51,866 --> 00:41:52,776 return value. 1005 00:41:53,026 --> 00:41:55,226 It not only does something, as we'll see, 1006 00:41:55,436 --> 00:41:58,046 it also gets something and it has to hand it back to me. 1007 00:41:58,276 --> 00:42:00,646 Otherwise, what was the point of getting a string from the user 1008 00:42:00,646 --> 00:42:02,096 if I'm not handed the result 1009 00:42:02,096 --> 00:42:04,086 to actually do something interesting with. 1010 00:42:04,246 --> 00:42:08,856 So, thankfully I know I have storage space for return values. 1011 00:42:08,856 --> 00:42:10,246 I can just whip out a variable, 1012 00:42:10,526 --> 00:42:12,746 this time it's gonna be dynamically stored there. 1013 00:42:12,746 --> 00:42:15,296 I'm not hardcoding in D-A-V-I-D anymore 1014 00:42:15,556 --> 00:42:16,826 but I can still use a variable. 1015 00:42:17,096 --> 00:42:18,896 So long as I say I'm gonna put a string here, 1016 00:42:18,896 --> 00:42:22,046 it doesn't matter what string it is, GetString is gonna take care 1017 00:42:22,206 --> 00:42:23,876 of the job of getting it from the user. 1018 00:42:24,096 --> 00:42:26,286 And then I'm gonna use the same printf line after that 1019 00:42:26,716 --> 00:42:29,776 to actually plop it into this formatted string. 1020 00:42:30,026 --> 00:42:32,506 Let me go ahead and run this now. 1021 00:42:32,506 --> 00:42:38,276 So, GCC hi3.c and I'm getting a little tired of a.out. 1022 00:42:38,276 --> 00:42:41,476 So if I wanna give this thing a name I do, you know, 1023 00:42:41,476 --> 00:42:43,876 dash oh hi 3 or whatever I wanna call it. 1024 00:42:43,876 --> 00:42:49,216 Enter. Problem, first of many, but let's solve it together. 1025 00:42:49,446 --> 00:42:52,196 And then remember how you solve this 'cause otherwise completely 1026 00:42:52,196 --> 00:42:52,996 useless exercise. 1027 00:42:53,586 --> 00:42:54,686 So what's the error here? 1028 00:42:54,686 --> 00:42:55,646 Well, the font is big. 1029 00:42:55,646 --> 00:42:57,296 Normally the errors don't look this complicated 1030 00:42:57,296 --> 00:42:58,706 but they still look fairly arcane. 1031 00:42:58,706 --> 00:43:02,626 So, on the one hand, I've run this command here, GCC dash O 1032 00:43:03,506 --> 00:43:07,146 and this projector I'm highlighting things all day long 1033 00:43:07,146 --> 00:43:08,346 on the screen here and you're not seeing any 1034 00:43:08,346 --> 00:43:09,996 of it unfortunately it seems on this projector. 1035 00:43:10,386 --> 00:43:14,226 But what I ran was GCC and then I get back this big 1036 00:43:14,226 --> 00:43:14,966 error message. 1037 00:43:14,966 --> 00:43:17,706 So this thing here that starts with slash temp, 1038 00:43:17,706 --> 00:43:19,376 that's pretty useless information. 1039 00:43:19,566 --> 00:43:21,076 It's just that GCC 1040 00:43:21,076 --> 00:43:24,146 when compiling your program was apparently using some temporary 1041 00:43:24,146 --> 00:43:25,826 storage space in a folder called temp, 1042 00:43:26,196 --> 00:43:27,746 not of real consequence to me. 1043 00:43:27,986 --> 00:43:29,666 But now the error message gets interesting. 1044 00:43:29,666 --> 00:43:34,956 It says undefined reference to GetString then collect to LD, 1045 00:43:34,956 --> 00:43:38,826 LD stands for loader and that will be a useful detail 1046 00:43:38,826 --> 00:43:39,926 to keep in mind later on. 1047 00:43:40,316 --> 00:43:42,496 Loader returned 1 exit status. 1048 00:43:42,636 --> 00:43:45,876 So, what is it sound like is the problem intuitively here? 1049 00:43:46,056 --> 00:43:47,306 So if you know the answer 1050 00:43:47,306 --> 00:43:49,066 and know what the fix is don't answer this. 1051 00:43:49,176 --> 00:43:50,796 But if intuitively, you just have a sense 1052 00:43:50,796 --> 00:43:51,656 of what the problem is. 1053 00:43:52,326 --> 00:43:53,736 >> You can't find GetString. 1054 00:43:53,846 --> 00:43:55,266 >> It can't find GetString right? 1055 00:43:55,336 --> 00:43:56,136 Someone else-- thank you. 1056 00:43:56,136 --> 00:43:58,636 Someone else wrote GetString namely CS50. 1057 00:43:58,996 --> 00:44:03,236 I have included it using the cs50.h 1058 00:44:03,756 --> 00:44:06,866 but that only gives me access in the source code 1059 00:44:06,866 --> 00:44:08,656 to actually using it by name. 1060 00:44:09,046 --> 00:44:10,936 The bits, the zeros and ones 1061 00:44:10,976 --> 00:44:13,416 that compose the library are actually 1062 00:44:13,416 --> 00:44:17,696 in that file called cs50.c but those bits live or-- 1063 00:44:17,696 --> 00:44:18,646 even that's a white lie. 1064 00:44:18,726 --> 00:44:19,936 They technically live in a file, 1065 00:44:19,936 --> 00:44:22,856 as we'll eventually see called cs50.o. But for now, 1066 00:44:22,856 --> 00:44:26,336 I have made no mentioned of cs50.c. I've made no mentioned 1067 00:44:26,336 --> 00:44:29,996 of cs50.o. So, the problem at hand really reduces 1068 00:44:30,026 --> 00:44:33,296 to GSS knowing that I wanna use a function called GetString 1069 00:44:33,616 --> 00:44:35,636 but it doesn't know where the zeros and ones are 1070 00:44:35,636 --> 00:44:38,756 so how can it possibly integrate our zeros and ones 1071 00:44:38,756 --> 00:44:41,186 that the staff wrote into the zeros and ones you wrote 1072 00:44:41,376 --> 00:44:43,126 to produce one collective whole. 1073 00:44:43,406 --> 00:44:45,856 So it turns out that there's a two-step process. 1074 00:44:45,856 --> 00:44:48,206 When you want to use a library like CS50's, 1075 00:44:48,426 --> 00:44:51,066 you can't just run GCC on your program. 1076 00:44:51,296 --> 00:44:54,096 You also have to tell the compiler at the very end 1077 00:44:54,096 --> 00:44:57,566 of your command-- you know what, let me use the library with the 1078 00:44:57,616 --> 00:45:00,936 so called switch/l so that's an L not a 1. 1079 00:45:01,146 --> 00:45:04,646 So a dash lowercase L and then what library? 1080 00:45:04,726 --> 00:45:05,816 The CS50 library. 1081 00:45:06,246 --> 00:45:11,306 So this is pretty-- similarly might we say this dash L STD 1082 00:45:11,306 --> 00:45:12,916 for standard, for Standard I/O. 1083 00:45:13,126 --> 00:45:16,056 But it turns out that because the Standard I/O Library is 1084 00:45:16,056 --> 00:45:19,486 so common they don't force people to type this all day long 1085 00:45:19,486 --> 00:45:21,066 because it's just so commonly used. 1086 00:45:21,226 --> 00:45:23,856 Whereas CS50 is not nearly as popular a library in the world, 1087 00:45:24,016 --> 00:45:26,726 and so you'll have to explicitly say dash L CS50. 1088 00:45:26,986 --> 00:45:27,996 Why is there no space? 1089 00:45:28,366 --> 00:45:30,606 Stupid convention, but you have to do dash L 1090 00:45:30,606 --> 00:45:31,666 and then the name of the library. 1091 00:45:31,916 --> 00:45:32,866 So now let's hit Enter. 1092 00:45:33,616 --> 00:45:37,486 Better! So no error messages means success is probably what 1093 00:45:37,486 --> 00:45:38,066 has happened. 1094 00:45:38,556 --> 00:45:40,146 HI3 Enter. 1095 00:45:41,096 --> 00:45:44,736 Oh-oh. Command not found. 1096 00:45:44,906 --> 00:45:46,176 Why is this now a problem? 1097 00:45:46,446 --> 00:45:48,016 I did call it hi3 right? 1098 00:45:48,016 --> 00:45:51,646 I did use the dash O and I said hi3 dash O. 1099 00:45:51,646 --> 00:45:54,216 So why is the command not found? 1100 00:45:55,506 --> 00:45:55,626 Yeah? 1101 00:45:55,746 --> 00:45:56,756 >> It didn't know where hi3 is. 1102 00:45:57,136 --> 00:45:59,016 >> So it doesn't know where hi3 is. 1103 00:45:59,056 --> 00:46:01,746 Now this is-- again, testament to the fact that computers, 1104 00:46:01,966 --> 00:46:04,836 you know, sophisticated or scary as they might seem to some, 1105 00:46:05,036 --> 00:46:06,196 they're pretty stupid devices. 1106 00:46:06,196 --> 00:46:07,126 Unless you tell them what 1107 00:46:07,126 --> 00:46:08,556 to do they're not gonna know how to do it. 1108 00:46:08,766 --> 00:46:12,946 And by default a Mac and a PC generally only know programs 1109 00:46:13,166 --> 00:46:16,176 that happen to live in your C colon backslash programs file 1110 00:46:16,406 --> 00:46:18,796 or in the Mac on slash applications. 1111 00:46:19,066 --> 00:46:22,556 Whereas Hi3, I just created, it happens to live 1112 00:46:22,556 --> 00:46:24,456 in this directory but my computer doesn't know 1113 00:46:24,456 --> 00:46:27,656 that so you do dot slash which says current directory. 1114 00:46:27,656 --> 00:46:32,356 Dot is my current directory, slash means Hi3 is located here. 1115 00:46:32,586 --> 00:46:34,016 So now my program's running. 1116 00:46:34,016 --> 00:46:36,556 State your name, excellent. 1117 00:46:36,666 --> 00:46:40,366 We have just recreated what I could've implemented moments ago 1118 00:46:40,586 --> 00:46:41,606 with something more dynamic. 1119 00:46:41,746 --> 00:46:42,986 Well-- or with something static. 1120 00:46:42,986 --> 00:46:43,866 Let's do this once more. 1121 00:46:44,086 --> 00:46:44,896 So, state your name. 1122 00:46:44,896 --> 00:46:46,686 Let's do Cansu, enter. 1123 00:46:47,346 --> 00:46:50,586 An S in the side so Cansu, that was spelled C-A-N-S-U. 1124 00:46:50,586 --> 00:46:53,256 Her name is pronounced Cansu so she will appreciate your keeping 1125 00:46:53,256 --> 00:46:55,416 that in mind when you say hello to her in person, 1126 00:46:55,416 --> 00:46:56,616 Cansu being our head TF. 1127 00:46:56,686 --> 00:46:57,776 So Cansu, oh hi Cansu! 1128 00:46:57,776 --> 00:46:58,736 And we can do this all day long. 1129 00:46:59,106 --> 00:47:01,736 So we can provide any form of input here 1130 00:47:01,816 --> 00:47:03,206 and the program will just keep working. 1131 00:47:03,206 --> 00:47:05,476 But we can also get a little manipulative. 1132 00:47:05,516 --> 00:47:08,696 So, it turns out and this is a hint at some 1133 00:47:08,696 --> 00:47:10,336 of the security issues we'll discuss. 1134 00:47:10,606 --> 00:47:12,196 Sometimes you can mess with the computer, 1135 00:47:12,196 --> 00:47:13,116 for better or for worse. 1136 00:47:13,116 --> 00:47:14,516 And give it for instance nothing. 1137 00:47:15,166 --> 00:47:15,976 Well, this is weird. 1138 00:47:16,246 --> 00:47:20,276 This time I did nothing and so it just said oh, hi null. 1139 00:47:20,276 --> 00:47:21,696 So that's kind of interesting. 1140 00:47:21,696 --> 00:47:23,216 And it turns out-- a little teaser 1141 00:47:23,216 --> 00:47:26,266 for our security discussion as time passes that a lot 1142 00:47:26,266 --> 00:47:28,916 of the times when software is compromised and you download 1143 00:47:28,916 --> 00:47:30,466 like a cracked version of Photoshop 1144 00:47:30,466 --> 00:47:31,316 or something like that. 1145 00:47:31,566 --> 00:47:33,596 Or a lot of the times when a website is compromised 1146 00:47:33,596 --> 00:47:35,136 and you're able to steal someone's usernames 1147 00:47:35,136 --> 00:47:37,476 and passwords, very often are the result 1148 00:47:37,476 --> 00:47:39,826 of simple little things like this. 1149 00:47:39,826 --> 00:47:43,276 Notice in this program I have not checked whether 1150 00:47:43,276 --> 00:47:45,986 or not the user actually provided input. 1151 00:47:45,986 --> 00:47:48,446 I have not checked the length of the variable called nan. 1152 00:47:48,676 --> 00:47:49,356 I have not checked 1153 00:47:49,356 --> 00:47:51,416 that something actually came back to GetString. 1154 00:47:51,416 --> 00:47:53,996 In fact, if I don't hit the right key on the keyboard 1155 00:47:53,996 --> 00:47:57,406 like D-A-V-I-D and enter but instead hit a special command 1156 00:47:57,456 --> 00:47:59,546 that says, I'm not gonna give you a string at all. 1157 00:48:00,066 --> 00:48:03,026 Weird things, bad things can happen and so one 1158 00:48:03,026 --> 00:48:05,926 of the lessons too that will be repeated throughout the course 1159 00:48:05,926 --> 00:48:08,926 is to actually rigorously error check and make sure 1160 00:48:09,086 --> 00:48:12,666 that you're always thinking about your user being a bad guy. 1161 00:48:12,856 --> 00:48:14,576 So an adversary who's actually trying 1162 00:48:14,576 --> 00:48:17,216 to break your programs 'cause frankly too much software these 1163 00:48:17,216 --> 00:48:20,886 days is not written with a paranoia in mind. 1164 00:48:21,116 --> 00:48:26,856 So with that said, any questions on some the basic thus far? 1165 00:48:27,066 --> 00:48:27,216 >> Yes. 1166 00:48:27,216 --> 00:48:27,506 >> Yeah. 1167 00:48:27,506 --> 00:48:28,956 >> Okay. Now, I mean. 1168 00:48:29,026 --> 00:48:31,046 I got string and it went along [inaudible] 1169 00:48:31,046 --> 00:48:34,556 but what does that actually mean? 1170 00:48:34,556 --> 00:48:36,096 Is it is just like a string of words or-- 1171 00:48:36,096 --> 00:48:37,176 >> What does-- what does what mean? 1172 00:48:37,176 --> 00:48:37,596 A string? 1173 00:48:37,656 --> 00:48:39,466 >> Like what is that actually behind it? 1174 00:48:39,466 --> 00:48:40,446 >> So a good question. 1175 00:48:40,446 --> 00:48:43,426 So just-- and we will come back to this and flush out some 1176 00:48:43,426 --> 00:48:44,696 of these lower level details. 1177 00:48:45,116 --> 00:48:47,336 But for now, today's teaser will be this. 1178 00:48:47,336 --> 00:48:50,846 We know already and we know last week from our demonstration 1179 00:48:50,846 --> 00:48:53,496 of the floppy disks and such that there're various forms 1180 00:48:53,496 --> 00:48:54,726 of memory inside of a computer. 1181 00:48:54,726 --> 00:48:55,846 We know that there're hard disks 1182 00:48:55,846 --> 00:48:57,196 for instance but there's also RAM. 1183 00:48:57,196 --> 00:48:58,746 RAM being the one-- the memory 1184 00:48:58,746 --> 00:49:00,386 where things are stored temporarily 1185 00:49:00,386 --> 00:49:01,376 when you're running a program. 1186 00:49:01,716 --> 00:49:03,476 Well, RAM you might have a gigabyte 1187 00:49:03,476 --> 00:49:05,136 of RAM these days or two gigabytes. 1188 00:49:05,406 --> 00:49:07,076 Now the byte is the key word there. 1189 00:49:07,076 --> 00:49:09,696 Even though we say gigabyte that really means billion bytes. 1190 00:49:10,106 --> 00:49:12,806 So that means if you-- whether you own a Mac, PC or whatever, 1191 00:49:13,026 --> 00:49:15,896 you have a whole chunk of RAM memory which you can draw 1192 00:49:15,896 --> 00:49:18,146 for instance as a rectangle, if you can't quite see. 1193 00:49:18,146 --> 00:49:18,956 It's just a rectangle. 1194 00:49:19,316 --> 00:49:23,366 And because there are a billion bytes accessible to us, that's-- 1195 00:49:23,516 --> 00:49:27,216 essentially means well we can just call that byte number zero. 1196 00:49:27,416 --> 00:49:28,896 We can call this byte number one, 1197 00:49:28,896 --> 00:49:30,436 byte number two, byte number three. 1198 00:49:30,436 --> 00:49:31,936 Again, for those-- for whom this is blocked, 1199 00:49:31,936 --> 00:49:34,646 I'm just dividing the rectangle into smaller squares which is 1200 00:49:34,646 --> 00:49:36,136 to say that memory is addressable. 1201 00:49:36,176 --> 00:49:37,816 You can number every single byte just 1202 00:49:37,816 --> 00:49:39,376 like you can number every house on a street. 1203 00:49:39,806 --> 00:49:42,966 So now, in a program and you're asking the user for a string. 1204 00:49:43,236 --> 00:49:45,906 A string is just a sequence of characters. 1205 00:49:46,026 --> 00:49:49,136 In this case, it's a sequence of ASCII characters. 1206 00:49:49,136 --> 00:49:51,446 And ASCII recalls the code that we talked about last week. 1207 00:49:51,676 --> 00:49:54,556 Capital A is number 65. 1208 00:49:54,556 --> 00:49:56,706 So we talked about how the computers represent characters. 1209 00:49:56,936 --> 00:49:59,256 So when you get a string like D-A-V-I-D, 1210 00:49:59,466 --> 00:50:02,486 what's really happening is inside your computer's RAM is 1211 00:50:02,486 --> 00:50:04,936 going a byte where there's some zeros and ones. 1212 00:50:05,046 --> 00:50:06,636 >> But if you actually do the math, those zeros 1213 00:50:06,636 --> 00:50:09,916 and ones translate to an ASCII character which is D 1214 00:50:09,916 --> 00:50:14,666 which is gonna be a 65, 66, 67, 68 would be the letter D. 1215 00:50:14,936 --> 00:50:17,506 So similarly would there be the number 65 next 1216 00:50:17,506 --> 00:50:20,946 to this then the corresponding number for V-I-D 1217 00:50:20,946 --> 00:50:23,346 and then there's generally a special character at the end 1218 00:50:23,616 --> 00:50:25,926 that looks like a zero that says end of string here. 1219 00:50:26,256 --> 00:50:28,956 So when we call GetString, the function 1220 00:50:28,956 --> 00:50:30,336 that the cs50 staff wrote. 1221 00:50:30,566 --> 00:50:33,656 What we are doing is figuring out how to get input 1222 00:50:33,656 --> 00:50:35,136 from the user at the keyboard 1223 00:50:35,276 --> 00:50:37,176 into individual chunks of memory like that. 1224 00:50:37,586 --> 00:50:39,046 And when we return the string 1225 00:50:39,046 --> 00:50:41,646 to you we're essentially telling you 1226 00:50:41,646 --> 00:50:43,786 where this chunk of memory is. 1227 00:50:44,046 --> 00:50:45,946 So I can now proceed to print it, change it, 1228 00:50:45,946 --> 00:50:49,746 manipulate it, or what now. 1229 00:50:49,746 --> 00:50:50,386 [ Inaudible Remark ] 1230 00:50:50,386 --> 00:50:52,486 >> Those bytes in the row are called-- 1231 00:50:52,486 --> 00:50:52,766 >> A string. 1232 00:50:52,766 --> 00:50:53,146 >> Exactly. 1233 00:50:53,146 --> 00:50:55,066 A string is a contiguous sequence 1234 00:50:55,066 --> 00:50:56,806 of characters aka bytes. 1235 00:50:57,936 --> 00:50:58,496 Other questions? 1236 00:50:59,236 --> 00:50:59,396 Yes? 1237 00:51:00,836 --> 00:51:03,406 >> What exactly is the difference with this system 1238 00:51:03,406 --> 00:51:04,936 that we have here of compiling 1239 00:51:04,936 --> 00:51:08,096 and say another compiler for one like the C++? 1240 00:51:08,296 --> 00:51:08,796 >> Good question. 1241 00:51:08,796 --> 00:51:11,116 What is the difference between this program here at GCC 1242 00:51:11,116 --> 00:51:14,556 and for instance various other compilers DEV++, Visual Studio, 1243 00:51:15,366 --> 00:51:17,656 Eclipse is another one, NetBeans and so forth? 1244 00:51:17,906 --> 00:51:19,506 Fundamentally, very little. 1245 00:51:19,596 --> 00:51:21,196 So generally and those of you coming 1246 00:51:21,196 --> 00:51:23,756 from high school computer science background probably used 1247 00:51:23,966 --> 00:51:24,856 what's called an IDE. 1248 00:51:24,856 --> 00:51:26,556 An Integrated Development Environment 1249 00:51:26,716 --> 00:51:29,446 which actually is a gooey with menus and icons and such. 1250 00:51:29,446 --> 00:51:30,786 And you can write your source code in it 1251 00:51:30,996 --> 00:51:33,306 but to compile something you don't run a command like GCC 1252 00:51:33,336 --> 00:51:35,846 and then the name of the file, you instead click a button. 1253 00:51:36,106 --> 00:51:37,886 We'll actually get back to that point. 1254 00:51:38,186 --> 00:51:40,506 But as part of a more rigorous introduction 1255 00:51:40,506 --> 00:51:41,946 to understanding how the machines work 1256 00:51:41,946 --> 00:51:43,496 and writing programs at this lower level. 1257 00:51:43,656 --> 00:51:45,936 We start at this lower level with GCC. 1258 00:51:46,246 --> 00:51:47,966 So really it's just a matter of features. 1259 00:51:47,966 --> 00:51:51,516 GCC is probably the most popular compiler 1260 00:51:51,766 --> 00:51:54,176 in the world these days 'cause it's been around so long 1261 00:51:54,176 --> 00:51:55,986 and a lot of tools are based on it. 1262 00:51:56,076 --> 00:51:58,676 But it's more limited in its feature set 1263 00:51:58,886 --> 00:52:00,856 but again will build backup to those features. 1264 00:52:01,336 --> 00:52:02,036 Other questions? 1265 00:52:02,586 --> 00:52:02,696 Yeah? 1266 00:52:02,696 --> 00:52:04,946 >> Before we had the Makefile? 1267 00:52:04,946 --> 00:52:05,216 >> Uh hmm. 1268 00:52:05,376 --> 00:52:09,846 >> What's the difference between a compiler and a Makefile? 1269 00:52:09,846 --> 00:52:11,786 >> Good question, so we didn't have a Makefile yet, 1270 00:52:11,786 --> 00:52:12,736 more on that in the future. 1271 00:52:12,736 --> 00:52:14,016 But I do use the command make. 1272 00:52:14,456 --> 00:52:17,076 Essentially, this command make is just another program 1273 00:52:17,296 --> 00:52:20,356 that uses GCC to compile my program for me 1274 00:52:20,726 --> 00:52:22,486 but make is configurable. 1275 00:52:22,666 --> 00:52:25,196 So where as I currently am manually typing 1276 00:52:25,196 --> 00:52:28,166 like dash L cs50 and dash O hello. 1277 00:52:28,326 --> 00:52:29,786 All of this-- pain in the neck right? 1278 00:52:29,786 --> 00:52:30,766 If you're writing a program 1279 00:52:30,766 --> 00:52:32,376 and you're testing it and recompiling. 1280 00:52:32,586 --> 00:52:33,906 This very quickly gets tedious. 1281 00:52:33,946 --> 00:52:35,526 Typing the same commands again and again 1282 00:52:35,526 --> 00:52:38,426 and again make can be configured, as you'll soon see 1283 00:52:38,426 --> 00:52:41,296 in problem set 2 or 3 to do all these for you. 1284 00:52:41,296 --> 00:52:43,716 So that we take very quickly the tedium out of-- 1285 00:52:44,016 --> 00:52:47,086 what is otherwise-- could be a very repetitive process. 1286 00:52:47,896 --> 00:52:48,876 One more question? 1287 00:52:50,226 --> 00:52:52,606 Alright, so just to-- yup? 1288 00:52:53,066 --> 00:52:57,106 >> So the dot slash the A out thing-- 1289 00:52:57,406 --> 00:52:57,516 >> Uh hmm. 1290 00:52:57,756 --> 00:53:02,506 >> They only use that when the program that you're running-- 1291 00:53:03,976 --> 00:53:05,576 the program that you're referring 1292 00:53:05,576 --> 00:53:06,486 to is going to your computer? 1293 00:53:06,486 --> 00:53:06,986 >> That's correct. 1294 00:53:07,076 --> 00:53:09,676 When the program is on your computer you say dot slash 1295 00:53:10,706 --> 00:53:12,816 and this means on my computer. 1296 00:53:12,816 --> 00:53:13,646 >> And that's the [inaudible] this other stuff 1297 00:53:13,646 --> 00:53:14,786 that you would use, the GCC oh hi-- 1298 00:53:14,786 --> 00:53:15,816 >> Exactly. 1299 00:53:15,816 --> 00:53:17,536 Now GCC was installed by a special installer. 1300 00:53:17,536 --> 00:53:19,106 It's elsewhere on my hardware. 1301 00:53:19,106 --> 00:53:20,916 It's not in my current folder but because I installed it 1302 00:53:20,916 --> 00:53:22,806 with like a normal installer, double click and all of that. 1303 00:53:23,036 --> 00:53:25,816 It configured the Mac or in this case, the Linux server to put it 1304 00:53:25,816 --> 00:53:28,276 into my so-called path which just means I know 1305 00:53:28,276 --> 00:53:29,446 where it is automatically. 1306 00:53:30,566 --> 00:53:32,756 But we'll pull back these layers too before long. 1307 00:53:32,986 --> 00:53:37,456 So a couple of commands and then an interesting code 1308 00:53:37,886 --> 00:53:40,166 and then the teaser, that's a teaser. 1309 00:53:40,166 --> 00:53:41,486 So I just type the command LS. 1310 00:53:41,866 --> 00:53:44,886 So at first this might seem very limited in terms 1311 00:53:44,886 --> 00:53:47,076 of functionality for you but when I first logged in to 1312 00:53:47,076 --> 00:53:49,416 that cloud, recall that my prompt looked like this. 1313 00:53:49,766 --> 00:53:52,236 LS is the list command, those of you who played 1314 00:53:52,236 --> 00:53:54,316 with DOS before might recall DIR. 1315 00:53:54,596 --> 00:53:57,086 When you type LS it just lists what's in your current folder, 1316 00:53:57,336 --> 00:54:00,746 my current folder or my so-called home directory. 1317 00:54:00,826 --> 00:54:04,106 The folder that I own on the cloud or that I own 1318 00:54:04,106 --> 00:54:08,126 on my own Mac or my own PC apparently has two folders 1319 00:54:08,126 --> 00:54:08,866 in it at the moment. 1320 00:54:09,076 --> 00:54:12,876 One is called SRC for Source and one is called SRC.back 1321 00:54:13,166 --> 00:54:15,726 which was my backup of today's source code in case I screw up. 1322 00:54:16,076 --> 00:54:19,046 If I want to move into a directory and you'll see-- 1323 00:54:19,046 --> 00:54:20,236 all of these will be walked 1324 00:54:20,236 --> 00:54:21,386 through in the first problem set. 1325 00:54:21,386 --> 00:54:24,286 So realize if it doesn't quite sink in today, not a problems 1326 00:54:24,286 --> 00:54:25,766 in terms of these low level details. 1327 00:54:26,216 --> 00:54:27,826 CD is Change Directory. 1328 00:54:28,036 --> 00:54:30,666 So, I'm gonna go ahead and type CD source, enter. 1329 00:54:30,836 --> 00:54:33,076 And now notice my prompt changed a little bit. 1330 00:54:33,076 --> 00:54:35,926 In parenthesis now is a tilde which is shorthand notation 1331 00:54:35,926 --> 00:54:37,346 for My Home Directory. 1332 00:54:37,726 --> 00:54:40,516 And then Source which is the current directory I'm in. 1333 00:54:40,736 --> 00:54:42,956 So now if I type LS again and hit Enter, 1334 00:54:43,166 --> 00:54:44,566 I see a whole bunch of stuff. 1335 00:54:44,566 --> 00:54:48,186 And what's in this output are all the C files that we happened 1336 00:54:48,186 --> 00:54:50,116 to have printouts of for this particular week. 1337 00:54:50,116 --> 00:54:52,126 This is just the folder containing all of that stuff 1338 00:54:52,366 --> 00:54:54,936 that we created a PDF out of ultimately. 1339 00:54:55,146 --> 00:54:58,646 Now, the things in green are special things that I ran. 1340 00:54:58,646 --> 00:55:01,456 So what's nice if you configure your command line environment 1341 00:55:01,456 --> 00:55:04,276 appropriately is that things that are so-called executable, 1342 00:55:04,346 --> 00:55:05,316 programs that you wrote 1343 00:55:05,316 --> 00:55:07,456 like a.out happened to show up as green. 1344 00:55:07,506 --> 00:55:08,756 Hi3 is green. 1345 00:55:08,926 --> 00:55:11,476 And then all of these other white files are just source code 1346 00:55:11,476 --> 00:55:12,746 files, dot C file. 1347 00:55:12,966 --> 00:55:16,476 And I thought I would tease you with this one here. 1348 00:55:16,826 --> 00:55:20,036 This is a file called Holloway.c. This is-- 1349 00:55:20,036 --> 00:55:21,566 to be clear, not a program. 1350 00:55:21,566 --> 00:55:23,606 We'd expect you to write now or perhaps ever 1351 00:55:23,926 --> 00:55:25,836 since this program was written as part 1352 00:55:25,836 --> 00:55:28,026 of an obfuscated C contest. 1353 00:55:28,176 --> 00:55:31,096 So there're a lot of geeks in this world that like to compete 1354 00:55:31,096 --> 00:55:33,736 against each other by writing the most complicated looking 1355 00:55:33,736 --> 00:55:35,916 compact programs possible. 1356 00:55:36,236 --> 00:55:38,126 Even though it would take a computer 1357 00:55:38,126 --> 00:55:39,526 to even understand how they work. 1358 00:55:40,046 --> 00:55:41,056 So here is a program. 1359 00:55:41,056 --> 00:55:43,096 It's not necessary very well formatted 1360 00:55:43,096 --> 00:55:44,966 but it looks pretty complex, right? 1361 00:55:44,966 --> 00:55:46,776 I will be happy to admit that I read 1362 00:55:46,776 --> 00:55:48,726 through this I probably cannot tell you what it did unless I 1363 00:55:48,726 --> 00:55:50,666 really obsessed over these details. 1364 00:55:50,666 --> 00:55:52,566 And there's some syntax we haven't seen before. 1365 00:55:52,566 --> 00:55:54,906 This define keyword is something we'll see later on. 1366 00:55:54,906 --> 00:55:56,906 It turns out long is another keyword we'll see. 1367 00:55:57,086 --> 00:55:58,776 These curly-- these square brackets, 1368 00:55:59,066 --> 00:56:01,186 this is a pretty complicated looking program. 1369 00:56:01,666 --> 00:56:06,196 So for those more comfortable what does it do? 1370 00:56:06,426 --> 00:56:06,786 Could you tell? 1371 00:56:07,486 --> 00:56:11,056 So the winner of this year's contest, 1372 00:56:11,056 --> 00:56:12,326 this is like 2003 or so. 1373 00:56:12,326 --> 00:56:13,476 I included the length, let me go ahead 1374 00:56:13,476 --> 00:56:16,466 and compile this program called Holloway.c. No errors 1375 00:56:16,466 --> 00:56:17,266 which means that's good. 1376 00:56:17,266 --> 00:56:21,526 I'm gonna run a.out and your teaser for the kinds 1377 00:56:21,526 --> 00:56:24,196 of programs you can write after years and years and years 1378 00:56:24,196 --> 00:56:27,016 of practice will give you output like this. 1379 00:56:27,886 --> 00:56:31,356 Let's take a break. 1380 00:56:31,356 --> 00:56:31,423 [ Laughter ] 1381 00:56:31,423 --> 00:56:34,366 >> Let's take five-minute break. 1382 00:56:35,826 --> 00:56:39,096 Alright, so we are back. 1383 00:56:39,096 --> 00:56:41,906 Just to plant a couple of seeds so that you've seen it before 1384 00:56:41,906 --> 00:56:44,346 and then we can revisit in more useful context. 1385 00:56:44,646 --> 00:56:47,286 Printf, again it's a function, it's a tool someone else wrote. 1386 00:56:47,326 --> 00:56:50,716 You can invoke it with zero or more arguments or parameters. 1387 00:56:50,716 --> 00:56:53,546 The result-- or one or more arguments or parameters 1388 00:56:53,576 --> 00:56:56,656 that result of which is-- that will influence its behavior. 1389 00:56:57,216 --> 00:56:59,246 So these format strings we saw earlier. 1390 00:56:59,246 --> 00:57:01,526 We saw percent S which is a placeholder for a string, 1391 00:57:01,816 --> 00:57:02,756 turns out as you'll see 1392 00:57:02,756 --> 00:57:04,336 over time there are other placeholders. 1393 00:57:04,336 --> 00:57:06,536 If you know you're not gonna output a full string 1394 00:57:06,806 --> 00:57:10,066 but a single character, percent C will get that job done. 1395 00:57:10,296 --> 00:57:14,286 Percent D is for a digit, like a number or-- one or more digits. 1396 00:57:14,286 --> 00:57:17,086 If you want to output an integer or other types as we'll see. 1397 00:57:17,486 --> 00:57:20,036 F is for floating point value which is the fancy way 1398 00:57:20,036 --> 00:57:21,116 of saying real number. 1399 00:57:21,116 --> 00:57:23,426 Something with a decimal point and numbers thereafter. 1400 00:57:23,706 --> 00:57:25,526 And then LLD, we'll see in a moment, 1401 00:57:25,526 --> 00:57:28,016 this is for particularly long numbers 1402 00:57:28,016 --> 00:57:30,606 which will become relevant especially as you're trying 1403 00:57:30,606 --> 00:57:32,056 to process lots of information. 1404 00:57:32,366 --> 00:57:34,736 This backlash N, it's also a recurring theme and not just 1405 00:57:34,736 --> 00:57:37,806 in C but in PHP, JavaScript, other languages as well. 1406 00:57:38,446 --> 00:57:40,566 If you want to escape certain characters, 1407 00:57:40,796 --> 00:57:43,016 you need to use these so-called escape sequences. 1408 00:57:43,086 --> 00:57:45,666 So this is a way of experiencing various succinctly. 1409 00:57:45,746 --> 00:57:47,596 Usually with just two, maybe three characters, 1410 00:57:47,936 --> 00:57:50,726 a character that you can't really see as well 1411 00:57:50,726 --> 00:57:53,196 on the screen, otherwise, so a new line character. 1412 00:57:53,376 --> 00:57:55,306 You can certainly hit Enter but as we saw-- 1413 00:57:55,436 --> 00:57:57,776 seen that very quickly makes a mess of your code and such 1414 00:57:57,776 --> 00:57:59,496 and so backslash N is new line. 1415 00:57:59,816 --> 00:58:04,076 There's also this backslash R. Anyone know what backslash R is? 1416 00:58:04,526 --> 00:58:06,286 Yeah, so this is a carriage return. 1417 00:58:06,626 --> 00:58:09,406 So if you've ever seen or used an old school typewriter, 1418 00:58:09,656 --> 00:58:11,646 you might recall that when you're typing something 1419 00:58:11,646 --> 00:58:13,656 out on an old mechanical typewriter, 1420 00:58:13,946 --> 00:58:15,556 when you like hit the char-- 1421 00:58:15,556 --> 00:58:18,776 when you hit the enter key to move the head back 1422 00:58:18,816 --> 00:58:21,346 to the left hand side to the next line or you would pull 1423 00:58:21,346 --> 00:58:24,026 down some lever, it was really a two part process. 1424 00:58:24,026 --> 00:58:27,146 Pulling this lever or pushing the button would actually rotate 1425 00:58:27,146 --> 00:58:29,496 the paper enough so that you'd be on a new line 1426 00:58:29,596 --> 00:58:31,626 and it would also move the little imprinting head 1427 00:58:31,626 --> 00:58:33,026 that prints the letters of the alphabet 1428 00:58:33,026 --> 00:58:34,486 and such all the way to the left. 1429 00:58:34,486 --> 00:58:38,326 So you can think of a new line as being rolling the paper 1430 00:58:38,326 --> 00:58:42,016 up so you're on a new line but a carriage return moves the cursor 1431 00:58:42,016 --> 00:58:44,676 or moves that writing head all the way back to the left. 1432 00:58:45,316 --> 00:58:49,496 Now, in the world of Linux and Unix more generally, 1433 00:58:49,496 --> 00:58:52,486 both of which are our operating systems that had been 1434 00:58:52,486 --> 00:58:54,766 around for many, many years, Mac OS is based 1435 00:58:54,766 --> 00:58:56,636 on one of-- one version thereof. 1436 00:58:56,636 --> 00:58:58,516 I mean, you'll play with it in great-- 1437 00:58:59,026 --> 00:59:03,186 with great delight this semester, you will see 1438 00:59:03,186 --> 00:59:06,116 that backslash N is the standard in any text file 1439 00:59:06,496 --> 00:59:10,196 for creating what we know as new line characters. 1440 00:59:10,196 --> 00:59:12,006 In other words, if you pull up on a Linux system, 1441 00:59:12,256 --> 00:59:14,696 the equivalent nano or this thing called Vim 1442 00:59:14,916 --> 00:59:17,646 or the equivalent in general of Text Edit or Notepad 1443 00:59:17,646 --> 00:59:19,576 and you save that text file, what is-- 1444 00:59:19,576 --> 00:59:22,796 and anytime you hit the enter key, what's really being stored 1445 00:59:22,796 --> 00:59:25,836 in memory, what's really being stored in one of these bytes 1446 00:59:26,076 --> 00:59:30,756 on disk or in RAM is backslash N. Unfortunately, 1447 00:59:30,786 --> 00:59:33,336 in the Windows world, they decided years ago 1448 00:59:33,336 --> 00:59:36,486 that they would store not only backslash N but much 1449 00:59:36,486 --> 00:59:39,956 like a typewriter they would store backslash N, backslash-- 1450 00:59:40,126 --> 00:59:45,766 backslash, sorry, backslash R, backslash N. They store 2 bytes 1451 00:59:45,936 --> 00:59:47,206 at the end of every line. 1452 00:59:47,206 --> 00:59:50,706 Every time you hit the enter key in Notepad or in Microsoft Word, 1453 00:59:50,706 --> 00:59:53,046 small white lie, those two characters end 1454 00:59:53,046 --> 00:59:54,106 up getting saved in the file. 1455 00:59:54,416 --> 00:59:56,006 Now, for who knows what reason, 1456 00:59:56,186 --> 01:00:00,146 Apple decided they would use not backslash N, not backslash 1457 01:00:00,146 --> 01:00:03,476 or backslash N, they would just use backslash R. This is a 1458 01:00:03,476 --> 01:00:06,786 complete pain in the neck and at some point in your life, 1459 01:00:07,226 --> 01:00:09,246 even if it's after the course working in some lab 1460 01:00:09,246 --> 01:00:11,246 or in some project where you're processing files 1461 01:00:11,246 --> 01:00:12,756 or just opening files in a program, 1462 01:00:12,976 --> 01:00:14,716 you'll run into this stupid issue 1463 01:00:14,926 --> 01:00:17,566 that really is just a result of humans not agreeing 1464 01:00:17,566 --> 01:00:18,976 on some standard or just doing things differently. 1465 01:00:19,106 --> 01:00:22,286 >> You can see this frankly in the consumer world. 1466 01:00:22,486 --> 01:00:25,256 If you've ever noticed, Mac OS has its Apple menu 1467 01:00:25,256 --> 01:00:27,426 on the top left and Windows has its start menu 1468 01:00:27,426 --> 01:00:30,406 in the bottom left, really for no compelling reason. 1469 01:00:30,406 --> 01:00:32,226 In the other context, you know in a Window 1470 01:00:32,226 --> 01:00:34,346 and a Windows machine, you close it with the X 1471 01:00:34,346 --> 01:00:35,286 on the top right hand corner, 1472 01:00:35,526 --> 01:00:37,456 where do you close it in the Mac? 1473 01:00:37,456 --> 01:00:39,346 Top left hand corner, it's completely stupid, right? 1474 01:00:39,346 --> 01:00:41,186 It's just different for the sake of being different. 1475 01:00:41,186 --> 01:00:42,656 Whoever got there first, who cares, 1476 01:00:43,046 --> 01:00:44,366 same issue with these things here. 1477 01:00:44,366 --> 01:00:46,606 Now I mention this only because over time, 1478 01:00:46,886 --> 01:00:49,176 this will become important when you're not reading 1479 01:00:49,176 --> 01:00:51,796 in files necessarily but writing files. 1480 01:00:51,796 --> 01:00:54,266 When we get to the spell checking, 1481 01:00:54,266 --> 01:00:55,306 the dictionary assignment, 1482 01:00:55,466 --> 01:00:57,056 when you're actually manipulating text files, 1483 01:00:57,056 --> 01:00:59,626 maybe writing some output, these lower level details 1484 01:00:59,626 --> 01:01:02,956 that actually empower you to solve problems properly. 1485 01:01:02,956 --> 01:01:08,006 But there're others, backslash T. Folks in the consulting world 1486 01:01:08,006 --> 01:01:10,766 and finance world like to share files in Excel format 1487 01:01:10,766 --> 01:01:12,676 or in Text format that looks like Excel. 1488 01:01:12,966 --> 01:01:16,016 A CSV file, if you've ever opened a dot CSV file, 1489 01:01:16,306 --> 01:01:19,566 comma-separated values, that just means it's rows and columns 1490 01:01:19,566 --> 01:01:21,886 where each of the columns is represented with a comma 1491 01:01:22,056 --> 01:01:23,866 but you can open them in things like Excel. 1492 01:01:24,096 --> 01:01:27,936 A TSV file, tab-separated values uses the same thing 1493 01:01:27,936 --> 01:01:28,936 but a tab character, 1494 01:01:29,136 --> 01:01:31,436 if you wanna output an actual tab character, 1495 01:01:31,436 --> 01:01:32,876 the result of hitting the tab key, 1496 01:01:33,126 --> 01:01:34,446 you do use backslash T. That's 1497 01:01:34,446 --> 01:01:35,746 so those kinds of files are made. 1498 01:01:36,096 --> 01:01:38,186 And then sometimes we'll see you need 1499 01:01:38,186 --> 01:01:40,446 to escape even more useful characters. 1500 01:01:40,446 --> 01:01:43,116 Sometimes you need to escape a single quote as we'll see. 1501 01:01:43,436 --> 01:01:45,676 Sometimes you need to escape a double quote. 1502 01:01:45,976 --> 01:01:49,296 And just intuitively, when might you need to put a backslash 1503 01:01:49,606 --> 01:01:53,006 in front of a double quote given the examples we've seen 1504 01:01:53,006 --> 01:01:53,506 thus far? 1505 01:01:53,506 --> 01:01:54,086 [ Inaudible Remark ] 1506 01:01:54,086 --> 01:01:56,376 >> What's that? 1507 01:01:56,946 --> 01:01:58,056 Someone speak up more boldly. 1508 01:01:58,056 --> 01:01:58,736 [ Inaudible Remark ] 1509 01:01:58,736 --> 01:02:01,466 >> If it's in the middle of the string. 1510 01:02:01,466 --> 01:02:02,946 So thus far, the things I put 1511 01:02:02,946 --> 01:02:05,396 between quotes are just simple things like David 1512 01:02:05,396 --> 01:02:07,836 or David backslash N, but what if I want 1513 01:02:07,836 --> 01:02:10,636 to do call my self David in quotes, right? 1514 01:02:10,636 --> 01:02:12,976 I would need to surround my self with quotes just 1515 01:02:12,976 --> 01:02:14,706 to pass my self into printf. 1516 01:02:14,996 --> 01:02:17,646 But if I want those quotes to also appear in the output, 1517 01:02:17,876 --> 01:02:21,226 I now need to also include not another pair of double quotes 1518 01:02:21,486 --> 01:02:22,926 but escaped double quotes. 1519 01:02:22,926 --> 01:02:26,236 The reason being, again, computers are not very bright. 1520 01:02:26,326 --> 01:02:30,216 If I do this and this but then I also put some other quotes 1521 01:02:30,216 --> 01:02:33,226 inside, what's the computer potentially gonna think? 1522 01:02:33,856 --> 01:02:35,956 That it's actually two strings, right? 1523 01:02:35,956 --> 01:02:37,186 One starts here, one ends here, 1524 01:02:37,186 --> 01:02:38,446 then there's another and then another. 1525 01:02:38,576 --> 01:02:39,466 But this is not legit. 1526 01:02:39,466 --> 01:02:41,796 You can't just take a quoted string back to back 1527 01:02:41,896 --> 01:02:42,896 with another quoted string 1528 01:02:43,146 --> 01:02:44,756 so you'd get some kind of compiler error. 1529 01:02:44,756 --> 01:02:46,466 GCC would yell at you for some reason. 1530 01:02:46,746 --> 01:02:51,676 But if I instead put backslash there and backslash there, 1531 01:02:51,936 --> 01:02:54,466 so now I have double quote, backslash double quote, 1532 01:02:54,466 --> 01:02:56,796 backslash double quote, double quote, 1533 01:02:57,116 --> 01:02:58,606 this will actually print properly. 1534 01:02:58,606 --> 01:03:00,746 So, when in doubt, if you're having some weird character 1535 01:03:00,746 --> 01:03:02,706 issue, odds are you need to escape it in some way. 1536 01:03:02,906 --> 01:03:05,966 It gets really kind of trippy if you wanna output a backslash, 1537 01:03:06,126 --> 01:03:07,416 you can't just use the backslash, 1538 01:03:07,416 --> 01:03:09,066 you need to use backslash backslash 1539 01:03:09,316 --> 01:03:11,936 because otherwise a single backslash is confused with the 1540 01:03:11,936 --> 01:03:13,126 so called escape character. 1541 01:03:13,306 --> 01:03:15,016 And we'll also see backslash 0 1542 01:03:15,276 --> 01:03:17,736 which is actually the special character that's used at the end 1543 01:03:17,736 --> 01:03:19,726 of a string to say no more string 1544 01:03:19,726 --> 01:03:21,096 after this point in memory. 1545 01:03:21,366 --> 01:03:22,766 Okay, not to dwell too much on math 1546 01:03:22,986 --> 01:03:26,166 because you can very quickly bore someone in computer science 1547 01:03:26,166 --> 01:03:28,606 by dwelling too much on math but it's useful even 1548 01:03:28,606 --> 01:03:31,326 for logical constructs as we'll see with loops. 1549 01:03:31,386 --> 01:03:33,406 You do have mathematical operators in C 1550 01:03:33,406 --> 01:03:34,416 and most every language. 1551 01:03:34,696 --> 01:03:37,356 Most of these are probably self explanatory, addition, 1552 01:03:37,476 --> 01:03:40,456 subtraction, multiplication, division, and anyone? 1553 01:03:40,456 --> 01:03:41,806 [ Inaudible Remark ] 1554 01:03:41,806 --> 01:03:43,486 >> Mod or remainder. 1555 01:03:43,486 --> 01:03:44,936 So, there's subtle difference there. 1556 01:03:44,936 --> 01:03:46,166 But we'll use this over time 1557 01:03:46,416 --> 01:03:48,806 but the percent sign essentially gives you the remainder. 1558 01:03:48,806 --> 01:03:52,886 So if you do for instance 11 divided by 10 in grade school, 1559 01:03:53,126 --> 01:03:55,306 that's 1 with the remainder of 1. 1560 01:03:55,686 --> 01:03:59,236 So if you actually do in a program 11 percent 10, 1561 01:03:59,606 --> 01:04:02,446 what you'll get back is 1 because that is 1562 01:04:02,446 --> 01:04:05,296 in fact the remainder of that arithmetic operation. 1563 01:04:05,296 --> 01:04:07,926 So, there is more useful purposes than that 1564 01:04:07,926 --> 01:04:08,926 than just taking remainders. 1565 01:04:09,106 --> 01:04:10,546 We'll actually see it's a wonderful way 1566 01:04:10,546 --> 01:04:13,716 of effectively looping yourself back around, for instance 1567 01:04:13,716 --> 01:04:14,936 if you're at the end of the alphabet 1568 01:04:15,266 --> 01:04:17,296 or for other tricks altogether. 1569 01:04:17,606 --> 01:04:18,936 So what about those variables? 1570 01:04:18,936 --> 01:04:20,896 Well, it turns out C, like a lot of languages, 1571 01:04:21,136 --> 01:04:24,296 comes out of the box with a whole bunch 1572 01:04:24,296 --> 01:04:26,646 of built-in variable types. 1573 01:04:27,176 --> 01:04:31,026 So in C, this is not going to be the case as we'll see with PHP 1574 01:04:31,026 --> 01:04:33,696 and in JavaScript, at least not as rigorously. 1575 01:04:33,906 --> 01:04:36,966 In C, you really have to be specific as to what data type, 1576 01:04:37,286 --> 01:04:40,256 what type of data you're gonna put in a variable partly 1577 01:04:40,256 --> 01:04:43,586 because C gives us so low level access 1578 01:04:43,626 --> 01:04:46,026 to your computer's hardware and memory that you have 1579 01:04:46,066 --> 01:04:48,646 to tell your program what kind of variable you're gonna use 1580 01:04:48,886 --> 01:04:50,926 so that the operating system knows how much memory 1581 01:04:50,926 --> 01:04:52,056 to actually give you. 1582 01:04:52,276 --> 01:04:55,266 And with this low level power as we'll see, as I hinted before, 1583 01:04:55,356 --> 01:04:58,666 comes a lot of fun, interesting, scary security implications. 1584 01:04:58,666 --> 01:05:00,736 'Cause when you can actually manipulate a computer's memory 1585 01:05:00,736 --> 01:05:03,686 at this low level, you can steal people's passwords, 1586 01:05:03,686 --> 01:05:05,366 you can steal their data if you know how 1587 01:05:05,366 --> 01:05:06,306 that memory is laid out. 1588 01:05:06,476 --> 01:05:08,616 So we'll-- more on that as time passes. 1589 01:05:08,886 --> 01:05:12,506 So the types that come built into C are several 1590 01:05:12,686 --> 01:05:14,826 but the most common ones or the most useful ones 1591 01:05:14,826 --> 01:05:16,106 for our purposes are char. 1592 01:05:16,376 --> 01:05:18,246 If you just wanna store a single character 1593 01:05:18,446 --> 01:05:19,566 that maybe the user typed in, 1594 01:05:19,566 --> 01:05:21,906 you just call the variable of type char. 1595 01:05:22,266 --> 01:05:25,726 Float is a floating point number which is like a real number, 1596 01:05:25,726 --> 01:05:27,736 something with the digits after the decimal point, 1597 01:05:28,026 --> 01:05:31,896 and it's fairly small, it's what we're gonna call 32 bits. 1598 01:05:32,046 --> 01:05:35,046 A double is also a float but it has twice 1599 01:05:35,226 --> 01:05:36,836 as much storage capacity. 1600 01:05:37,076 --> 01:05:39,216 So here is one of the interesting takeaways, a char, 1601 01:05:39,216 --> 01:05:42,756 we've already spoiled this answer, takes up how many bits. 1602 01:05:43,896 --> 01:05:45,086 Here is the char, here is the char, 1603 01:05:45,086 --> 01:05:46,856 how many bits did we say each of these squares is? 1604 01:05:47,556 --> 01:05:47,876 >> 8. 1605 01:05:47,876 --> 01:05:49,736 >> So 8 bits, aka 1 byte. 1606 01:05:50,116 --> 01:05:53,386 So a char size is 1 byte or 8 bits. 1607 01:05:53,386 --> 01:05:55,886 This is convention, so on almost-- 1608 01:05:55,886 --> 01:05:58,276 and most any computer you overuse, if you're writing code 1609 01:05:58,486 --> 01:06:01,776 that uses a char, you're gonna be handed 8 bits or 1 byte 1610 01:06:01,826 --> 01:06:03,266 to store that particular char. 1611 01:06:03,466 --> 01:06:07,016 This is useful especially when we get to details like forensics 1612 01:06:07,126 --> 01:06:08,706 and looking at data on a hard drive, 1613 01:06:08,896 --> 01:06:10,266 'cause if you don't know how much store-- 1614 01:06:10,266 --> 01:06:13,756 how many bits are composing your file, you're never gonna recover 1615 01:06:13,806 --> 01:06:16,276 that data or be able to access what you're looking for. 1616 01:06:16,486 --> 01:06:22,276 Now a float is not 1 byte, it's 4 bytes, or 4 times 8, 32 bits. 1617 01:06:22,766 --> 01:06:28,576 So, a 32 bit of float gives you a bunch of precision. 1618 01:06:28,926 --> 01:06:31,086 So a floating point value again is a number 1619 01:06:31,086 --> 01:06:32,716 with a decimal point, so it depends. 1620 01:06:32,856 --> 01:06:33,506 It's a tradeoff. 1621 01:06:33,506 --> 01:06:35,526 You can either have a big number and a small decimal point 1622 01:06:35,756 --> 01:06:37,486 or small number and a big decimal point. 1623 01:06:37,606 --> 01:06:39,536 But because you only have a finite number of bits, 1624 01:06:39,536 --> 01:06:42,906 the takeaway for today is that there's only so much precision. 1625 01:06:42,906 --> 01:06:45,406 With the computer today, you can, for the most part, 1626 01:06:45,406 --> 01:06:48,676 not represent every possible number 1627 01:06:48,676 --> 01:06:50,056 that mankind can think of. 1628 01:06:50,116 --> 01:06:52,496 Because you can come up with an infinite number of values 1629 01:06:52,656 --> 01:06:54,746 but if you have a finite amount of space 1630 01:06:54,776 --> 01:06:57,276 and certainly a computer has a finite amount of disk space 1631 01:06:57,276 --> 01:06:59,396 or RAM, you have to make approximations. 1632 01:06:59,666 --> 01:07:00,406 And that turns out, 1633 01:07:00,406 --> 01:07:02,596 approximations might sometimes be fine, 1634 01:07:02,596 --> 01:07:04,606 certainly for the small programs we've seen thus far. 1635 01:07:04,836 --> 01:07:06,676 But when you start doing scientific research 1636 01:07:06,716 --> 01:07:09,216 or financial work, imprecision 1637 01:07:09,216 --> 01:07:11,836 and rounding errors start to add up. 1638 01:07:11,836 --> 01:07:16,076 Those of you may have seen Superman 3 or Office Space, 1639 01:07:16,076 --> 01:07:19,216 this little scam where they make money in the financial markets 1640 01:07:19,216 --> 01:07:21,136 by scheming fractions of pennies off. 1641 01:07:21,526 --> 01:07:23,616 That kind of problem tends to arise 1642 01:07:23,866 --> 01:07:25,916 because they're using a language or a computer 1643 01:07:25,916 --> 01:07:27,266 that only has finite precision 1644 01:07:27,266 --> 01:07:28,436 and so there are rounding errors. 1645 01:07:28,436 --> 01:07:31,386 And so the guys in Office Space took that money and they-- 1646 01:07:31,386 --> 01:07:32,956 their for loop-- way too fast is-- 1647 01:07:33,036 --> 01:07:35,556 was the takeaway in that movie, when they freaked 1648 01:07:35,556 --> 01:07:37,326 out because they had million dollars 1649 01:07:37,326 --> 01:07:38,426 or something like that overnight. 1650 01:07:38,746 --> 01:07:40,046 Okay, too long of a digression. 1651 01:07:40,326 --> 01:07:43,576 So, primitive type besides those three types, we have ints 1652 01:07:43,576 --> 01:07:46,316 which are just integers which are similarly 32 bits. 1653 01:07:46,406 --> 01:07:48,786 And just to plant this seed with 32 bits, 1654 01:07:48,786 --> 01:07:50,746 what's the biggest number you can represent? 1655 01:07:50,876 --> 01:07:56,426 So it's 2 to the 32, 'cause remember a bit is a 0 or 1. 1656 01:07:56,426 --> 01:07:59,196 So if I have 32 bits, each of which can be a 0 or 1, 1657 01:07:59,446 --> 01:08:01,286 that's two possibilities for every place, 1658 01:08:01,286 --> 01:08:03,346 so 2 to the 32, that's 4 billion. 1659 01:08:03,696 --> 01:08:07,536 So with an integer, an int data type, you can store any number 1660 01:08:07,536 --> 01:08:09,856 between 0 and 4 billion roughly. 1661 01:08:10,066 --> 01:08:13,186 Or if you want negative numbers, between negative 2 billion 1662 01:08:13,186 --> 01:08:15,736 and positive 2 billion, but it's a finite range. 1663 01:08:15,736 --> 01:08:18,506 And as we'll see, this can in fact be problematic. 1664 01:08:18,766 --> 01:08:22,586 In fact, when Facebook first debuted, random aside, 1665 01:08:23,036 --> 01:08:26,766 they used I believe an int to represent your profile IDs. 1666 01:08:26,946 --> 01:08:28,886 But if you've looked at more recent [inaudible] 1667 01:08:29,046 --> 01:08:32,356 or if you've looked at friends who relatively recently signed 1668 01:08:32,356 --> 01:08:34,976 up for Facebook, their profile IDs which you sometimes see 1669 01:08:34,976 --> 01:08:37,116 in the URL are actually pretty long and that's 1670 01:08:37,146 --> 01:08:39,366 because I believe at some point they did transition 1671 01:08:39,366 --> 01:08:44,226 to 64-bit values which tend to be called long longs. 1672 01:08:44,226 --> 01:08:47,936 And this is so that they could have more possible user IDs not 1673 01:08:47,936 --> 01:08:51,666 because they have billions and billions of users 1674 01:08:51,886 --> 01:08:53,976 but surely people create accounts to lead accounts 1675 01:08:53,976 --> 01:08:55,956 and they don't necessarily have to reuse numbers this way. 1676 01:08:56,206 --> 01:08:57,986 So, that might be a complete lie 1677 01:08:58,086 --> 01:08:59,866 but I think the numbers are now 64 bits, 1678 01:09:00,026 --> 01:09:01,786 so we'll see, I'll take a look. 1679 01:09:02,266 --> 01:09:04,766 Alright, so it turns out there're two other useful types 1680 01:09:04,766 --> 01:09:06,556 that you don't get for free out of the box. 1681 01:09:06,556 --> 01:09:07,456 One is a bool. 1682 01:09:07,626 --> 01:09:10,266 So we talked about Mr. Bool briefly last week in this idea 1683 01:09:10,266 --> 01:09:13,686 of true or false, but in C, you don't have a Boolean data type. 1684 01:09:13,686 --> 01:09:15,706 There is no such thing as true or false. 1685 01:09:15,986 --> 01:09:18,646 You have to fake it by using 0 or 1. 1686 01:09:18,886 --> 01:09:20,596 This is just a little inelegant and so one 1687 01:09:20,596 --> 01:09:22,486 of the things we'll introduce early on is this idea 1688 01:09:22,486 --> 01:09:26,756 of abstraction or design whereby if you wanna represent the ideas 1689 01:09:26,756 --> 01:09:29,526 of 0 and 1, true or false, well, 1690 01:09:29,526 --> 01:09:31,906 let's give them a synonym like true or false. 1691 01:09:31,956 --> 01:09:34,666 Let's not just arbitrarily say the number 0 is false, 1692 01:09:34,706 --> 01:09:35,776 the number 1 is true. 1693 01:09:36,096 --> 01:09:39,736 You can actually declare in C synonyms using special syntax 1694 01:09:39,736 --> 01:09:41,996 that says, you know what, even though yes at the end 1695 01:09:41,996 --> 01:09:45,866 of the day I'm implementing false as a 0 and true as a 1, 1696 01:09:46,146 --> 01:09:48,886 I can abstract away from that detail and use words 1697 01:09:48,886 --> 01:09:51,316 that are more intuitive to me like true and false. 1698 01:09:51,566 --> 01:09:54,136 So earlier today when I showed these little snippets 1699 01:09:54,136 --> 01:09:56,126 of infinite loops with while true, 1700 01:09:56,456 --> 01:09:59,526 true is we've inserted it ourselves 1701 01:09:59,566 --> 01:10:01,956 because we're assuming you're using the CS50 library. 1702 01:10:02,206 --> 01:10:03,846 >> In other languages, C++, 1703 01:10:03,846 --> 01:10:06,566 Java have the bool data type built in. 1704 01:10:06,786 --> 01:10:08,926 Strings, this also is a little white lie. 1705 01:10:08,926 --> 01:10:11,696 We've been using strings thus far to get input from user 1706 01:10:11,696 --> 01:10:12,656 and store it in variables. 1707 01:10:12,936 --> 01:10:14,916 String is actually not a data type 1708 01:10:14,916 --> 01:10:20,916 in C. It actually is something called a char star 1709 01:10:20,916 --> 01:10:22,886 and we'll come back to this before long. 1710 01:10:22,886 --> 01:10:24,066 But a char we've seen, 1711 01:10:24,436 --> 01:10:25,986 a character being a single character. 1712 01:10:26,196 --> 01:10:28,816 A star is something called a pointer so it turns 1713 01:10:28,816 --> 01:10:33,926 out a string is not quite what I'm describing it to be here. 1714 01:10:33,926 --> 01:10:36,356 It turns out when we call string a string, 1715 01:10:36,636 --> 01:10:38,866 we're really referring not to those characters 1716 01:10:39,126 --> 01:10:43,016 but to the address, the location and memory of those characters. 1717 01:10:43,056 --> 01:10:46,686 But, uninteresting for now but a teaser of details to come. 1718 01:10:46,986 --> 01:10:50,866 This chart, very boring and uninteresting but useful 1719 01:10:50,866 --> 01:10:52,046 over time and that is 1720 01:10:52,086 --> 01:10:55,386 to say there are very well defined rules as to 1721 01:10:55,386 --> 01:10:57,796 when you're writing code, what operators, 1722 01:10:57,856 --> 01:10:59,986 like what syntax should get evaluated first. 1723 01:10:59,986 --> 01:11:02,036 You might recall from grade school sometimes you put things 1724 01:11:02,036 --> 01:11:04,386 in parentheses so that your addition happens first, 1725 01:11:04,386 --> 01:11:05,536 then your multiplication, 1726 01:11:05,536 --> 01:11:07,756 otherwise multiplication would happen by default first. 1727 01:11:07,986 --> 01:11:10,586 Realize in programming languages like C, there is a set 1728 01:11:10,586 --> 01:11:12,136 of very well-defined rules, 1729 01:11:12,136 --> 01:11:13,426 and this is in the recommended books. 1730 01:11:13,426 --> 01:11:14,736 There're various resources online. 1731 01:11:14,956 --> 01:11:17,416 I just want to make mention of this in case there's ever doubt, 1732 01:11:17,946 --> 01:11:20,766 generally parentheses do solve your problem as an arithmetic 1733 01:11:21,056 --> 01:11:24,136 but do realize this is some lower level details we'll come 1734 01:11:24,136 --> 01:11:25,646 back to over time. 1735 01:11:26,026 --> 01:11:29,686 So we looked at a number of fairly small programs 1736 01:11:30,176 --> 01:11:32,886 that at the end of the day did very little more 1737 01:11:32,886 --> 01:11:34,736 than print hello world and the like. 1738 01:11:34,736 --> 01:11:36,526 There are so many other constructs like loops 1739 01:11:36,526 --> 01:11:39,086 and conditions and such, so what I thought we do 1740 01:11:39,396 --> 01:11:41,496 in conclusion today is look at this program. 1741 01:11:41,986 --> 01:11:45,356 So, we'll talk about this other axis of style on the course, 1742 01:11:45,356 --> 01:11:46,976 writing code that looks pretty 1743 01:11:46,976 --> 01:11:49,846 and as well aesthetically laid out. 1744 01:11:50,186 --> 01:11:54,166 So this is kind of a misinterpretation of that idea. 1745 01:11:54,516 --> 01:11:56,606 Writing code that just looks-- 1746 01:11:56,746 --> 01:11:58,546 I'm not even sure what this is supposed to look like, 1747 01:11:58,546 --> 01:12:00,646 a little flower or pinwheel or something like that. 1748 01:12:00,936 --> 01:12:02,946 It is in fact syntactically valid. 1749 01:12:02,946 --> 01:12:06,426 The semicolons are where they could be or should be. 1750 01:12:06,736 --> 01:12:11,076 I'm not quite sure what this is but it is-- 1751 01:12:11,076 --> 01:12:13,446 notice the familiar constructs, there's some ifs, some whiles, 1752 01:12:13,446 --> 01:12:15,766 some fors, these are details we'll certainly start playing 1753 01:12:15,766 --> 01:12:16,616 with in great detail. 1754 01:12:16,796 --> 01:12:17,996 Let me go ahead and compile this. 1755 01:12:18,026 --> 01:12:22,416 This two is from that obfuscated C contest so this is not 1756 01:12:22,416 --> 01:12:24,536 where we're going but this is where some people choose to go. 1757 01:12:24,896 --> 01:12:26,826 Let me go ahead and compile this. 1758 01:12:26,826 --> 01:12:29,166 Lots of errors but we've seen this error before. 1759 01:12:29,166 --> 01:12:31,516 Undefined reference, what was the solution last time? 1760 01:12:32,056 --> 01:12:32,396 >> Library. 1761 01:12:32,636 --> 01:12:33,446 >> So we need a library. 1762 01:12:33,446 --> 01:12:35,486 It turns out there's other libraries we'll see over time. 1763 01:12:35,486 --> 01:12:36,766 One is the math library. 1764 01:12:36,986 --> 01:12:40,066 You might wanna say dash L math but it turns out it's so common 1765 01:12:40,066 --> 01:12:41,286 that people just say dash LM. 1766 01:12:41,856 --> 01:12:44,136 And then for this stuff up here, it turns out 1767 01:12:44,136 --> 01:12:45,486 and I know this is just from experience, 1768 01:12:45,736 --> 01:12:47,656 this is called the Ncurses library. 1769 01:12:47,656 --> 01:12:51,456 This is a-- somewhat graphical library that allows me 1770 01:12:51,456 --> 01:12:53,056 to display graphics on the screen. 1771 01:12:53,276 --> 01:12:57,836 So if I link in both of those libraries, now it compiles okay. 1772 01:12:57,836 --> 01:13:00,626 If I now run a.out, your teaser 1773 01:13:00,626 --> 01:13:03,496 for this Friday's lecture is this climax. 1774 01:13:03,496 --> 01:13:04,746 [ Inaudible Remark ] 1775 01:13:04,746 --> 01:13:06,286 >> I know. 1776 01:13:06,436 --> 01:13:16,836 See you on Friday. 1777 01:13:16,836 --> 01:13:18,768 [ Inaudible Discussion ] 1778 01:13:18,768 --> 01:13:20,700 [ Music ]