1 00:00:09,316 --> 00:00:11,376 >> All right, welcome back to CS-50. 2 00:00:11,376 --> 00:00:15,646 This is the start of week 1, so it's this week that we dive 3 00:00:15,646 --> 00:00:19,166 into actual programming with a modern programming language 4 00:00:19,166 --> 00:00:21,636 and begin to tease apart some of the basic building blocks 5 00:00:21,636 --> 00:00:24,026 that frankly will carry us through the whole semester. 6 00:00:24,236 --> 00:00:26,596 More on that in a moment as to what that means, 7 00:00:26,636 --> 00:00:28,916 but quickly one, you do have a printout today 8 00:00:28,916 --> 00:00:29,966 of all the source code 9 00:00:29,966 --> 00:00:32,066 that we'll use today and also on Friday. 10 00:00:32,316 --> 00:00:34,146 You'll find that they lines are numbered, 11 00:00:34,146 --> 00:00:35,036 so if you've ever heard 12 00:00:35,036 --> 00:00:37,236 of a language called basic that's actually a language 13 00:00:37,236 --> 00:00:39,266 from yesteryear where you actually as the human how 14 00:00:39,306 --> 00:00:42,486 to type line 10, line 20, line 30, 15 00:00:42,486 --> 00:00:44,226 and then you wrote your code to the right of this. 16 00:00:44,226 --> 00:00:48,116 So that was kind of a humanities clever approach to leaving room 17 00:00:48,116 --> 00:00:50,786 in case in the future you wanted to add some more lines of code. 18 00:00:50,786 --> 00:00:53,196 You could put it between lines 11 and 19, 19 00:00:53,506 --> 00:00:56,256 didn't really scale particularly well. 20 00:00:56,256 --> 00:00:57,996 These are just printouts, line numbers, 21 00:00:57,996 --> 00:00:59,516 so you won't be writing those numbers there 22 00:00:59,516 --> 00:01:00,596 but I'll often refer to them. 23 00:01:01,096 --> 00:01:02,516 Office hours have been in progress. 24 00:01:03,046 --> 00:01:04,806 If you would like to come hang out in either the Quad 25 00:01:04,806 --> 00:01:07,136 or Quincy Hall dining hall this evening 26 00:01:07,136 --> 00:01:09,066 or next evening, do drop by. 27 00:01:09,066 --> 00:01:10,666 The teaching fellows will be there with the CAs 28 00:01:10,666 --> 00:01:12,616 to just chit chat about the course, 29 00:01:12,616 --> 00:01:14,946 help you with your scratch projects or just generally hang 30 00:01:14,946 --> 00:01:17,466 out and spend the evening in more of a communal environment. 31 00:01:17,766 --> 00:01:22,026 And then sections, we will have the sectioning process 32 00:01:22,026 --> 00:01:24,336 officially begin tonight around midnight, 33 00:01:24,336 --> 00:01:26,216 so check CS50.net's home page 34 00:01:26,216 --> 00:01:28,816 and we will have an announcement there as to how you section. 35 00:01:29,096 --> 00:01:30,816 But so that we have some breathing room 36 00:01:30,816 --> 00:01:32,876 and we don't start off the semester on too rushed a foot, 37 00:01:32,876 --> 00:01:36,106 what we're going to do is not start in intimate sections 38 00:01:36,106 --> 00:01:38,816 where 12 to 15 students and one CF this coming week, 39 00:01:38,816 --> 00:01:40,766 we're going to start those in two weeks time 40 00:01:40,976 --> 00:01:44,586 and this first week, so this coming Monday Tuesday Wednesday 41 00:01:44,586 --> 00:01:47,616 we're going to have super sections led by pairs of TFs. 42 00:01:47,616 --> 00:01:50,266 There's just going to be larger sections for the first week 43 00:01:50,266 --> 00:01:51,946 that you're welcome to attend any of those. 44 00:01:52,196 --> 00:01:56,316 The locations will be announced, so go to CS50.net/sections 45 00:01:56,386 --> 00:01:58,056 to see the current Google calendar for that. 46 00:01:58,056 --> 00:02:00,486 And then finally we do have a bunch of sensor boards 47 00:02:00,696 --> 00:02:03,816 if you would like to take on the hacker edition of PSET-0, 48 00:02:03,816 --> 00:02:07,076 but do realize that will always be the case we expect, 49 00:02:07,326 --> 00:02:09,496 we encourage almost everyone in the course 50 00:02:09,496 --> 00:02:12,726 to pursue the standard edition, so by no means should you feel 51 00:02:12,726 --> 00:02:14,946 like you are falling behind or cutting corners 52 00:02:14,946 --> 00:02:17,056 by just doing the standard edition that's meant 53 00:02:17,116 --> 00:02:17,736 for most folks. 54 00:02:18,266 --> 00:02:21,346 So with that said, we spent and a number 55 00:02:21,346 --> 00:02:23,956 of you have spent already some time playing with scratch 56 00:02:23,956 --> 00:02:25,566 and we actually have kept an eye, 57 00:02:25,646 --> 00:02:28,256 partly just out of curiosity, on what kinds 58 00:02:28,256 --> 00:02:30,716 of submissions have been coming in already. 59 00:02:31,096 --> 00:02:35,726 Now, not to worry if you think it's a little premature for -- 60 00:02:36,706 --> 00:02:39,806 little bug sometimes, the mouse stops responding so see 61 00:02:39,806 --> 00:02:44,316 if the screen starts responding again. 62 00:02:45,396 --> 00:02:45,766 There we go. 63 00:02:46,086 --> 00:02:46,476 All right. 64 00:02:46,616 --> 00:02:50,566 So I went ahead and dug up this so if only for curiosity's sake, 65 00:02:50,566 --> 00:02:52,536 only about 10 percent of the class has submitted already. 66 00:02:52,536 --> 00:02:53,556 So here too don't worry 67 00:02:53,556 --> 00:02:56,626 that you're already behind given how quickly some folks have 68 00:02:56,626 --> 00:02:57,556 started submitting. 69 00:02:57,736 --> 00:02:58,996 About 30 people submitted. 70 00:02:58,996 --> 00:03:01,216 We found this is slightly statistically interesting. 71 00:03:01,506 --> 00:03:04,036 So as might be expected, most of you, 72 00:03:04,036 --> 00:03:05,406 many of you are running Windows. 73 00:03:05,696 --> 00:03:08,756 What was kind of interesting was that neon of you have gone out 74 00:03:08,916 --> 00:03:12,016 and got and installed this brand new shiny operating system 75 00:03:12,016 --> 00:03:14,236 by Apple, Snow Leopard, and we'll see if some 76 00:03:14,236 --> 00:03:15,086 of those numbers change. 77 00:03:15,086 --> 00:03:17,376 Or maybe someone back there, so we'll see 78 00:03:17,376 --> 00:03:18,296 when those numbers come in. 79 00:03:18,496 --> 00:03:19,626 Some of these questions you'll find 80 00:03:19,686 --> 00:03:22,276 in PSET-0 survey are really just for statistical purposes. 81 00:03:22,276 --> 00:03:23,726 We'll try to do some fun things 82 00:03:23,726 --> 00:03:25,086 with the data throughout the semester. 83 00:03:25,546 --> 00:03:28,566 We don't fundamentally need to know what city you were born in, 84 00:03:28,566 --> 00:03:31,176 but it's kind of interesting if we can later visualize 85 00:03:31,176 --> 00:03:32,376 that with something like a Google map. 86 00:03:32,646 --> 00:03:34,656 So don't be too thrown off by those. 87 00:03:34,696 --> 00:03:37,436 If you're worried that everyone in this room has an i-Phone, 88 00:03:37,436 --> 00:03:38,426 very much not the case. 89 00:03:38,476 --> 00:03:40,396 Most of you have, or most of you 90 00:03:40,396 --> 00:03:43,436 that have submitted already have a normal mobile phone, 91 00:03:43,436 --> 00:03:46,956 which is perfect because it actually means the time Willy 92 00:03:46,956 --> 00:03:49,986 Chow CA and myself has spent on shuttle boy voice 93 00:03:50,036 --> 00:03:52,046 of late will actually get used. 94 00:03:52,046 --> 00:03:53,746 It's kind of a retro approach 95 00:03:53,746 --> 00:03:56,566 to making an application using touchtone prompt, 96 00:03:56,856 --> 00:03:59,866 but indeed if you call 617-BUG-CS50 97 00:03:59,996 --> 00:04:01,586 that service is now live. 98 00:04:01,586 --> 00:04:04,396 You can get out of here quickly to the Quad or to matter 99 00:04:04,396 --> 00:04:06,676 after class where it operates 24/7 as well. 100 00:04:06,886 --> 00:04:09,716 And coming soon are going to be a whole bunch of other features. 101 00:04:09,716 --> 00:04:11,636 So right now it's just option 1 for shuttle boy, 102 00:04:11,886 --> 00:04:14,816 but coming soon will be an option for wakeup calls, 103 00:04:14,816 --> 00:04:17,076 which we suspect will be kind of useful 104 00:04:17,076 --> 00:04:19,486 around mid-term time, exam time. 105 00:04:19,786 --> 00:04:21,996 We're mildly nervous because I think this could backfire 106 00:04:21,996 --> 00:04:24,906 completely if we only discover bugs the morning you have 107 00:04:24,906 --> 00:04:26,616 midterms but we'll try to avoid that. 108 00:04:26,986 --> 00:04:29,246 Coming soon too will be dining hall menus. 109 00:04:29,246 --> 00:04:31,346 If you want to find out what is for lunch today, 110 00:04:31,346 --> 00:04:33,476 what is for dinner today, thanks to a former teaching fellow, 111 00:04:33,476 --> 00:04:34,216 Kato Yuchiama [assumed spelling], 112 00:04:34,466 --> 00:04:36,326 we'll have an option online there as well, 113 00:04:36,506 --> 00:04:37,496 and then soon you'll be able to find 114 00:04:37,496 --> 00:04:40,486 out when the next CS-50 office hours actually are. 115 00:04:40,696 --> 00:04:42,786 Well, people tend to like where they live. 116 00:04:42,786 --> 00:04:45,376 This is a little slide that gives you a sense 117 00:04:45,376 --> 00:04:47,146 of where PSETs have been submitted from. 118 00:04:47,466 --> 00:04:50,076 Freshmen are kind of slacking it seems thus far, 119 00:04:50,116 --> 00:04:51,736 although Holeworthy and Matthews seem 120 00:04:51,736 --> 00:04:53,066 to have some decent contingencies. 121 00:04:53,246 --> 00:04:55,176 And then here among the houses what's striking is 122 00:04:55,176 --> 00:04:57,926 that the only house that is apparently doing other things 123 00:04:57,926 --> 00:05:02,206 this week has been Leverett, which ironically is one 124 00:05:02,206 --> 00:05:05,366 of the largest houses and last year the data was completely 125 00:05:05,366 --> 00:05:05,886 the opposite. 126 00:05:05,886 --> 00:05:08,026 We had more students from Leverett house than any 127 00:05:08,026 --> 00:05:10,256 so perhaps we're now paying that price. 128 00:05:10,256 --> 00:05:13,806 So just random fun facts here from CS-50. 129 00:05:13,976 --> 00:05:16,586 So let's now dive in. 130 00:05:17,136 --> 00:05:19,276 So this week and beyond is largely 131 00:05:19,276 --> 00:05:23,006 about this language called C and when you start playing 132 00:05:23,006 --> 00:05:25,186 with C you're playing with a real, 133 00:05:25,186 --> 00:05:27,046 a traditional programming language. 134 00:05:27,046 --> 00:05:29,296 so PSET 1, which will be realized this Friday 135 00:05:29,296 --> 00:05:32,336 on the course's website, will have you tackle something 136 00:05:32,336 --> 00:05:33,986 that may be unfamiliar to many of you, 137 00:05:33,986 --> 00:05:35,416 this operating system called Linux [assumed spelling], 138 00:05:35,686 --> 00:05:38,036 and also this language called C and we'll spend much 139 00:05:38,036 --> 00:05:41,236 of the semester on C. What's fun I think particularly 140 00:05:41,236 --> 00:05:43,296 about this language is that unlike Java 141 00:05:43,456 --> 00:05:46,226 and unlike scripting languages that some of you might know, 142 00:05:46,686 --> 00:05:50,186 PHP, Python, buzzwords like this, it's really low level. 143 00:05:50,186 --> 00:05:52,996 It's just shy of something called assembly language, 144 00:05:53,026 --> 00:05:54,946 which we'll glance at later in the semester, 145 00:05:55,156 --> 00:05:57,986 and by that I mean it really gives you find-grained control 146 00:05:57,986 --> 00:06:00,176 over where stuff is going in memory, 147 00:06:00,256 --> 00:06:02,966 like where in RAM are you actually using strings 148 00:06:02,966 --> 00:06:05,596 and numbers and databases and stuff like that. 149 00:06:05,996 --> 00:06:09,296 And it allows you at the same time to do really bad things. 150 00:06:09,296 --> 00:06:11,986 So many of these exploits that you read 151 00:06:11,986 --> 00:06:15,306 about if you read these kinds of publications or websites 152 00:06:15,626 --> 00:06:19,066 that involve websites being hacked over servers being taken 153 00:06:19,066 --> 00:06:22,326 over by hackers and so forth, to this day a lot 154 00:06:22,326 --> 00:06:24,546 of those compromises are either the result 155 00:06:24,546 --> 00:06:26,136 of just stupid mistakes by humans 156 00:06:26,596 --> 00:06:29,466 or sometimes more subtle mistakes made 157 00:06:29,646 --> 00:06:32,706 because the website or the program, whatever it is, 158 00:06:32,706 --> 00:06:37,676 was written in C or C++ or some related languages as well 159 00:06:37,866 --> 00:06:41,136 because with this power comes as they say great responsibility. 160 00:06:41,136 --> 00:06:43,686 You can do a lot of damage to your own data, 161 00:06:44,016 --> 00:06:48,586 to your own security by coding improperly and not being mindful 162 00:06:48,586 --> 00:06:49,836 of certain security issues. 163 00:06:50,016 --> 00:06:52,016 And what's actually fun, I think, as a result, 164 00:06:52,016 --> 00:06:53,366 about talking about this language 165 00:06:53,366 --> 00:06:56,426 for the next several weeks is you really understand just some 166 00:06:56,426 --> 00:06:59,576 societal issues like that, like why did this website get hacked? 167 00:06:59,576 --> 00:07:01,216 How could someone possibly do that 168 00:07:01,216 --> 00:07:04,436 and then what are more intelligent solutions to some 169 00:07:04,436 --> 00:07:06,486 of the problems we ourselves will see. 170 00:07:06,736 --> 00:07:09,346 So scratch meets C. so hopefully, 171 00:07:09,346 --> 00:07:11,906 even if you haven't sat down to do PSET 0 yet, which is fine, 172 00:07:11,996 --> 00:07:14,516 still plenty of time, you hopefully got a sense last week 173 00:07:14,516 --> 00:07:16,666 of what some of the basic building blocks are 174 00:07:16,666 --> 00:07:17,646 of programming. 175 00:07:17,646 --> 00:07:19,346 There's like a statement, do this, 176 00:07:19,346 --> 00:07:22,136 whether it's say something, think something, move somewhere. 177 00:07:22,466 --> 00:07:23,406 There's conditions. 178 00:07:23,486 --> 00:07:25,706 Do this if the following is true. 179 00:07:25,976 --> 00:07:28,416 There's variables if you need to keep around a piece of data, 180 00:07:28,516 --> 00:07:31,066 and then we saw some slightly more sophisticated topics 181 00:07:31,066 --> 00:07:34,366 like threads, multiples scripts and events, this ability 182 00:07:34,366 --> 00:07:35,856 to send messages from one script 183 00:07:35,856 --> 00:07:39,166 to another while all these same building blocks exist in C 184 00:07:39,166 --> 00:07:40,566 as they do in most languages. 185 00:07:40,836 --> 00:07:43,136 And even though what you're going to start writing 186 00:07:43,136 --> 00:07:46,486 for PSET 1 onward is going to look more like the below 187 00:07:46,486 --> 00:07:49,356 and less like the top, it's really the same ideas. 188 00:07:49,356 --> 00:07:51,786 And if you take one important point away from today, 189 00:07:52,366 --> 00:07:55,536 realize that you will, when you first sit down to program, 190 00:07:55,586 --> 00:07:58,406 maybe for the very first time and most likely with C 191 00:07:58,406 --> 00:08:01,056 for the first time, you will make some stupid mistake 192 00:08:01,296 --> 00:08:03,706 and you'll stress over some meaningless detail 193 00:08:03,796 --> 00:08:07,206 like why is my code not working, why is not compiling, 194 00:08:07,506 --> 00:08:09,616 and odds are it's going to boil down to something stupid 195 00:08:09,616 --> 00:08:11,856 like see that semicolon on line 4 there, 196 00:08:12,196 --> 00:08:14,106 like you'll accidentally have omitted that 197 00:08:14,236 --> 00:08:16,286 or you will have left off a single quote somewhere 198 00:08:16,286 --> 00:08:17,126 or double quote. 199 00:08:17,276 --> 00:08:18,186 And frankly this is one 200 00:08:18,186 --> 00:08:21,246 of the reasons we start off the course just last week 201 00:08:21,526 --> 00:08:22,266 with scratch, right? 202 00:08:22,266 --> 00:08:24,896 You don't have to worry about this uninteresting minutiae 203 00:08:25,186 --> 00:08:28,596 but realize now that we're going to be programming real computers 204 00:08:28,846 --> 00:08:31,826 and we have to be ever more precise and it's this level 205 00:08:31,826 --> 00:08:33,706 of precision, this level of nitpickiness 206 00:08:33,996 --> 00:08:35,156 that actually does matter. 207 00:08:35,196 --> 00:08:35,856 Because you'll find 208 00:08:35,856 --> 00:08:38,326 that a computer will do literally what you tell it 209 00:08:38,326 --> 00:08:40,836 to do, and if you don't say semicolon there 210 00:08:40,836 --> 00:08:43,506 in this particular language, you're not telling the computer 211 00:08:43,506 --> 00:08:45,246 that this is the end of the statement, 212 00:08:45,296 --> 00:08:46,716 and you're making things ambiguous. 213 00:08:47,106 --> 00:08:50,336 So ambiguity tends to be bad in programs as we will see. 214 00:08:50,616 --> 00:08:53,016 So what are these things going to start looking like today? 215 00:08:53,016 --> 00:08:55,676 Well, a quick teaser, a statement that last week looked 216 00:08:55,676 --> 00:08:58,546 like this, oh, hi world, with the little purple block 217 00:08:58,546 --> 00:09:01,296 and C onward is going to look a little more cryptic 218 00:09:01,296 --> 00:09:04,616 but at the tense of the day the same exact idea, Print F, 219 00:09:04,616 --> 00:09:08,936 which means a formatted printing mechanism, Ohi kind of a world 220 00:09:08,936 --> 00:09:11,056 and then backslash N, I think we said last week, 221 00:09:11,056 --> 00:09:11,636 what does that mean? 222 00:09:12,756 --> 00:09:15,216 New line. So you have to be even ever so explicit 223 00:09:15,216 --> 00:09:17,556 about where you want the enter key to be hit 224 00:09:17,556 --> 00:09:21,086 and /N represents the act of hitting enter on the keyboard. 225 00:09:21,306 --> 00:09:23,006 So that's what the statement's going to start looking 226 00:09:23,006 --> 00:09:24,866 like shortly, a bouillon expression, 227 00:09:24,866 --> 00:09:28,056 so this was something that could evaluate to true or false 1 228 00:09:28,056 --> 00:09:30,806 or 0 on or off, however you want to view the world. 229 00:09:31,036 --> 00:09:33,466 Well now what used to look like orange and green 230 00:09:33,466 --> 00:09:35,196 up top there is instead going 231 00:09:35,196 --> 00:09:36,896 to look a little more high school math, 232 00:09:37,196 --> 00:09:40,206 but parenthesis X less than Y, closed parenthesis. 233 00:09:40,206 --> 00:09:42,686 And then if you want to join these things together we're 234 00:09:42,686 --> 00:09:45,026 going to see things like ampersand ampersand, 235 00:09:45,316 --> 00:09:47,386 just happens to be the nomenclature in C 236 00:09:47,426 --> 00:09:49,666 for the bouillon idea of and. 237 00:09:50,366 --> 00:09:53,266 And the notion of or, if you want to say this or this, 238 00:09:53,626 --> 00:09:54,946 it's not going to be or it's going 239 00:09:54,946 --> 00:09:56,946 to be a vertical bar a vertical bar. 240 00:09:56,946 --> 00:09:58,356 And it's stupid things like this too, 241 00:09:58,356 --> 00:10:01,156 where we'll inevitably get an FAQ this semester 242 00:10:01,156 --> 00:10:03,236 like where is the vertical bar on my keyboard? 243 00:10:03,236 --> 00:10:05,356 This is not a character many of you usually use, 244 00:10:05,666 --> 00:10:08,566 it's usually shift and above the enter key 245 00:10:08,566 --> 00:10:10,416 on the typical keyboard that would vary. 246 00:10:10,416 --> 00:10:12,756 So again don't get hung up early in the semester 247 00:10:12,756 --> 00:10:16,486 on stupid details like this because they are stupid 248 00:10:16,486 --> 00:10:17,906 and they're intellectually uninteresting 249 00:10:18,136 --> 00:10:20,876 so just accept the fact that this is what 250 00:10:20,876 --> 00:10:23,746 at least initially some languages are like. 251 00:10:23,746 --> 00:10:25,576 Well, what about more interesting constructs, 252 00:10:25,646 --> 00:10:26,176 conditions. 253 00:10:26,226 --> 00:10:29,786 If X is less than Y last week we wanted to say something. 254 00:10:30,086 --> 00:10:31,986 Alt we wanted to say something else. 255 00:10:32,226 --> 00:10:35,366 Well, what looks like this nested puzzle piece construction 256 00:10:35,366 --> 00:10:37,486 at left this week onward is going to look 257 00:10:37,486 --> 00:10:38,516 like the stuff at right. 258 00:10:38,516 --> 00:10:40,216 And again it's getting a little messier 259 00:10:40,216 --> 00:10:42,436 but it's still basic building blocks, 260 00:10:42,476 --> 00:10:46,416 if space open parenthesis, X less than Y close parentheses, 261 00:10:46,416 --> 00:10:49,086 curly braces, essentially represent what scratch does 262 00:10:49,086 --> 00:10:51,786 graphically by embracing puzzle pieces similarly 263 00:10:51,786 --> 00:10:55,236 to embrace chunks or lines of code with these curly braces. 264 00:10:55,546 --> 00:10:57,376 Print up is again formatted print. 265 00:10:57,416 --> 00:11:00,246 We've got some familiar quotes now, backslash and semicolon. 266 00:11:00,286 --> 00:11:02,026 So already this is old hat. 267 00:11:02,026 --> 00:11:04,866 Else if now we have a second branch here, else, 268 00:11:05,036 --> 00:11:07,806 a third branch, and notice again the symmetry throughout 269 00:11:07,806 --> 00:11:08,276 this code. 270 00:11:08,276 --> 00:11:11,396 If we open a curly brace we close it and things seem 271 00:11:11,396 --> 00:11:12,976 to be done symmetrically. 272 00:11:13,036 --> 00:11:14,486 So that will become important as well. 273 00:11:14,876 --> 00:11:16,936 So loops, we saw things like forever 274 00:11:16,936 --> 00:11:18,346 and repeat some number of times. 275 00:11:18,596 --> 00:11:20,616 Well, these two look a little weird at first glance 276 00:11:20,666 --> 00:11:24,216 but at the top right there we have while 1 and this is kind 277 00:11:24,216 --> 00:11:27,286 of a stupid loop in a sense because while 1, 278 00:11:27,286 --> 00:11:28,076 well what does that mean? 279 00:11:28,076 --> 00:11:30,746 Well, 1 is synonymous so far as we've seen 280 00:11:30,746 --> 00:11:32,036 with the notion of true. 281 00:11:32,266 --> 00:11:36,186 So while true what does this suggest is going to happen 282 00:11:36,236 --> 00:11:38,776 with that loop at top right? 283 00:11:38,886 --> 00:11:41,506 It's going to go forever, right because true is true, 284 00:11:41,506 --> 00:11:42,916 it's not going to change. 285 00:11:43,006 --> 00:11:44,526 One is not a variable, it's not X, 286 00:11:44,736 --> 00:11:46,526 it's not Y. It's not foo it's not far. 287 00:11:46,526 --> 00:11:49,796 It's not some placeholder, it is literally a value of truth 288 00:11:50,116 --> 00:11:52,956 so this is an intentional infinite loop. 289 00:11:53,236 --> 00:11:55,306 Now probably not the best use 290 00:11:55,306 --> 00:11:57,506 of an infinite loop here unless you literally want 291 00:11:57,506 --> 00:11:59,836 to write a program that when double-clicked or run 292 00:11:59,836 --> 00:12:04,716 and a prompt just says ohi ad nauseum forever, 293 00:12:04,716 --> 00:12:06,956 literally until the cord is pulled from the computer. 294 00:12:06,956 --> 00:12:10,416 So sometimes we'll see infinitive loops are good 295 00:12:10,416 --> 00:12:12,786 but infinite loops if you can somehow get out of them 296 00:12:12,786 --> 00:12:15,296 and the only way to get out of this is to do the equivalent 297 00:12:15,296 --> 00:12:17,146 in Linux will see of force quitting 298 00:12:17,146 --> 00:12:18,236 that particular program. 299 00:12:18,236 --> 00:12:20,686 And to be clear, these are not real programs, 300 00:12:20,966 --> 00:12:22,566 just as the left-hand blocks don't have 301 00:12:22,566 --> 00:12:24,796 that when green flag clicked block 302 00:12:24,796 --> 00:12:26,706 which started a script last week, 303 00:12:27,036 --> 00:12:28,496 these are not complete programs, 304 00:12:28,496 --> 00:12:29,786 these are just snippets thus far. 305 00:12:29,786 --> 00:12:31,046 We'll see what we need to wrap them 306 00:12:31,046 --> 00:12:32,356 in to make them real programs. 307 00:12:32,596 --> 00:12:34,416 Now bottom right's a little more cryptic still 308 00:12:34,416 --> 00:12:36,906 but it's a pattern you're going to get very familiar 309 00:12:36,906 --> 00:12:38,266 with because it's terribly useful. 310 00:12:38,446 --> 00:12:40,666 Four int I gets 0. 311 00:12:40,666 --> 00:12:43,896 So this says declare variable called I, initialize it to 0. 312 00:12:44,186 --> 00:12:48,536 Do the following so long as I is less than 10, 313 00:12:48,536 --> 00:12:49,716 so that middle piece there 314 00:12:49,716 --> 00:12:52,676 between the two semicolons says it's a condition, 315 00:12:52,996 --> 00:12:56,006 so it's similar in spirit to what's in parentheses up above, 316 00:12:56,006 --> 00:12:57,786 uninteresting though that parenthetical was, 317 00:12:58,056 --> 00:13:01,396 so while I is less than 10, do the following 318 00:13:01,446 --> 00:13:03,846 and then you recall from last week what plus plus tends 319 00:13:03,846 --> 00:13:04,306 to denote? 320 00:13:05,406 --> 00:13:08,746 Increment, so the effect in English of this bottom loop is 321 00:13:08,746 --> 00:13:10,116 to do what in layman's terms? 322 00:13:10,566 --> 00:13:13,606 Print ohi 10 times. 323 00:13:14,556 --> 00:13:16,016 Right. It's not an interesting program, 324 00:13:16,016 --> 00:13:19,236 but again what's otherwise cryptic looking syntax just gets 325 00:13:19,236 --> 00:13:20,496 a very simple job done. 326 00:13:20,496 --> 00:13:22,706 And so that will become now a new building block. 327 00:13:22,706 --> 00:13:23,856 Well what about variables? 328 00:13:23,856 --> 00:13:25,926 Well, this snippet of scratch code 329 00:13:25,926 --> 00:13:28,316 at top declares a variable called counter. 330 00:13:28,316 --> 00:13:29,766 It initializes it to zero 331 00:13:30,006 --> 00:13:33,136 and then apparently this scratch code forever said the value 332 00:13:33,136 --> 00:13:35,606 of counter and this little cartoon bubble on the screen 333 00:13:35,606 --> 00:13:37,396 and then it changed counter by one 334 00:13:37,396 --> 00:13:39,576 and we said last week that's the equivalent of plus plus. 335 00:13:39,886 --> 00:13:43,396 So that loop up there, that scratch construct has the effect 336 00:13:43,776 --> 00:13:45,646 of counting from 0 on up. 337 00:13:45,716 --> 00:13:47,706 Well, how do we implement that same idea in C? 338 00:13:47,706 --> 00:13:49,216 Well, it's going to look a little something like this. 339 00:13:49,306 --> 00:13:54,136 To declare a variable in C we shall see will be a relatively 340 00:13:54,176 --> 00:13:56,296 simple saying what kind of variable do you want? 341 00:13:56,536 --> 00:13:58,516 I want an integer, aka int. 342 00:13:58,816 --> 00:14:00,756 what do you want to give its name as? 343 00:14:00,756 --> 00:14:04,266 Counter. What do you want to assign it a default value of? 344 00:14:04,926 --> 00:14:07,226 Gets 0 so = 0 semicolon. 345 00:14:07,306 --> 00:14:09,926 Declares a variable called counter, it says I'm going 346 00:14:09,926 --> 00:14:11,786 to put an int inside of this thing, what Int? 347 00:14:11,786 --> 00:14:13,976 I'm going to put 0 inside of it initially. 348 00:14:14,216 --> 00:14:17,196 Now this loop we saw before while 1 means do this ad 349 00:14:17,196 --> 00:14:20,076 infinitum, print F. Now this is a little interesting 350 00:14:20,076 --> 00:14:23,876 and we'll look at more of this today and on Friday, percent D, 351 00:14:24,536 --> 00:14:26,946 this is what the F is in Print F. 352 00:14:27,166 --> 00:14:30,146 When we say Print F it means print a formatted string, 353 00:14:30,476 --> 00:14:32,036 well what does it mean to format a string? 354 00:14:32,286 --> 00:14:35,016 It means to tell the computer display this value somewhat 355 00:14:35,016 --> 00:14:37,466 differently based on what type of value it is. 356 00:14:37,726 --> 00:14:39,436 Now this is kind of an uninteresting case 357 00:14:39,436 --> 00:14:40,516 because what do we want to print? 358 00:14:40,856 --> 00:14:43,076 Just that value of counter, because I want 359 00:14:43,076 --> 00:14:45,486 to essentially implement the scratch thing up above, 360 00:14:45,776 --> 00:14:49,516 so percent D henceforth, decimal stands for integer. 361 00:14:49,666 --> 00:14:50,896 Put in integer here. 362 00:14:51,136 --> 00:14:51,866 What integer? 363 00:14:52,106 --> 00:14:55,756 Well, put whatever integer is inside of the variable 364 00:14:55,756 --> 00:14:57,246 that comes after the comma. 365 00:14:57,596 --> 00:14:59,056 So again, basic building blocks. 366 00:14:59,396 --> 00:15:01,466 You're certainly welcome to take little notes on things like this 367 00:15:01,466 --> 00:15:02,926 but it will recur so frequently 368 00:15:03,146 --> 00:15:05,086 that this will very quickly become old hat. 369 00:15:05,086 --> 00:15:08,146 And finally counter plus plus, and that simply means increment 370 00:15:08,146 --> 00:15:09,466 that counter and do it again. 371 00:15:09,706 --> 00:15:11,056 So we have scratch, we have C, 372 00:15:11,286 --> 00:15:12,786 functionally they're exactly the same. 373 00:15:12,786 --> 00:15:15,056 I think you'll find in week zero and for PSET 0, 374 00:15:15,236 --> 00:15:17,056 just a lot more fun to get something up 375 00:15:17,056 --> 00:15:20,396 and running using the approach up top, but you're going to run, 376 00:15:20,516 --> 00:15:23,806 some of you, into sort of ceilings with scratch, 377 00:15:23,806 --> 00:15:25,436 things you just can't really do 378 00:15:25,436 --> 00:15:27,486 because it's not expressive enough of language. 379 00:15:27,686 --> 00:15:29,986 We'll be able to do much more powerful things with C, 380 00:15:30,336 --> 00:15:32,936 much more powerful things with PHP and with java script, 381 00:15:33,236 --> 00:15:35,906 so we are on, as you shall see, an upward trajectory. 382 00:15:36,046 --> 00:15:37,846 And finally we have this thing called an inventory. 383 00:15:37,846 --> 00:15:41,266 What program was it last week for which I used an inventory? 384 00:15:41,266 --> 00:15:44,846 Yeah, it was that RPG, the role-playing game 385 00:15:44,846 --> 00:15:46,636 where I moved the little guy up, down, left, 386 00:15:46,636 --> 00:15:48,656 right and collected fruit and then presented it 387 00:15:48,656 --> 00:15:49,606 to the guy at the counter. 388 00:15:49,606 --> 00:15:54,026 Well, I was putting fruit into an array, aka a vector, 389 00:15:54,026 --> 00:15:56,876 aka a list, although technically we'll see this semester those 390 00:15:56,876 --> 00:15:59,296 actually have different useful meanings. 391 00:15:59,456 --> 00:16:02,016 In scratch we just have a variable called inventory. 392 00:16:02,016 --> 00:16:05,676 We can add a string, like orange, and when I say string, 393 00:16:05,676 --> 00:16:08,676 incidentally, this is computer science speak for a sequence 394 00:16:08,676 --> 00:16:11,866 of characters that represent a word, a sentence, whatever. 395 00:16:12,576 --> 00:16:14,426 So that's going to be represented in C, 396 00:16:14,426 --> 00:16:18,566 perhaps most cryptically still is char star inventory, 397 00:16:18,686 --> 00:16:21,386 open bracket size closed bracket semicolon. 398 00:16:21,386 --> 00:16:23,836 And we won't spend much time on arrays this week, 399 00:16:24,106 --> 00:16:27,156 but in C because you're so close to the hardware, so to speak, 400 00:16:27,226 --> 00:16:29,586 because you have so much control over RAM 401 00:16:29,586 --> 00:16:31,166 and where the computer puts stuff, 402 00:16:31,576 --> 00:16:34,136 you have to be more explicit in C than in scratch. 403 00:16:34,136 --> 00:16:37,136 You have to say I want an array of what size? 404 00:16:37,356 --> 00:16:39,286 Well, you tell the computer by way 405 00:16:39,286 --> 00:16:42,676 of this bracket notation how big you want the array to be, 406 00:16:42,886 --> 00:16:46,276 so size is actually a variable for some constant value 407 00:16:46,496 --> 00:16:48,906 that must be defined somewhere else in my program. 408 00:16:48,906 --> 00:16:50,046 It's simply not on screen. 409 00:16:50,326 --> 00:16:53,976 But then when you want to put something at a specific location 410 00:16:53,976 --> 00:16:56,666 in an array, the IFE [sounds like] location, we'll say, 411 00:16:56,666 --> 00:16:58,116 I'll stop using finger quotes today 412 00:16:58,116 --> 00:16:59,846 because all these terms are new, I'm sure. 413 00:16:59,846 --> 00:17:04,206 So the IFE location in inventory is expressed 414 00:17:04,206 --> 00:17:08,416 with inventory bracket I closed bracket gets or equals 415 00:17:08,586 --> 00:17:10,716 and then whatever value I want to put in there. 416 00:17:10,986 --> 00:17:14,956 and now notice I'm trying even today, this day zero of week 1 417 00:17:14,956 --> 00:17:18,406 to be careful with terms like equals because we'll find 418 00:17:18,406 --> 00:17:20,236 that this equals sign that you're familiar 419 00:17:20,236 --> 00:17:23,546 with from algebra in most programming languages is not 420 00:17:23,546 --> 00:17:26,116 equal, it's not equality, it's assignment. 421 00:17:26,476 --> 00:17:28,936 It means take what's on the left and put it inside 422 00:17:28,936 --> 00:17:30,036 of the thing on the left. 423 00:17:30,266 --> 00:17:32,796 Wait, take what's on the right and put it inside 424 00:17:32,796 --> 00:17:34,016 of the thing on the left. 425 00:17:34,176 --> 00:17:35,916 And that's why we've seen this pattern array. 426 00:17:36,226 --> 00:17:37,766 So this begs an interesting question 427 00:17:37,766 --> 00:17:39,526 and invites an interesting bug. 428 00:17:39,826 --> 00:17:41,526 How do you compare two values? 429 00:17:41,646 --> 00:17:43,376 Is X equal to Y? 430 00:17:43,666 --> 00:17:46,356 Well, you can't say X equals Y because that would seem 431 00:17:46,356 --> 00:17:47,666 to put Y's value in X, 432 00:17:47,806 --> 00:17:50,606 so hopefully there will be a solution to that. 433 00:17:51,416 --> 00:17:56,646 So with that said, here comes C. when you write a C program, 434 00:17:56,696 --> 00:17:59,946 as I did on the fly on my Mac last week, recall that I opened 435 00:17:59,946 --> 00:18:02,496 that like terminal window, it was this little blinking cursor. 436 00:18:02,646 --> 00:18:05,696 I quickly whipped up the code, the source code, 437 00:18:05,696 --> 00:18:07,706 at top left there, and then I ran a command. 438 00:18:08,116 --> 00:18:11,786 Well, trivia what was that command? 439 00:18:12,306 --> 00:18:14,936 GCC. So this is a term you will not forget after term's end 440 00:18:14,936 --> 00:18:16,396 because you're going to be running it every day 441 00:18:16,756 --> 00:18:17,856 that you're tackling problems. 442 00:18:17,856 --> 00:18:20,176 That's GCC, is a compiler. 443 00:18:20,586 --> 00:18:21,946 A compiler is a program 444 00:18:22,016 --> 00:18:23,636 that takes something called source code 445 00:18:23,866 --> 00:18:25,896 and turns it into object code. 446 00:18:26,016 --> 00:18:26,816 Now what does that mean? 447 00:18:27,076 --> 00:18:29,646 Well, nicely enough this course is not 448 00:18:29,706 --> 00:18:31,886 about writing stuff like this. 449 00:18:32,246 --> 00:18:34,016 There was a time and there were -- 450 00:18:34,016 --> 00:18:36,556 there are people still roaming this campus who had 451 00:18:36,556 --> 00:18:40,746 to essentially write programs at the level of zeros and ones, 452 00:18:40,746 --> 00:18:42,576 or if you've heard of this thing called a punch card, 453 00:18:42,576 --> 00:18:44,816 very similar in spirit, and there was a time 454 00:18:44,816 --> 00:18:48,026 when the science center was a center of science. 455 00:18:48,026 --> 00:18:49,826 You would go into the basement, you would hand 456 00:18:49,826 --> 00:18:54,096 in your punch cards and then the person running the mainframe 457 00:18:54,096 --> 00:18:55,006 computer in the basement 458 00:18:55,006 --> 00:18:57,496 of the science center would essentially run your homework 459 00:18:57,496 --> 00:19:00,236 for you overnight, because it would usually take a while, 460 00:19:00,236 --> 00:19:03,426 by literally putting a stack of cards somewhere into a machine. 461 00:19:03,736 --> 00:19:05,816 So not the most efficient process, and I've talked 462 00:19:05,816 --> 00:19:10,356 to current faculty who grew up this way on a campus, 463 00:19:10,586 --> 00:19:14,416 not the best time of day to find out that your program is buggy, 464 00:19:14,556 --> 00:19:15,976 it has some mistake in it, right? 465 00:19:15,976 --> 00:19:17,146 Because that's probably happening 466 00:19:17,146 --> 00:19:19,646 at 9 a.m. the next morning when you go back to the center 467 00:19:19,646 --> 00:19:21,996 of science and pick up the results of your program 468 00:19:22,176 --> 00:19:23,676 and it's just one big error message. 469 00:19:23,676 --> 00:19:26,676 So fortunately we've come a long way, and all of your fighting 470 00:19:26,676 --> 00:19:29,426 with bugs and implementing cool things will happen much more 471 00:19:29,426 --> 00:19:32,316 interactively within seconds in front of your own computer 472 00:19:32,316 --> 00:19:35,696 or the lab computer, but the process is essentially still 473 00:19:35,696 --> 00:19:36,126 the same. 474 00:19:36,216 --> 00:19:39,236 At the end of the day a computer, whether it's a PC 475 00:19:39,236 --> 00:19:41,016 or a Mac or whatever you guys own at home, 476 00:19:41,326 --> 00:19:43,596 it only understands 0s and 1s. 477 00:19:43,596 --> 00:19:46,126 Now the world has done some really clever things with 0s 478 00:19:46,126 --> 00:19:49,626 and 1s but you still have to turn your programs into 0s 479 00:19:49,626 --> 00:19:51,966 and 1s and a compiler does exactly that. 480 00:19:52,176 --> 00:19:54,476 So you'll be writing stuff that looks like this at top left. 481 00:19:54,786 --> 00:19:57,416 You'll run that file, which is really just a text file, 482 00:19:57,506 --> 00:19:58,916 through a program called a compiler, 483 00:19:59,116 --> 00:20:01,556 and that compiler will take care of the task of turning it 484 00:20:01,556 --> 00:20:03,976 into 0s and 1s like this. 485 00:20:04,886 --> 00:20:06,016 So where do you do this? 486 00:20:06,016 --> 00:20:10,116 Well, last week I went ahead and ran this program, 487 00:20:10,116 --> 00:20:12,136 so just a quick show of hands then we'll have the data 488 00:20:12,136 --> 00:20:13,856 for sure, how many of you have Macs in the room? 489 00:20:14,036 --> 00:20:17,106 All right, so that's actually statistically kind of a lot, 490 00:20:17,556 --> 00:20:19,446 so it looks like more than half of you 491 00:20:19,446 --> 00:20:21,266 in the room this semester, and that's pretty consistent 492 00:20:21,266 --> 00:20:24,626 with terms past, what's neat is that all the stuff we're going 493 00:20:24,626 --> 00:20:25,926 to start doing this week and beyond, 494 00:20:26,176 --> 00:20:28,706 you actually could have been doing on your own computers, 495 00:20:28,706 --> 00:20:30,656 your own Macs, this whole time. 496 00:20:30,656 --> 00:20:33,156 Now terminal is not a program that's usually 497 00:20:33,386 --> 00:20:35,676 in the little dock here as it is in mine 498 00:20:35,676 --> 00:20:38,006 because most users don't need to know or care 499 00:20:38,006 --> 00:20:39,016 about this little terminal. 500 00:20:39,276 --> 00:20:40,796 But you may at least know anecdotally 501 00:20:40,796 --> 00:20:42,986 that Mac OS these days is based 502 00:20:42,986 --> 00:20:44,576 on an operating system called UNIX. 503 00:20:44,796 --> 00:20:48,626 And back in the day UNIX was essentially a command line 504 00:20:48,686 --> 00:20:51,096 interface, CLI is the buzzword there, 505 00:20:51,096 --> 00:20:54,356 and this means you don't double click on icons to run programs, 506 00:20:54,586 --> 00:20:57,106 you don't go to pretty menus that have sub menus 507 00:20:57,106 --> 00:20:59,806 and graphical icons and all of this fanciness that developed 508 00:20:59,806 --> 00:21:02,186 over the years, you if you want to run a program, 509 00:21:02,396 --> 00:21:04,796 you type the name of the program and hit enter. 510 00:21:05,026 --> 00:21:07,726 And if you want to influence the behavior of that program, 511 00:21:07,726 --> 00:21:11,216 you provide it with commands line argument, words or numbers 512 00:21:11,486 --> 00:21:13,736 after the program's name, and then hit enter. 513 00:21:13,736 --> 00:21:16,246 If you've ever seen DOS on a computer back home, 514 00:21:16,416 --> 00:21:17,746 it's very similar in spirit. 515 00:21:17,826 --> 00:21:20,636 Usually a black screen, white text, and you can't do much 516 00:21:20,706 --> 00:21:23,926 of interest with it unless you know what's there underneath. 517 00:21:23,926 --> 00:21:26,226 So you're not going to write for the most part programs 518 00:21:26,226 --> 00:21:29,746 in this course on your own Macs or on your own PCs per se, 519 00:21:29,966 --> 00:21:34,346 because it's quite hard for us to standardize the configuration 520 00:21:34,346 --> 00:21:36,176 of 300 plus people's laptops. 521 00:21:36,406 --> 00:21:38,276 So instead what we do is provide everyone 522 00:21:38,276 --> 00:21:40,676 with a uniform environment throughout the semester. 523 00:21:40,926 --> 00:21:42,936 Initially this is a cluster 524 00:21:43,146 --> 00:21:48,036 of servers called nice.fas.harvard.edu. 525 00:21:48,106 --> 00:21:50,246 And this nice cluster does so happen to live 526 00:21:50,246 --> 00:21:52,306 in this very idyllic building here. 527 00:21:52,646 --> 00:21:54,246 So it's in the basement, unfortunately, 528 00:21:54,246 --> 00:21:55,206 the cluster of servers. 529 00:21:55,206 --> 00:21:57,216 It's also a bit of a misnomer 530 00:21:57,216 --> 00:22:01,366 because NICE new instructional computing environments has been 531 00:22:01,446 --> 00:22:05,486 NICE for about 10 years now, and there isn't even an old version 532 00:22:05,486 --> 00:22:07,846 so they've kind of painted themselves into a corner here. 533 00:22:07,906 --> 00:22:10,006 We're using the same NICE cluster as always. 534 00:22:10,006 --> 00:22:13,416 So nice.fas.harvard.edu, it's not a website, 535 00:22:13,416 --> 00:22:15,906 it's not something you type into a browser, but it's going 536 00:22:15,906 --> 00:22:18,966 to be the name of a server that you access by way 537 00:22:18,966 --> 00:22:21,766 of your own Mac or by way of your own PC. 538 00:22:21,766 --> 00:22:22,686 Well, what do I mean by that? 539 00:22:23,086 --> 00:22:26,216 well, a lot of you these days have college.harvard.edu 540 00:22:26,216 --> 00:22:28,776 addresses, but you also have the so-called FAS accounts, 541 00:22:28,776 --> 00:22:31,756 and these are the user names and passwords you use to login 542 00:22:31,756 --> 00:22:34,276 to the basement of the science center, kiosks and all of that, 543 00:22:34,396 --> 00:22:36,186 frankly it will be a few more years, I think, 544 00:22:36,186 --> 00:22:38,336 before the university converges on just one account. 545 00:22:38,586 --> 00:22:40,326 But for now you have these accounts, though they're 546 00:22:40,326 --> 00:22:41,536 of decreasing utility. 547 00:22:41,926 --> 00:22:44,256 But for this course at least at the start 548 00:22:44,256 --> 00:22:48,446 of the semester we do acquaint you with nice.fas.harvard.edu 549 00:22:48,446 --> 00:22:49,316 because what it means is 550 00:22:49,316 --> 00:22:52,846 that after the semester you'll actually still have a Linux 551 00:22:52,846 --> 00:22:56,276 server environment on which you can run and write code, 552 00:22:56,276 --> 00:22:58,896 whether it's for an economic class in the further 553 00:22:58,896 --> 00:23:00,646 or a chemistry project or anything 554 00:23:00,646 --> 00:23:03,326 where you need the ability to write programs and you're not 555 00:23:03,376 --> 00:23:05,636 in CS-50 anymore, you'll at least have this resource 556 00:23:05,636 --> 00:23:08,046 for some number for a good amount of time. 557 00:23:08,416 --> 00:23:10,656 What we will start using mid-semester, though, 558 00:23:10,826 --> 00:23:12,896 and this is the extent of my Photoshop ability, 559 00:23:13,316 --> 00:23:15,596 we will use the so-called Cloud. 560 00:23:15,686 --> 00:23:17,786 So this is another one of these buzzwords these days, 561 00:23:17,836 --> 00:23:20,316 Cloud computing refers to the ability 562 00:23:20,316 --> 00:23:23,636 to rent server space somewhere out there on the Internet, 563 00:23:23,636 --> 00:23:27,236 you don't know or care who or where the servers are 564 00:23:27,236 --> 00:23:28,676 or who is running these servers. 565 00:23:28,956 --> 00:23:31,176 So we're essentially going to implement this same idea. 566 00:23:31,176 --> 00:23:33,356 CS-50 has its own cluster of servers now 567 00:23:33,356 --> 00:23:35,876 in the computer science building called Maxwell Dworkin 568 00:23:35,876 --> 00:23:36,096 [sounds like]. 569 00:23:36,096 --> 00:23:38,426 We've got a couple of really big Dell servers 570 00:23:38,426 --> 00:23:41,596 that have a whole lot of CPUs, 32 gigabytes of RAM inside 571 00:23:41,596 --> 00:23:43,446 of them, 6 terabytes of storage space. 572 00:23:43,866 --> 00:23:46,346 So we can hold our own against HCS here. 573 00:23:46,516 --> 00:23:47,466 We have a couple of those. 574 00:23:47,466 --> 00:23:49,866 We have an Apple X Serve and long story short all 575 00:23:49,866 --> 00:23:52,336 of this stuff is wired together in such a way 576 00:23:52,336 --> 00:23:54,666 that soon this semester you will have your own accounts 577 00:23:54,976 --> 00:23:59,976 on cloud.cs50.net, and what this will mean is you'll start using 578 00:23:59,976 --> 00:24:02,596 at some point this cluster instead of nice.fas 579 00:24:03,166 --> 00:24:04,376 but it's really the same thing. 580 00:24:04,376 --> 00:24:05,586 Now what do I mean by cluster? 581 00:24:05,936 --> 00:24:08,356 In the basement of the science center, and also in the basement 582 00:24:08,356 --> 00:24:10,396 of Maxwell Dworkin, there's a bunch of computers. 583 00:24:10,466 --> 00:24:12,766 They're all networked together using Ethernet cables 584 00:24:12,766 --> 00:24:13,816 into a thing called a switch. 585 00:24:14,106 --> 00:24:18,866 They are exposed publicly on the Internet by way of IP address 586 00:24:18,866 --> 00:24:21,596 or these names, nice.fas.harvard.edu, 587 00:24:22,056 --> 00:24:25,316 and there is this illusion that when you connect to one 588 00:24:25,316 --> 00:24:27,466 of these servers you're connecting to just one, 589 00:24:27,656 --> 00:24:29,886 but in fact there's like six computers 590 00:24:29,916 --> 00:24:32,896 in this nice.fas cluster, but they've set them up in 591 00:24:32,896 --> 00:24:34,856 such a way that you the user don't know or care 592 00:24:34,856 --> 00:24:35,946 which particular one you're 593 00:24:35,946 --> 00:24:39,646 on because your files are all visible on any one 594 00:24:39,646 --> 00:24:41,006 of those machines and all 595 00:24:41,006 --> 00:24:43,046 of the commands are identical across the same. 596 00:24:43,326 --> 00:24:46,746 So when it comes time to tackle PSET 1, what you're going to do 597 00:24:46,746 --> 00:24:48,736 if you're a Mac person is something like this, 598 00:24:48,946 --> 00:24:50,516 I just ran the terminal program, 599 00:24:50,936 --> 00:24:52,576 I'm going to type a command called SSH. 600 00:24:52,666 --> 00:24:54,786 This stands for Secure Shell, 601 00:24:54,786 --> 00:24:57,226 and this just means make an encrypted connection 602 00:24:57,226 --> 00:25:00,646 between my laptop, wherever I am in the world, and this server. 603 00:25:00,836 --> 00:25:04,546 And so what I'm going to do is type SSH.nice let's do Malan 604 00:25:04,646 --> 00:25:07,426 at nice.fas.harvard.edu. 605 00:25:07,426 --> 00:25:10,966 I'm going to get a prompt then, looks like I am indeed connected 606 00:25:10,966 --> 00:25:12,266 to Harvard University server. 607 00:25:12,306 --> 00:25:13,646 It's asking me for a password. 608 00:25:13,816 --> 00:25:16,436 Fortunately it won't show you on the screen what the password is 609 00:25:16,496 --> 00:25:18,576 but now I'm at the so-called blinking prompt 610 00:25:18,846 --> 00:25:20,056 that these days no longer blinks 611 00:25:20,276 --> 00:25:21,906 but a blinking prompt it once was. 612 00:25:22,246 --> 00:25:24,096 So I apparently have three unread messages, 613 00:25:24,196 --> 00:25:26,496 which has been some time since I never check this email 614 00:25:26,536 --> 00:25:27,296 account anymore. 615 00:25:27,556 --> 00:25:29,316 So Malan at nice and then there's this thing 616 00:25:29,316 --> 00:25:30,066 in parentheses. 617 00:25:30,206 --> 00:25:32,426 Well this is the command line, so to speak, 618 00:25:32,426 --> 00:25:34,216 where you can type anything you want. 619 00:25:34,536 --> 00:25:36,956 Well, that command was found. 620 00:25:37,566 --> 00:25:40,046 And this is where initially we're going to be writing things 621 00:25:40,086 --> 00:25:42,406 like C source code and compiling it. 622 00:25:42,806 --> 00:25:44,496 So what does that mean exactly? 623 00:25:44,496 --> 00:25:46,536 Well I'm going to do the following just 624 00:25:46,536 --> 00:25:48,396 to give you a sense of some basics and all 625 00:25:48,396 --> 00:25:52,666 of this realize will be repeated in problem set 1 own PDF. 626 00:25:52,826 --> 00:25:55,266 So your hand will be held certainly early in the semester 627 00:25:55,376 --> 00:25:56,956 so you needn't try to memorize or scribble 628 00:25:56,956 --> 00:25:58,836 down all these commands but just to give you a sense 629 00:25:58,836 --> 00:26:01,326 of what we're doing here, if I type the command LS, 630 00:26:01,576 --> 00:26:04,816 this is list, this means list the files in this directory, 631 00:26:04,816 --> 00:26:06,816 this folder, well there's nothing there. 632 00:26:07,096 --> 00:26:09,756 So when I'm at this prompt it's equivalent conceptually 633 00:26:09,756 --> 00:26:12,816 to being inside of a folder on your own Mac or your own PC, 634 00:26:12,936 --> 00:26:15,266 it's just graphically very underwhelming. 635 00:26:15,266 --> 00:26:16,866 And it's more mechanical at the keyboard 636 00:26:16,866 --> 00:26:18,046 than it is mouse oriented. 637 00:26:18,286 --> 00:26:21,246 If I want to make a directory called CS-50 for the duration 638 00:26:21,246 --> 00:26:24,556 of the semester, MKDIR is make directory. 639 00:26:24,836 --> 00:26:27,006 Back in the day people didn't like to write long commands, 640 00:26:27,106 --> 00:26:30,306 pretty reasonable, so it's just MKDIR CS-50 hit enter, 641 00:26:30,536 --> 00:26:34,686 nothing seems to happen but if I type LS now for list notice aha, 642 00:26:34,686 --> 00:26:36,336 there's a directory as implied 643 00:26:36,336 --> 00:26:38,476 by the slash there, that's called CS-50. 644 00:26:38,756 --> 00:26:41,176 Well, I can't, unfortunately, it's a little blue 645 00:26:41,176 --> 00:26:44,576 on this screen here but that indeed says CS-50 if I type LS. 646 00:26:45,186 --> 00:26:48,596 Notice that I can't, and this is kind of a neophyte mistake, 647 00:26:48,596 --> 00:26:51,236 can't really double click and nothing really happens there 648 00:26:51,396 --> 00:26:56,136 so you have to change directory CD into CS-50 and then hit enter 649 00:26:56,376 --> 00:26:58,926 and now my parentheses, this is just a special feature 650 00:26:58,926 --> 00:27:01,266 of the way we've configured your accounts, you'll see. 651 00:27:01,536 --> 00:27:03,786 This just reminds you where you are, which is nice 652 00:27:03,786 --> 00:27:06,116 when you start to have lots of folders open. 653 00:27:06,456 --> 00:27:09,996 Now if I type in LS here there's nothing here, so if I want 654 00:27:09,996 --> 00:27:13,106 to write my first program I need to open a text editor 655 00:27:13,106 --> 00:27:16,266 and the text editor that we'll encourage most students 656 00:27:16,266 --> 00:27:18,336 to use is this little program called NANO. 657 00:27:18,706 --> 00:27:22,366 If you've ever used notepad on PCs or text edit on Macs, 658 00:27:22,766 --> 00:27:25,586 this is like the most user friendly text editor you can 659 00:27:25,586 --> 00:27:27,996 find but as such it's also lacking a number 660 00:27:27,996 --> 00:27:29,856 of useful features as some of you 661 00:27:29,856 --> 00:27:31,116 with more experience will see. 662 00:27:31,376 --> 00:27:34,456 So let me go ahead and write hello.c. Notice 663 00:27:34,526 --> 00:27:36,866 that I have just a simple window. 664 00:27:37,056 --> 00:27:38,466 It's a little cryptic at the bottom, 665 00:27:38,466 --> 00:27:40,166 but again this is just stupid stuff 666 00:27:40,166 --> 00:27:41,946 that once you know it you kind of never forget. 667 00:27:42,146 --> 00:27:44,356 The carrot symbol means hold the control key, 668 00:27:44,626 --> 00:27:45,856 so we'll get to that in a second. 669 00:27:45,856 --> 00:27:48,246 But let me go each and recreate really quickly what I did 670 00:27:48,246 --> 00:27:48,816 last week. 671 00:27:49,096 --> 00:27:51,266 So I type this cryptic sequence of characters, 672 00:27:51,266 --> 00:27:55,706 include STDIO.h in angle brackets. 673 00:27:56,056 --> 00:28:00,936 I then set into main ent arg C char star, 674 00:28:02,156 --> 00:28:07,196 delete key is not working, we've got to fix that. 675 00:28:07,716 --> 00:28:09,626 Well, that's interesting. 676 00:28:11,786 --> 00:28:14,326 Wow. All right, I'm going to do this again 677 00:28:14,326 --> 00:28:15,736 without making any mistakes. 678 00:28:16,496 --> 00:28:23,556 Standardio.h ent main ent arg C char star, here it comes, 679 00:28:23,606 --> 00:28:28,916 arg v bracket parentheses open curly brace 1234 Print F 680 00:28:28,916 --> 00:28:31,486 and I'm just going to say hello, world. 681 00:28:31,486 --> 00:28:34,386 I'm going to make it look pretty at the end with the backslash n, 682 00:28:34,536 --> 00:28:37,126 closed parentheses, semicolon, close. 683 00:28:37,126 --> 00:28:39,996 Okay. we're not going to indent properly here we go, 684 00:28:41,086 --> 00:28:41,526 closed brace. 685 00:28:41,656 --> 00:28:43,076 So that's my first C program, 686 00:28:43,076 --> 00:28:45,246 and this is the thing we completely underwhelmed you 687 00:28:45,246 --> 00:28:45,956 with last week. 688 00:28:45,956 --> 00:28:47,396 But what does this mean I'm going to do now? 689 00:28:47,396 --> 00:28:49,106 I'm going to hit control X 690 00:28:49,176 --> 00:28:51,686 because down here it said carrot X for exit 691 00:28:52,016 --> 00:28:53,966 so now it's saying save modified buffer. 692 00:28:54,146 --> 00:28:55,626 This is the un-user friendly way 693 00:28:55,626 --> 00:28:57,356 of saying do you want to save your changes? 694 00:28:57,576 --> 00:28:58,636 I hit Y for yes. 695 00:28:58,746 --> 00:29:01,326 It then checks what name do you want to give this file? 696 00:29:01,326 --> 00:29:03,086 Again it's just reminding me what it's already called 697 00:29:03,086 --> 00:29:03,986 in case I want to change it. 698 00:29:03,986 --> 00:29:06,596 I hit enter and voila, I'm back at the prompt. 699 00:29:06,806 --> 00:29:09,446 But if I now type LS what should I see? 700 00:29:10,196 --> 00:29:12,656 Hopefully hello.c and this time it's all white 701 00:29:12,726 --> 00:29:15,346 because it's not a directory so now I need to run this thing, 702 00:29:15,566 --> 00:29:19,056 so I could try doing hello.c or again the newbie mistake 703 00:29:19,056 --> 00:29:21,296 of really that's not going to do anything, 704 00:29:21,616 --> 00:29:23,726 hit enter, permission denied. 705 00:29:23,936 --> 00:29:26,316 But that makes sense because what's inside this file? 706 00:29:27,086 --> 00:29:30,396 This is not 0s and 1s clearly, this is just source code. 707 00:29:30,396 --> 00:29:33,136 So source code is the human readable form of this stuff 708 00:29:33,286 --> 00:29:36,816 but I remember from last week GCC hello.c. Let me go ahead 709 00:29:36,816 --> 00:29:40,666 and hit enter now, nothing seems to happen, but wait, LS. 710 00:29:40,956 --> 00:29:41,486 Interesting. 711 00:29:41,916 --> 00:29:45,446 Again foolishly named default name for a program anything 712 00:29:45,446 --> 00:29:48,026 like hello.exe would seem a little more reasonable, 713 00:29:48,256 --> 00:29:50,586 but a.out is sort of the standard but we'll be able 714 00:29:50,586 --> 00:29:51,756 to override that soon. 715 00:29:52,056 --> 00:29:55,836 So a.out has an asterisks which just means I am executable. 716 00:29:55,836 --> 00:29:57,436 I have 0s and 1s inside of me. 717 00:29:57,436 --> 00:30:01,076 In fact if I get a little adventurous and now open a.out 718 00:30:01,076 --> 00:30:03,596 with nano, nano being a text editor, 719 00:30:03,596 --> 00:30:07,106 a.out being a binary file, binary meaning just 0s 720 00:30:07,106 --> 00:30:10,526 and 1s inside, so that's the program I wrote. 721 00:30:10,656 --> 00:30:13,676 Now here's too where you can do very bad things very easily. 722 00:30:13,676 --> 00:30:16,556 If you so much as hit the space bar accidentally 723 00:30:16,556 --> 00:30:18,496 as you're flailing about in the basement 724 00:30:18,496 --> 00:30:19,236 of the science center trying 725 00:30:19,236 --> 00:30:20,946 to fix whatever problem you've just created, 726 00:30:21,186 --> 00:30:23,936 you just inserted aski characters, 727 00:30:23,936 --> 00:30:26,556 random text into what's otherwise a binary file. 728 00:30:26,806 --> 00:30:28,466 Why does this look so cryptic? 729 00:30:28,746 --> 00:30:30,406 Well, nano is again a text editor. 730 00:30:30,406 --> 00:30:34,466 It doesn't know -- it's not supposed to show you 0s and 1s. 731 00:30:34,696 --> 00:30:37,646 It's supposed to show you the letters and the numbers 732 00:30:37,646 --> 00:30:40,296 and the punctuation that those 0s and 1s represent. 733 00:30:40,566 --> 00:30:42,916 So when you see all this crazy stuff on the screen, 734 00:30:43,136 --> 00:30:45,276 that's just nano's misinterpretation 735 00:30:45,476 --> 00:30:48,796 of what's really 0s and 1s, not necessarily aski letters. 736 00:30:49,006 --> 00:30:51,086 Fortunately control X will save me here, 737 00:30:51,086 --> 00:30:53,096 save modified buffer, no this time. 738 00:30:53,356 --> 00:30:55,236 So I hit N, I go back to the prompt 739 00:30:55,416 --> 00:30:58,776 and now I can run a.out just by typing this command here 740 00:30:58,776 --> 00:31:02,446 and in fact that's my first repeated C program. 741 00:31:02,806 --> 00:31:03,356 So that's it. 742 00:31:03,356 --> 00:31:05,606 that's the basic building block of writing a C program. 743 00:31:05,666 --> 00:31:08,706 Hopefully we can do much more interesting things than that 744 00:31:09,146 --> 00:31:10,826 but that covers the basic workflow. 745 00:31:10,826 --> 00:31:12,766 Now just to give you a sense that what we're doing 746 00:31:12,766 --> 00:31:16,336 in this course is ultimately pretty much operating system 747 00:31:16,336 --> 00:31:20,246 agnostic, here we have Mac, here we have PC, running Mac OS, 748 00:31:20,246 --> 00:31:23,246 running Windows, on the Windows world it's not quite as easy 749 00:31:23,246 --> 00:31:25,886 as going to a terminal window because Windows does not ship 750 00:31:25,966 --> 00:31:28,616 with that little program called SSH 751 00:31:28,616 --> 00:31:31,366 with which I created the secure connection, but there's plenty 752 00:31:31,366 --> 00:31:32,776 of free tools that do that. 753 00:31:33,166 --> 00:31:33,866 One thing you'll see 754 00:31:33,866 --> 00:31:35,876 on the course's website is program called putty. 755 00:31:35,966 --> 00:31:38,666 So if you're a PC user you run a program 756 00:31:38,666 --> 00:31:41,306 like this it's a little more user friendly if overwhelming 757 00:31:41,306 --> 00:31:42,296 with all of its options. 758 00:31:42,546 --> 00:31:44,846 What I'm simply going to do here is say, you know what, 759 00:31:44,846 --> 00:31:48,196 I want to connect to nice.Fas.harvard.edu. 760 00:31:48,466 --> 00:31:50,666 so if I were doing this for the first time I would simply do 761 00:31:50,666 --> 00:31:54,066 something like that, typing in its host name or its IP address. 762 00:31:54,066 --> 00:31:55,696 Now I'm going to go ahead and click -- 763 00:31:55,866 --> 00:31:57,666 actually I'm going to load something there 764 00:31:57,716 --> 00:31:58,806 so I get the bigger font size. 765 00:31:58,806 --> 00:32:02,296 I'm going to click open and now I get this black window again. 766 00:32:02,296 --> 00:32:03,566 I'm going to log in as Malan. 767 00:32:03,696 --> 00:32:07,526 I get a similar prompt, enter, notice I'm back in here. 768 00:32:07,526 --> 00:32:10,606 if I type LS there's CS-50, CD CS-50, aha, 769 00:32:10,606 --> 00:32:11,996 there's my a.out program. 770 00:32:12,536 --> 00:32:13,386 Same exact thing. 771 00:32:13,586 --> 00:32:14,906 So this is actually powerful. 772 00:32:14,906 --> 00:32:17,096 Even though I'm on a PC here, a Mac over there, 773 00:32:17,216 --> 00:32:20,466 I've accessed the same account so that's one of the upsides 774 00:32:20,466 --> 00:32:21,746 of having this central server. 775 00:32:21,746 --> 00:32:23,856 You'll be able to access your code anywhere. 776 00:32:23,856 --> 00:32:26,116 If you go home on break, if you get sick, if you're just hanging 777 00:32:26,116 --> 00:32:28,566 out a friend's you can access your code in this course 778 00:32:28,896 --> 00:32:31,106 from any computer on the Internet and you'll find 779 00:32:31,266 --> 00:32:33,676 that for better or for worse to be at least useful. 780 00:32:33,856 --> 00:32:35,876 So just to give you a sense of what else you can do 781 00:32:36,126 --> 00:32:40,136 on a typical Linux server, so the machine I connected to, 782 00:32:40,166 --> 00:32:42,016 nice.fas, it's running at Linux, 783 00:32:42,336 --> 00:32:45,306 which is again an operating system that's larger text-based 784 00:32:45,716 --> 00:32:49,246 but there are goes, graphical user interfaces, 785 00:32:49,426 --> 00:32:51,006 and in fact if you headed to the basement 786 00:32:51,006 --> 00:32:53,206 of the science center someday as you might for office hours, 787 00:32:53,466 --> 00:32:56,196 they've got like 33 percent Linux computers 788 00:32:56,196 --> 00:32:56,976 in the basement there. 789 00:32:57,256 --> 00:32:59,326 they do have Windows, they do have menus, 790 00:32:59,376 --> 00:33:01,306 things you can click, because the world has come 791 00:33:01,306 --> 00:33:03,596 up with more user-friendly interfaces to Linux. 792 00:33:03,826 --> 00:33:05,786 But you'll get your hands directly this semester with some 793 00:33:05,786 --> 00:33:08,636 of the building blocks because frankly when you get comfortable 794 00:33:08,636 --> 00:33:12,026 with this stuff you can do so much more, so much more easily 795 00:33:12,026 --> 00:33:13,756 and so much more efficiently just 796 00:33:13,756 --> 00:33:16,216 by telling the computer precisely what you want. 797 00:33:16,216 --> 00:33:19,226 So CD we saw, CP, any guess? 798 00:33:19,836 --> 00:33:23,416 CP, copy, kind of the shorthand version of copy, 799 00:33:23,416 --> 00:33:26,276 and in fact it is, CP we'll use to copy code this semester. 800 00:33:26,276 --> 00:33:32,186 LS is list, MK-Dir is make directory, MV, move, 801 00:33:32,186 --> 00:33:35,826 so that's synonymous, we'll see renaming a file, 802 00:33:35,826 --> 00:33:39,846 so to rename a file you'll say MV space the original name space 803 00:33:39,936 --> 00:33:43,206 the new name, not terribly hard. 804 00:33:43,896 --> 00:33:45,856 PWD. Yeah, so print working directory. 805 00:33:45,856 --> 00:33:48,726 What this means here is I'm back on the nice.fas server. 806 00:33:48,906 --> 00:33:50,946 If I kind of get a little confused sometime 807 00:33:50,946 --> 00:33:55,596 and I type PWD it will tell me the full path to where I am 808 00:33:55,596 --> 00:33:58,446 on the server, so right now I'm logged in as Malan 809 00:33:58,486 --> 00:34:01,096 and Malan does not have access to everything on the server 810 00:34:01,096 --> 00:34:04,156 because there's like 20,000 other people with fas accounts. 811 00:34:04,486 --> 00:34:07,096 So in fact, fas took the approach years ago, 812 00:34:07,096 --> 00:34:08,066 very intelligently, 813 00:34:08,256 --> 00:34:10,196 of organizing things hierarchically. 814 00:34:10,456 --> 00:34:11,936 So notice there's a directory, 815 00:34:11,976 --> 00:34:14,716 the root of the hard drive apparently has a directory 816 00:34:14,716 --> 00:34:17,616 in it called NFS, network file system, home, 817 00:34:17,616 --> 00:34:19,826 which is where everyone's home directories live. 818 00:34:19,826 --> 00:34:22,066 And a home directory is just your personal storage space. 819 00:34:22,586 --> 00:34:25,756 M/A just looks like there's a whole hierarchy of names there 820 00:34:25,756 --> 00:34:28,536 to keep things clean, slash CS-50, this is the full path 821 00:34:28,866 --> 00:34:32,516 to where I am, and realize that I can definitely go poking 822 00:34:32,516 --> 00:34:33,616 around if I get curious. 823 00:34:33,616 --> 00:34:34,716 This is not a bad thing. 824 00:34:34,716 --> 00:34:39,216 If I type CD DOT DOT it doesn't go this way it goes backwards 825 00:34:39,216 --> 00:34:41,846 so to speak or higher up in this tree, so now notice I'm back 826 00:34:41,956 --> 00:34:43,326 in my so-called home directory. 827 00:34:43,676 --> 00:34:46,126 Where am I going to be if I do it once more? 828 00:34:47,496 --> 00:34:49,316 Yeah, the directory called A. So with all 829 00:34:49,316 --> 00:34:52,976 of the people whose usernames start with MA, 830 00:34:52,976 --> 00:34:56,616 which if I do CD it's getting a little curious here. 831 00:34:56,866 --> 00:34:59,186 Maybe will this backfire? 832 00:34:59,456 --> 00:35:01,546 Maybe if we could put the camera down here for just a moment 833 00:35:01,546 --> 00:35:03,306 so that we're not podcasting this to the Internet, 834 00:35:03,556 --> 00:35:07,056 these are all of the people on fas.harvard.edu whose names, 835 00:35:07,146 --> 00:35:10,206 sorry folks at home can't see this, that begin with MA. 836 00:35:10,406 --> 00:35:14,066 If we go back one more directory keep the camera underwhelming 837 00:35:14,216 --> 00:35:15,796 down here, there's the whole alphabet. 838 00:35:16,036 --> 00:35:17,376 We can see people that start 839 00:35:17,376 --> 00:35:22,326 with probably no MZs I'm guessing, CDZ, LS, oh there are. 840 00:35:22,326 --> 00:35:25,026 So it's not a full name, it's first letter 841 00:35:25,136 --> 00:35:26,826 of the first name, last name. 842 00:35:26,826 --> 00:35:29,646 So again you can poke around all you want and see a whole bunch 843 00:35:29,646 --> 00:35:31,366 of stuff on this computer, but Linux, 844 00:35:31,366 --> 00:35:33,626 like most operating systems, has various permissions 845 00:35:33,626 --> 00:35:37,086 and fancy control features so you really can't do bad things. 846 00:35:37,086 --> 00:35:39,726 you can't access ,for instance, if I get nosy, 847 00:35:39,726 --> 00:35:44,496 let's say home/a/y idida 848 00:35:44,496 --> 00:35:46,006 for Jansue's [sounds like] home directory. 849 00:35:46,816 --> 00:35:55,346 Let me, I'm forgetting the day, so I'm in it. 850 00:35:55,796 --> 00:35:58,086 Thankfully, permission denied. 851 00:35:58,086 --> 00:36:00,076 So things are, in fact, configured correctly. 852 00:36:00,306 --> 00:36:01,826 So again just random little trivia 853 00:36:01,826 --> 00:36:03,246 that ultimately gets useful 854 00:36:03,246 --> 00:36:05,276 because just understanding the world 855 00:36:05,276 --> 00:36:07,366 in which you're playing I think lets you get yourself 856 00:36:07,366 --> 00:36:09,786 out of problems and actually solve more interesting ones. 857 00:36:10,026 --> 00:36:11,746 So there are some other commands that we'll see, 858 00:36:11,746 --> 00:36:14,176 and again every piece that's pdf will walk you 859 00:36:14,236 --> 00:36:15,366 through any of this minutiae. 860 00:36:15,576 --> 00:36:17,066 You're not going to have to figure it out or figure 861 00:36:17,066 --> 00:36:20,446 out from week 1 what the heck was the command for this. 862 00:36:20,446 --> 00:36:23,846 So we're going to see things like GCC, GDB, 863 00:36:24,046 --> 00:36:26,996 commands like more and then someone got clever. 864 00:36:26,996 --> 00:36:28,076 More is just a program 865 00:36:28,076 --> 00:36:29,686 that shows you the contents of a file. 866 00:36:29,906 --> 00:36:31,606 Well, someone wanted to be clever and kind of one 867 00:36:31,606 --> 00:36:33,306 up that person so they'd called their program less 868 00:36:33,626 --> 00:36:35,366 but it does really the same thing. 869 00:36:35,686 --> 00:36:39,056 Man for manual, which is something you'll get familiar 870 00:36:39,056 --> 00:36:39,936 with as well. 871 00:36:40,176 --> 00:36:42,576 So what does it take to write a program, so it's three steps 872 00:36:42,576 --> 00:36:43,346 and we've done it twice. 873 00:36:43,346 --> 00:36:46,546 Last week and this, one, open up a text editor like nano 874 00:36:46,676 --> 00:36:49,046 in a Linux environment and call the file whatever, 875 00:36:49,146 --> 00:36:53,866 hi1.c or hello1.c, using the dot C convention for namesake. 876 00:36:54,236 --> 00:36:56,806 GCC the name of the file and then you run the thing 877 00:36:56,886 --> 00:36:59,516 by simply running the default name. 878 00:36:59,906 --> 00:37:02,046 Well, what if we want to do something more interesting, 879 00:37:02,046 --> 00:37:04,866 give it a non-stupid name like a.out? 880 00:37:05,226 --> 00:37:07,636 So there are again these things called command line arguments 881 00:37:07,676 --> 00:37:10,736 or switches with which you can control the behavior 882 00:37:10,736 --> 00:37:12,586 of code more interestingly. 883 00:37:12,586 --> 00:37:15,736 So if I go back over here into my CS-50 account, 884 00:37:16,166 --> 00:37:19,526 my CS-50 directory, here's hello.c. my goal now is 885 00:37:19,526 --> 00:37:22,416 to create a more interesting name than a.out, 886 00:37:22,776 --> 00:37:25,856 so per that previous slide I say dash 0 for output. 887 00:37:26,156 --> 00:37:27,816 Let's call the program hello 888 00:37:28,026 --> 00:37:31,286 and then let's say compile this particular program, LS. 889 00:37:31,286 --> 00:37:35,346 Now I have a binary, 0s and 1s called hello up. 890 00:37:35,726 --> 00:37:39,796 So you'll notice little keystrokes that save you time. 891 00:37:39,796 --> 00:37:42,526 If you hit tab it will do auto complete which saves you time. 892 00:37:42,776 --> 00:37:44,766 Unfortunately it also reveals all the other commands 893 00:37:44,766 --> 00:37:47,926 on the server which start with he, in this case head, hello, 894 00:37:47,926 --> 00:37:50,316 help desk, help desk server, help z tags and [inaudible], 895 00:37:50,546 --> 00:37:54,636 none of which I care about so hello, enter, 896 00:37:54,766 --> 00:37:56,996 same exact program even though it's called something 897 00:37:56,996 --> 00:37:57,606 different still. 898 00:37:57,606 --> 00:38:00,366 So more interesting but again just a fundamentally 899 00:38:00,366 --> 00:38:01,586 uninteresting building block. 900 00:38:01,736 --> 00:38:04,056 But your programs are going to get more complicated over time. 901 00:38:04,296 --> 00:38:06,846 Most of your programs by semester's end are not going 902 00:38:06,846 --> 00:38:09,456 to be this big in a simple text file, 903 00:38:09,736 --> 00:38:11,196 they're going to be multiple files. 904 00:38:11,196 --> 00:38:12,896 And they're not going to be terribly long each, 905 00:38:12,896 --> 00:38:14,936 but we'll see one of the principals we'll preach 906 00:38:14,936 --> 00:38:17,316 in the course is design, and good design, 907 00:38:17,546 --> 00:38:20,116 and as some of you may have experienced in scratch already, 908 00:38:20,746 --> 00:38:23,266 if you're implementing a script that's kind of this big 909 00:38:23,266 --> 00:38:25,106 and then you add some more features and then it's this big 910 00:38:25,106 --> 00:38:27,566 and then this big and this big and God forbid this wide 911 00:38:27,566 --> 00:38:30,646 and wide and wide because you keep adding ifs and its and ifs 912 00:38:30,646 --> 00:38:33,706 and its you're probably crossing the boundary at some point 913 00:38:33,706 --> 00:38:35,596 of what we'd call good design. 914 00:38:35,596 --> 00:38:37,486 If you're scrolling up, down, left right just 915 00:38:37,486 --> 00:38:40,416 to see your own program there's probably a better approach. 916 00:38:40,416 --> 00:38:43,256 Now for PSET 0, they don't need to worry so much about this 917 00:38:43,256 --> 00:38:45,336 because we haven't really told you what good design is, 918 00:38:45,636 --> 00:38:48,596 but among the features will be chopping up your code 919 00:38:48,596 --> 00:38:50,936 into much more manageable bite-sized pieces, 920 00:38:50,986 --> 00:38:53,146 putting it in separate files, putting it in scratch 921 00:38:53,146 --> 00:38:55,436 in separate scripts, and you'll see that a command 922 00:38:55,436 --> 00:38:57,626 that will facilitate this is called make. 923 00:38:57,886 --> 00:39:00,316 So you'll see both of these approaches in PSET 1, 924 00:39:00,566 --> 00:39:03,766 simply typing Make and then the name of the program you want 925 00:39:03,826 --> 00:39:05,876 to make, in this case, hello. 926 00:39:05,876 --> 00:39:08,966 Notice I'm not typing DOT C, I'm saying make hello. 927 00:39:09,236 --> 00:39:11,636 Make is a program that's going to infer, well, 928 00:39:11,636 --> 00:39:13,806 if this guy wants to make a program called make it's 929 00:39:13,856 --> 00:39:16,216 probably based on a file called make.c 930 00:39:16,476 --> 00:39:20,566 so if I hit enter it tried to recompile it but it realized -- 931 00:39:20,566 --> 00:39:22,646 and this is what's cool about make -- it realized, oh, 932 00:39:22,686 --> 00:39:23,776 you haven't changed the program, 933 00:39:23,776 --> 00:39:27,316 why should I waste my time regenerated identical 0s and 1s. 934 00:39:27,626 --> 00:39:29,046 So in fact it didn't bother. 935 00:39:29,226 --> 00:39:33,016 But if I do LS to see my files how do I remove hello and a.out? 936 00:39:34,146 --> 00:39:37,916 Yeah, rm, hello, enter, it's going to ask for a sanity check. 937 00:39:37,916 --> 00:39:39,786 Remove regular file hello question mark? 938 00:39:39,786 --> 00:39:42,826 Yes. RM a.out, yes for that too. 939 00:39:42,826 --> 00:39:46,426 So now I just have my source code, make hello enter, 940 00:39:46,646 --> 00:39:49,496 so the command's going to look a little more cryptic because we, 941 00:39:49,496 --> 00:39:51,206 just to get you started at term start, 942 00:39:51,296 --> 00:39:53,086 are configuring these accounts in a way 943 00:39:53,086 --> 00:39:54,666 that just makes some things easier 944 00:39:54,666 --> 00:39:56,666 and we'll take those training wheels off over time, 945 00:39:56,936 --> 00:39:58,806 but it achieves the same results because I now 946 00:39:58,806 --> 00:40:02,296 in fact have a program called hello in that directory as well. 947 00:40:02,616 --> 00:40:04,606 So what are these programs you can use? 948 00:40:04,606 --> 00:40:07,686 So nano is by far the simplest editor you can use 949 00:40:07,686 --> 00:40:08,786 at least at term start. 950 00:40:09,026 --> 00:40:11,596 On my computer it doesn't support the delete feature 951 00:40:11,596 --> 00:40:14,016 but we will fix that so that you don't need 952 00:40:14,016 --> 00:40:17,486 to fail the very first time you make a program like I did, 953 00:40:17,746 --> 00:40:19,116 but there are other tools out there. 954 00:40:19,116 --> 00:40:21,646 Those of you might know programs like VIM and in fact most 955 00:40:21,646 --> 00:40:24,446 of the time I'll use a program called VIM just 956 00:40:24,446 --> 00:40:26,516 because that's what I was taught in CS-50 years ago. 957 00:40:26,516 --> 00:40:27,556 I kind of grew up on it. 958 00:40:27,766 --> 00:40:30,816 EMAX is another popular one, but more on this in the future. 959 00:40:30,816 --> 00:40:33,446 I mention it now only so that you don't get confused 960 00:40:33,446 --> 00:40:35,776 or puzzled when you see this TF used this program, 961 00:40:35,936 --> 00:40:36,736 this TF used another. 962 00:40:37,016 --> 00:40:40,126 It's just like using pages versus Microsoft Word 963 00:40:40,126 --> 00:40:42,936 versus Word Pad, it's all the same kind of stuff 964 00:40:42,936 --> 00:40:43,946 in this world as well. 965 00:40:44,256 --> 00:40:45,366 So in a moment we're going 966 00:40:45,436 --> 00:40:47,236 to actually tease apart what it means 967 00:40:47,236 --> 00:40:49,626 to write a more interesting program than hello world. 968 00:40:49,626 --> 00:40:52,756 The basic building block for almost any C program is going 969 00:40:52,756 --> 00:40:55,766 to start with this construct here, but why don't we go ahead 970 00:40:55,766 --> 00:40:56,896 since this is a lot to digest, 971 00:40:57,096 --> 00:40:58,326 and take a five minute break first. 972 00:41:05,486 --> 00:41:06,456 All right. 973 00:41:06,876 --> 00:41:08,826 We are back, so Yuki [assumed spelling] 974 00:41:08,826 --> 00:41:09,966 and Jantsu [assumed spelling] informed me 975 00:41:09,966 --> 00:41:12,186 that sectioning will indeed debut this evening, 976 00:41:12,186 --> 00:41:13,846 go to CS-50.net, take a look 977 00:41:13,846 --> 00:41:15,966 at any instructions you'll find on the home page. 978 00:41:15,966 --> 00:41:18,076 Check around midnight or certainly by morning, 979 00:41:18,416 --> 00:41:21,026 and note this, so per last week's announcements 980 00:41:21,026 --> 00:41:23,826 in the syllabus we do offer three tracks of sections, 981 00:41:23,826 --> 00:41:27,146 just like First Nights does for our students who are more 982 00:41:27,146 --> 00:41:29,566 and less comfortable with music, so we do the same. 983 00:41:29,566 --> 00:41:32,136 It's just the idea of being in CS-50, 984 00:41:32,386 --> 00:41:34,106 so there's no steadfast rule 985 00:41:34,106 --> 00:41:36,056 as to what makes you less comfortable, 986 00:41:36,056 --> 00:41:37,316 what makes you more comfortable 987 00:41:37,316 --> 00:41:38,746 and what makes you somewhere in between. 988 00:41:39,036 --> 00:41:41,036 To be honest, it's a term you can probably just slap 989 00:41:41,036 --> 00:41:43,606 on yourself based on just how comfortable you actually are 990 00:41:43,826 --> 00:41:46,396 having returned this week to CS-50. 991 00:41:46,396 --> 00:41:48,616 You've seen what the percentage break-downs are so we get plenty 992 00:41:48,616 --> 00:41:49,776 of folks from each of the buckets, 993 00:41:50,036 --> 00:41:51,946 so when you use fas's sectioning tool, 994 00:41:52,226 --> 00:41:54,346 realize that because we have these three different types 995 00:41:54,346 --> 00:41:56,206 of sections you're going to see a long list 996 00:41:56,206 --> 00:41:57,616 of about 30 sections, since we have 997 00:41:57,616 --> 00:41:59,756 about 30 teaching fellows and thus 30 sections. 998 00:41:59,976 --> 00:42:01,336 and in the comments fields 999 00:42:01,336 --> 00:42:03,116 of the sectioning program you'll see 1000 00:42:03,116 --> 00:42:05,086 if that section slot has been designated 1001 00:42:05,086 --> 00:42:06,946 for those more comfortable, those less 1002 00:42:07,246 --> 00:42:08,586 or those somewhere in between. 1003 00:42:08,846 --> 00:42:09,776 If you're on the fence as to 1004 00:42:09,776 --> 00:42:12,476 where you belong you cannot go wrong being 1005 00:42:12,476 --> 00:42:14,016 in the somewhere in between section. 1006 00:42:14,016 --> 00:42:16,146 That's really for folks that just don't feel themselves 1007 00:42:16,146 --> 00:42:19,256 at either extreme, but certainly feel free to chat with me or any 1008 00:42:19,256 --> 00:42:20,576 of the TFs if you're curious 1009 00:42:20,576 --> 00:42:23,336 as to what this all means or where you belong. 1010 00:42:23,336 --> 00:42:25,206 Now what does this mean in real terms for sections? 1011 00:42:25,896 --> 00:42:27,486 Frankly it just means that especially 1012 00:42:27,486 --> 00:42:29,906 for those less comfortable, it just means you're going to be 1013 00:42:29,906 --> 00:42:33,406 in a room full of people who feel as apprehensive 1014 00:42:33,406 --> 00:42:36,656 or as inexperienced as you and it's sort of a safer space 1015 00:42:36,656 --> 00:42:37,826 in which to put up your hand 1016 00:42:37,826 --> 00:42:40,416 and ask a question you think is the proverbial dumb question. 1017 00:42:40,716 --> 00:42:43,106 The sections for those more comfortable are meant 1018 00:42:43,106 --> 00:42:45,956 to digress intentionally from say the course's syllabus, 1019 00:42:46,266 --> 00:42:48,196 from the week's problem set and really go off 1020 00:42:48,196 --> 00:42:50,336 in any interesting technical direction that some 1021 00:42:50,336 --> 00:42:51,716 of the savvier students might 1022 00:42:51,716 --> 00:42:54,186 like to explore even though it's not going to help them per se 1023 00:42:54,356 --> 00:42:55,896 on that particular week's problem set. 1024 00:42:56,036 --> 00:42:58,296 But again if you don't feel yourself needing that sort 1025 00:42:58,296 --> 00:43:00,866 of comfort zone here or you don't really think 1026 00:43:00,916 --> 00:43:02,786 that you'd want to try holding your own with some 1027 00:43:02,786 --> 00:43:04,836 of those more comfortable then the sweet spot, 1028 00:43:04,836 --> 00:43:06,776 just like the standard editions for most students is 1029 00:43:06,776 --> 00:43:08,886 that for those somewhere in between. 1030 00:43:09,276 --> 00:43:12,246 All right, so let's do something a little more interesting now. 1031 00:43:12,246 --> 00:43:15,206 I've gone ahead and per the handout you have I have a whole 1032 00:43:15,206 --> 00:43:17,456 bunch of code already in this account. 1033 00:43:17,456 --> 00:43:19,246 So I'm going to go back to my screen here 1034 00:43:19,476 --> 00:43:21,486 and you'll see again commands lli this on PSETs. 1035 00:43:21,486 --> 00:43:26,066 What I'm going to do for a moment is copy, tilda, CS-50, 1036 00:43:26,276 --> 00:43:27,956 so henceforth the Tilda character 1037 00:43:27,956 --> 00:43:29,306 which on most U.S. keyboards is 1038 00:43:29,306 --> 00:43:33,336 on the top left near the escape key, Tilda CS-50 means go 1039 00:43:33,336 --> 00:43:35,946 into CS-50's home directory. 1040 00:43:36,116 --> 00:43:37,996 Now I'm in Malan's home directory at the moment, 1041 00:43:37,996 --> 00:43:41,916 so go into CS-50 is just like I tried to idida's home directory, 1042 00:43:41,916 --> 00:43:43,976 so go into CS-50's and then I know 1043 00:43:43,976 --> 00:43:46,226 because I'm reading this off of a pdf or whatever 1044 00:43:46,426 --> 00:43:49,526 that there's a pub directory, that there's a source, src, 1045 00:43:49,526 --> 00:43:51,996 again, nicknames in Linux, source directory, 1046 00:43:51,996 --> 00:43:54,536 and then I know there's a lectures directory in there 1047 00:43:54,536 --> 00:43:55,706 and then we're in week 1. 1048 00:43:55,706 --> 00:43:56,966 So it turns out there is, in fact, 1049 00:43:56,966 --> 00:44:00,626 a directory called 1 inside of lectures, inside of source, 1050 00:44:00,626 --> 00:44:04,086 inside of pub, inside of CS-50's home directory and my goal is 1051 00:44:04,086 --> 00:44:08,346 to copy it from there to here because I, now a student 1052 00:44:08,346 --> 00:44:11,146 in the class, decide that I want to push ahead, I want to do this 1053 00:44:11,146 --> 00:44:12,406 after class and play around. 1054 00:44:12,406 --> 00:44:14,106 I want to copy David's source code 1055 00:44:14,106 --> 00:44:16,176 into my own account just so I can play. 1056 00:44:16,176 --> 00:44:20,516 Perfectly fine, notice that DOT represents this directory just 1057 00:44:20,516 --> 00:44:22,946 as DOT DOT represents the parent directory. 1058 00:44:23,156 --> 00:44:24,966 So if hit enter this is actually flawed, 1059 00:44:25,246 --> 00:44:27,536 notice that something bad happened and then 1060 00:44:27,536 --> 00:44:29,726 if I type LS I didn't get any files. 1061 00:44:29,966 --> 00:44:33,286 So realize, too, sort of FAQ early on is if you want 1062 00:44:33,286 --> 00:44:37,396 to copy a directory with CP you need to do it recursively, 1063 00:44:37,436 --> 00:44:39,656 which is a keyword that we'll revisit in the term, 1064 00:44:39,656 --> 00:44:41,596 it means to do it not only to that directory 1065 00:44:41,846 --> 00:44:45,166 but all the files inside and all the subdirectories inside again 1066 00:44:45,166 --> 00:44:46,856 and again and again until you bottom out. 1067 00:44:46,856 --> 00:44:49,846 So CP-R means copy the file recursively 1068 00:44:50,146 --> 00:44:52,836 into my current directory, enter, took a second 1069 00:44:52,836 --> 00:44:54,256 because there are, in fact, a bunch of files. 1070 00:44:54,546 --> 00:44:57,706 In fact if I go into my one directory now here are some 1071 00:44:57,706 --> 00:45:00,056 examples that I've pre-prepared so that we can go off 1072 00:45:00,056 --> 00:45:01,686 in a known direction here. 1073 00:45:01,926 --> 00:45:06,976 So one direction we already went in was hi-1.c. but your version, 1074 00:45:06,976 --> 00:45:08,826 on paper looks a little different from the quick 1075 00:45:08,826 --> 00:45:10,566 and dirty thing I've been whipping up. 1076 00:45:10,676 --> 00:45:14,086 there's a lot more purple on the screen here, so all the stuff 1077 00:45:14,086 --> 00:45:17,796 at top is fundamentally irrelevant to the computer, 1078 00:45:17,916 --> 00:45:20,606 but just like scratch does if you've discovered it yet, 1079 00:45:20,846 --> 00:45:22,466 lets you put little comments here and there, 1080 00:45:22,466 --> 00:45:24,376 sort of sticky notes, so to speak, post-it notes, 1081 00:45:24,646 --> 00:45:26,936 so does C in most any high-level language. 1082 00:45:27,156 --> 00:45:29,086 and by high-level language henceforth I mean something 1083 00:45:29,086 --> 00:45:30,626 that's not 0s and 1s, 1084 00:45:30,626 --> 00:45:32,066 not something called assembly language. 1085 00:45:32,386 --> 00:45:33,846 It means ignore all 1086 00:45:33,846 --> 00:45:36,246 of the following this is useful only to me the human. 1087 00:45:36,606 --> 00:45:39,466 So in C the means by which you write a comment is 1088 00:45:39,496 --> 00:45:43,926 to do a forward slash star and that means anything after that 1089 00:45:43,926 --> 00:45:47,556 on this line or below will be ignored by the compiler, GCC, 1090 00:45:47,726 --> 00:45:50,976 and the comment stops when you hit the opposite 1091 00:45:50,976 --> 00:45:52,286 of that, star slash. 1092 00:45:52,916 --> 00:45:56,196 So the fact that I actually have all of these stars and I've kind 1093 00:45:56,196 --> 00:45:59,206 of boxed things in is really just some OCD on my part. 1094 00:45:59,206 --> 00:46:02,036 It's not technical I just wanted to make a nice pretty purple box 1095 00:46:02,246 --> 00:46:03,466 to box in all of my code. 1096 00:46:03,676 --> 00:46:07,416 But one key takeaway is that I made sure that this line 1097 00:46:07,566 --> 00:46:10,666 of stars and the slashes does not exceed the width 1098 00:46:10,666 --> 00:46:11,346 of my window. 1099 00:46:11,566 --> 00:46:13,866 So another lesson that's important early on is 1100 00:46:13,866 --> 00:46:15,876 to make sure your -- the code you start writing is 1101 00:46:16,186 --> 00:46:17,296 pretty printed. 1102 00:46:17,516 --> 00:46:19,746 And this just means it actually displays nicely 1103 00:46:19,746 --> 00:46:20,906 on a typical window. 1104 00:46:21,126 --> 00:46:24,136 In real terms this means you should never at least 1105 00:46:24,186 --> 00:46:27,546 for the C part of this course, have a line of text 1106 00:46:27,606 --> 00:46:29,556 that is longer than 80 characters long. 1107 00:46:29,776 --> 00:46:32,676 And even that's pushing it, 76 is kind of the geek's rule 1108 00:46:32,676 --> 00:46:34,346 of thumb as to how long a line should be. 1109 00:46:34,646 --> 00:46:37,316 How do you know what's 76 or what's 80 characters? 1110 00:46:37,486 --> 00:46:41,446 Well, right now my window has essentially been expanded to be 1111 00:46:41,636 --> 00:46:45,506 as long as I wanted it to be, but you get into the habit 1112 00:46:45,666 --> 00:46:48,276 of knowing when to hit enter to move things onto the next line 1113 00:46:48,476 --> 00:46:52,356 and nano and programs like it will actually force your lines 1114 00:46:52,356 --> 00:46:54,276 to wrap if they start to get long. 1115 00:46:54,596 --> 00:46:56,036 And this is just a matter of good style, 1116 00:46:56,066 --> 00:46:58,956 because as you could imagine, if I wrote my whole program 1117 00:46:58,956 --> 00:47:00,946 on just one line it would still work, 1118 00:47:00,946 --> 00:47:04,546 the computer would not care, but you the humans probably would. 1119 00:47:04,546 --> 00:47:07,426 In fact, this is hi1.c, let me go ahead 1120 00:47:07,426 --> 00:47:09,036 and delete all that stuff. 1121 00:47:09,086 --> 00:47:11,016 I'm going to go ahead and do this, you know, this, 1122 00:47:11,136 --> 00:47:13,096 I really don't need that there. 1123 00:47:13,096 --> 00:47:15,016 Let me go ahead and put that there. 1124 00:47:15,016 --> 00:47:16,946 Right, if I'd given you a printout of this, I mean, 1125 00:47:16,946 --> 00:47:20,666 rightly so would things start to look pretty damn complicated. 1126 00:47:20,666 --> 00:47:22,216 Now some of these spaces are important 1127 00:47:22,216 --> 00:47:23,646 so that you can distinguish what's ints 1128 00:47:23,646 --> 00:47:26,206 and what's [inaudible], but if I go ahead and save this 1129 00:47:26,206 --> 00:47:31,796 with control X, hit Y and now go ahead and run GCC of hi1.c, 1130 00:47:32,016 --> 00:47:33,796 the program thankfully still compiles. 1131 00:47:33,796 --> 00:47:36,636 I still have eight odd out, so it still works, this version, 1132 00:47:36,906 --> 00:47:39,256 but it's certainly not very pretty printed 1133 00:47:39,256 --> 00:47:40,396 and not very legible. 1134 00:47:40,396 --> 00:47:42,016 So what else is inside of this file? 1135 00:47:42,256 --> 00:47:44,676 Well, hmm, I didn't really want to do that. 1136 00:47:44,676 --> 00:47:48,886 Let me go ahead and go back into CS-50, Pub source lectures 1, 1137 00:47:48,886 --> 00:47:50,196 let me just get a new copy of this. 1138 00:47:50,546 --> 00:47:53,856 So now it's going to prompt me, wait a minute, overwrite hi1.c, 1139 00:47:54,016 --> 00:47:55,366 little sanity check, say yes. 1140 00:47:55,466 --> 00:47:56,856 So now I have the prettier version back. 1141 00:47:56,856 --> 00:47:59,776 I'm going to start using a program called VI or VIM just 1142 00:47:59,776 --> 00:48:01,936 because frankly I'm more comfortable with it than nano, 1143 00:48:01,936 --> 00:48:05,266 even though nano is simpler, do as I say not as I do. 1144 00:48:05,666 --> 00:48:09,526 Hi1.c, still kind of pretty prints everything 1145 00:48:09,526 --> 00:48:10,356 and color codes it. 1146 00:48:10,576 --> 00:48:13,136 And the color is actually a feature that you don't get 1147 00:48:13,136 --> 00:48:16,596 if you trying writing your first programs with notebad.exe 1148 00:48:16,846 --> 00:48:19,236 or other similar client side programs. 1149 00:48:19,476 --> 00:48:22,266 It's just making some inferences about the different roles 1150 00:48:22,266 --> 00:48:23,646 that some of these keywords play, 1151 00:48:23,646 --> 00:48:25,936 so I didn't write the word int in green. 1152 00:48:26,196 --> 00:48:29,096 I didn't write the word char in green, it just knows 1153 00:48:29,136 --> 00:48:31,076 that that represents the type of a variable. 1154 00:48:31,076 --> 00:48:33,016 Let me draw David's attention to it 1155 00:48:33,056 --> 00:48:34,656 with this default color scheme. 1156 00:48:34,986 --> 00:48:36,316 So what's interesting here? 1157 00:48:36,396 --> 00:48:38,916 Well, this stuff at the top is all comments, 1158 00:48:38,916 --> 00:48:40,946 and actually now we get to use a fun little toy. 1159 00:48:40,946 --> 00:48:43,106 this stuff up here is all comments. 1160 00:48:43,236 --> 00:48:43,696 All right. 1161 00:48:43,696 --> 00:48:45,846 So it's fundamentally irrelevant but useful 1162 00:48:45,846 --> 00:48:47,196 for documenting your code. 1163 00:48:47,496 --> 00:48:48,896 This things here I'm going 1164 00:48:48,896 --> 00:48:52,016 to briefly call today a pre-processor directive, 1165 00:48:52,366 --> 00:48:55,416 which is just a fancy way of saying tell the compiler 1166 00:48:55,616 --> 00:48:57,586 to go get the contents of some other file 1167 00:48:57,706 --> 00:48:59,246 that often someone else wrote. 1168 00:48:59,576 --> 00:49:02,966 So what we don't have here is any kind 1169 00:49:02,966 --> 00:49:05,186 of specification of what Print F does. 1170 00:49:05,246 --> 00:49:06,646 Like who wrote Print F? 1171 00:49:06,746 --> 00:49:10,096 I wrote this program called main, but who wrote Print F? 1172 00:49:10,096 --> 00:49:11,716 Right, I'm kind of cutting corners here. 1173 00:49:11,986 --> 00:49:15,566 Well, it turns out that when Linux shipped with the compiler, 1174 00:49:15,806 --> 00:49:18,086 someone else, some really smart people spent a lot 1175 00:49:18,086 --> 00:49:21,706 of time getting this very basic important function implemented 1176 00:49:21,706 --> 00:49:23,206 in their own source code. 1177 00:49:23,446 --> 00:49:29,156 So by saying sharp include open angle bracket standard io.h 1178 00:49:29,246 --> 00:49:32,056 closed bracket, that's just telling GCC, you know what, 1179 00:49:32,186 --> 00:49:34,876 the following code has some functions, some programs 1180 00:49:35,106 --> 00:49:36,066 that other people wrote. 1181 00:49:36,296 --> 00:49:37,976 Those programs happen to be inside 1182 00:49:37,976 --> 00:49:41,666 of a file called standardio.h. please go grab the contents, 1183 00:49:41,926 --> 00:49:44,676 paste them in here so that I don't have to do it myself. 1184 00:49:44,976 --> 00:49:47,136 So I'm kind of oversimplifying there 1185 00:49:47,136 --> 00:49:50,356 because in real terms what we're using here is a library. 1186 00:49:50,426 --> 00:49:53,886 So a library of code henceforth is just a bunch of code 1187 00:49:53,886 --> 00:49:56,976 that someone else wrote or maybe you wrote but that you factored 1188 00:49:56,976 --> 00:49:59,336 out into separate files so that you don't have to copy 1189 00:49:59,336 --> 00:50:01,426 and paste it into every future program you write. 1190 00:50:01,836 --> 00:50:04,566 To technically my white lie I just told is 1191 00:50:04,566 --> 00:50:06,926 that these program Print F these functions, 1192 00:50:06,926 --> 00:50:09,706 actually as they should really be called, are not actually 1193 00:50:09,736 --> 00:50:11,746 in that file, standardio.h, 1194 00:50:12,406 --> 00:50:15,646 inside of that file will eventually see is just a minimal 1195 00:50:15,646 --> 00:50:18,026 amount of information, essentially a summary of all 1196 00:50:18,026 --> 00:50:20,636 of the functions in that file because as you might imagine, 1197 00:50:20,636 --> 00:50:23,806 where is the actual implementation, the actual lines 1198 00:50:23,806 --> 00:50:25,346 of code that implement Print F? 1199 00:50:25,386 --> 00:50:28,726 What would the file name probably be called? 1200 00:50:29,496 --> 00:50:33,336 Standardio.c, so in fact, yeah, there is a file out somewhere 1201 00:50:33,336 --> 00:50:35,486 on the system called standardio.c 1202 00:50:35,486 --> 00:50:38,206 that someone else implemented but there's a lot of lines 1203 00:50:38,206 --> 00:50:40,256 of code in there, a normal human doesn't want to read through it 1204 00:50:40,256 --> 00:50:43,016 so we have these header files which are called .h files 1205 00:50:43,266 --> 00:50:45,446 and then c files which have actual code 1206 00:50:45,636 --> 00:50:48,196 and initially I'm not making any header files myself, 1207 00:50:48,196 --> 00:50:50,126 we'll get to that eventually but just realize, 1208 00:50:50,126 --> 00:50:52,466 some of these minor tripping points early on are just 1209 00:50:52,466 --> 00:50:54,826 about keeping programs relatively clean, so this, 1210 00:50:54,876 --> 00:50:58,416 to be short, simply says, go get the contents of this file 1211 00:50:58,466 --> 00:51:00,646 so that I can use functions like Print F 1212 00:51:01,216 --> 00:51:02,476 and henceforth let me clarify. 1213 00:51:02,816 --> 00:51:06,256 So I am writing a program, this program is arbitrarily going 1214 00:51:06,256 --> 00:51:09,896 to be called hello, but I can override that as we've seen 1215 00:51:09,896 --> 00:51:11,866 with the -o flag from GCC 1216 00:51:12,266 --> 00:51:15,256 so programs are made up of functions. 1217 00:51:15,256 --> 00:51:17,676 one or more functions and these are kind 1218 00:51:17,676 --> 00:51:19,626 of like miniature programs just 1219 00:51:19,626 --> 00:51:21,856 like your scratch projects are probably made 1220 00:51:21,856 --> 00:51:23,316 up of multiple scripts. 1221 00:51:23,616 --> 00:51:27,036 A script is a function so a function is just a piece of code 1222 00:51:27,036 --> 00:51:29,456 that does something and as you can have multiple scripts 1223 00:51:29,456 --> 00:51:32,506 in scratch you can have multiple functions in c and just 1224 00:51:32,506 --> 00:51:35,866 as the default block or script and scratch is 1225 00:51:35,866 --> 00:51:37,636 that thing called wingreenflag clicked, 1226 00:51:38,066 --> 00:51:40,516 so in c is the default script, 1227 00:51:40,516 --> 00:51:42,456 the default function called main. 1228 00:51:42,886 --> 00:51:46,006 Now why, if its just called main do I need to write something 1229 00:51:46,006 --> 00:51:49,906 like int and then ints and then RGC, char or this asterisks, 1230 00:51:49,906 --> 00:51:52,836 this RGV this bracket notation, there's just a lot of crap 1231 00:51:52,836 --> 00:51:55,156 at the beginning of a program when you're writing it in c, 1232 00:51:55,316 --> 00:51:57,156 those of you who took app computer science know 1233 00:51:57,156 --> 00:52:00,546 that there's classfoo public static, voidmain, its even worse 1234 00:52:00,596 --> 00:52:02,406 in Java and frankly this is just 1235 00:52:02,406 --> 00:52:06,456 because these languages were not designed with week 0 1236 00:52:06,456 --> 00:52:07,916 of a programming class in mind. 1237 00:52:08,056 --> 00:52:10,366 So again, its stupid stuff, you get past it quickly. 1238 00:52:10,546 --> 00:52:13,006 For today we're going to wave our hands at details like that 1239 00:52:13,006 --> 00:52:15,136 but to say for now it's a copy paste thing. 1240 00:52:15,136 --> 00:52:18,166 To start writing a function you literally write int main 1241 00:52:18,166 --> 00:52:20,926 and all the rest of that stuff but then the fun part 1242 00:52:20,926 --> 00:52:23,996 or at least the intellectually interesting part happens 1243 00:52:23,996 --> 00:52:25,536 in between the curly braces for now. 1244 00:52:25,996 --> 00:52:28,406 So that's how I would go about printing this simple program. 1245 00:52:28,406 --> 00:52:31,036 Let me actually take a look at a more interesting version, 1246 00:52:31,036 --> 00:52:33,656 thought only slightly called high2.c 1247 00:52:33,656 --> 00:52:35,586 and you have a printout of this. 1248 00:52:36,166 --> 00:52:39,006 So what's the fundamental difference between version one 1249 00:52:39,116 --> 00:52:41,356 and this one version two? 1250 00:52:41,956 --> 00:52:44,926 Hopefully it jumps out at you. 1251 00:52:44,986 --> 00:52:47,686 yeah, so if bit egotistically this jumps out at you 1252 00:52:47,686 --> 00:52:50,656 that this program has my name immortalized in it, 1253 00:52:50,916 --> 00:52:51,946 but how did I do this? 1254 00:52:51,946 --> 00:52:53,816 Well it looks like I've got this additional line 1255 00:52:53,816 --> 00:52:56,396 and so David is mentioned but what's going on? 1256 00:52:56,396 --> 00:53:00,026 well even just using the jargon I started using earlier, string, 1257 00:53:00,246 --> 00:53:01,586 because it comes before the name 1258 00:53:01,586 --> 00:53:03,606 of a variable, means what precisely? 1259 00:53:03,606 --> 00:53:06,706 Yeah, it's the type of variable. 1260 00:53:06,706 --> 00:53:09,566 So the variable is called main, what's going to be inside 1261 00:53:09,566 --> 00:53:11,626 of this variable, well its not going to be an int, 1262 00:53:11,626 --> 00:53:14,346 because David is not an int, it's a string, it's a word, 1263 00:53:14,346 --> 00:53:16,546 it's a sentence so I'm going to specify that the type 1264 00:53:16,546 --> 00:53:18,046 of this variable is string, 1265 00:53:18,316 --> 00:53:21,176 now the equal sign again is the assignment operator, 1266 00:53:21,176 --> 00:53:23,446 it means put the value of the thing on the right 1267 00:53:23,596 --> 00:53:26,096 on the inside the thing on the left so the value 1268 00:53:26,096 --> 00:53:30,136 on the right is itself a string and in c as in many languages 1269 00:53:30,136 --> 00:53:32,776 if you want to have a literal sequence 1270 00:53:32,776 --> 00:53:34,586 of characters be treated as one unit, 1271 00:53:34,836 --> 00:53:37,266 you put them in all double quotes. 1272 00:53:37,546 --> 00:53:40,906 You can use single quotes but only around single characters, 1273 00:53:40,906 --> 00:53:42,646 so those of you who know a little 1274 00:53:42,646 --> 00:53:44,476 of html probably know it doesn't matter if you use single 1275 00:53:44,476 --> 00:53:45,976 or double, it does matter. 1276 00:53:45,976 --> 00:53:48,186 In many programming languages including c, 1277 00:53:48,396 --> 00:53:50,176 so strings are double quotes. 1278 00:53:50,436 --> 00:53:52,406 All right, then what do I do on the second line? 1279 00:53:52,646 --> 00:53:54,766 Well, we've not quite seen this syntax 1280 00:53:55,466 --> 00:53:58,366 but in laymen's term can someone explain what the percent s 1281 00:53:58,486 --> 00:53:59,996 there represents? 1282 00:54:00,776 --> 00:54:02,956 String. Right, really not that hard. 1283 00:54:02,956 --> 00:54:06,716 Percent d happened to represent int, percent s represents string 1284 00:54:06,946 --> 00:54:09,536 and so the thing after the comma means what do you want 1285 00:54:09,536 --> 00:54:12,686 to substitute in for that formatting code, the percent s, 1286 00:54:13,176 --> 00:54:14,506 put the name of the variable. 1287 00:54:14,556 --> 00:54:20,206 All right, so more interesting, so GCC hi2.c enter, 1288 00:54:20,446 --> 00:54:23,706 now I'm going to run a.out, ohi David 1289 00:54:23,786 --> 00:54:26,906 but unfortunately its really not that interesting 1290 00:54:26,946 --> 00:54:30,276 if your program is completely deterministic and always spits 1291 00:54:30,276 --> 00:54:32,706 out the same result because really, I mean, 1292 00:54:32,706 --> 00:54:35,066 there are so many easier ways to just say something like this 1293 00:54:35,066 --> 00:54:38,366 than this set up so let's take a look at the third version 1294 00:54:38,366 --> 00:54:40,246 of this which is a little more interesting 1295 00:54:40,246 --> 00:54:42,746 and finally we get a compelling use for a keyboard, 1296 00:54:43,046 --> 00:54:46,056 so this program differs for the comments up top 1297 00:54:46,206 --> 00:54:48,296 in that there's a third line of code here 1298 00:54:48,586 --> 00:54:51,396 that uses a new function, so we've been using Print F 1299 00:54:51,546 --> 00:54:53,856 to send output, it turns out that you can also, 1300 00:54:54,176 --> 00:54:56,056 nicely enough, get input from the user. 1301 00:54:56,296 --> 00:54:58,896 So not by way of the mouse, at least in this context but by way 1302 00:54:58,896 --> 00:55:00,906 of the keyboard and what's the new function, 1303 00:55:01,246 --> 00:55:04,216 just to take an obvious guess here, get string. 1304 00:55:04,576 --> 00:55:07,696 As the name implies, the function get strings purpose 1305 00:55:07,696 --> 00:55:10,586 in life is to get a string from the user and do what 1306 00:55:10,586 --> 00:55:12,856 with the result apparently? 1307 00:55:13,026 --> 00:55:16,256 Store it. so functions we now see right away, 1308 00:55:16,256 --> 00:55:19,536 have this ability not only to do something aesthetically, 1309 00:55:19,586 --> 00:55:23,246 Print F has this so called side effect of displaying something 1310 00:55:23,246 --> 00:55:26,516 on the screen but functions can also return a value 1311 00:55:26,516 --> 00:55:29,236 so you can think of a function kind of as a black box 1312 00:55:29,456 --> 00:55:32,316 that does some work and then can maybe hand you some answers 1313 00:55:32,496 --> 00:55:33,926 and its up to you, the programmer, 1314 00:55:33,926 --> 00:55:36,006 to decide if you're going to do anything with that answer. 1315 00:55:36,226 --> 00:55:37,446 How do you do something with it? 1316 00:55:37,446 --> 00:55:39,976 well one of the things you could do is keep the answer around. 1317 00:55:40,556 --> 00:55:42,886 So by having string name on the left 1318 00:55:42,886 --> 00:55:45,596 that says give me a variable called name, it's a type sting, 1319 00:55:45,596 --> 00:55:47,586 what are you going to put inside of it? 1320 00:55:47,746 --> 00:55:49,396 well, whatever the result is. 1321 00:55:49,476 --> 00:55:52,066 The return value so to speak of the thing on the right 1322 00:55:52,166 --> 00:55:54,246 so get string returns now a value. 1323 00:55:54,546 --> 00:55:56,336 How does this all behave in reality? 1324 00:55:56,336 --> 00:56:00,086 So GCC hi3.c, I'm getting a little tired of a.out, 1325 00:56:00,086 --> 00:56:01,286 how do I give this a better name? 1326 00:56:02,656 --> 00:56:07,276 yes, so -o for output, and then let's call it hi3 though I could 1327 00:56:07,276 --> 00:56:13,726 call it anything I want, oh, oh, did I create a problem there? 1328 00:56:13,846 --> 00:56:20,286 its kind of a coincidence, so still the same error, yeah, 1329 00:56:20,286 --> 00:56:24,006 so correct, fancy correct answer so there's a problem here 1330 00:56:24,136 --> 00:56:25,976 in that get string is a function, 1331 00:56:25,976 --> 00:56:28,526 yes but it wasn't written by the original authors 1332 00:56:28,526 --> 00:56:31,976 of the c programming language, in fact just getting input 1333 00:56:31,976 --> 00:56:34,146 from the user in c is really a pain. 1334 00:56:34,146 --> 00:56:36,596 You have to use a function among others called scanf 1335 00:56:36,596 --> 00:56:39,006 and we'll use this eventually, there's other tricks too, 1336 00:56:39,126 --> 00:56:41,196 but long story short, in week one of a course, 1337 00:56:41,196 --> 00:56:44,566 especially this course, to complete a bunch of hurdles, 1338 00:56:44,566 --> 00:56:47,076 you have to jump through as simple as get a character 1339 00:56:47,076 --> 00:56:49,006 from the keyboard, even in Java, those of you familiar 1340 00:56:49,006 --> 00:56:51,046 with the scanner class, even that is a pain 1341 00:56:51,046 --> 00:56:53,766 to actually use I'd say in the first week of a class 1342 00:56:54,256 --> 00:56:56,096 so notice what did I do in this file? 1343 00:56:56,316 --> 00:56:58,946 I did one other thing besides mention get string. 1344 00:56:59,226 --> 00:57:01,696 There's one other key difference outside of the comments. 1345 00:57:02,096 --> 00:57:03,116 What else is new here? 1346 00:57:05,386 --> 00:57:08,246 yeah, so I'm not just using the standard io library, 1347 00:57:08,276 --> 00:57:12,086 it turns out that the cs50 staff a few years ago wrote our 1348 00:57:12,086 --> 00:57:13,296 own library. 1349 00:57:13,466 --> 00:57:15,486 And we wrote what that means, two files. 1350 00:57:15,486 --> 00:57:18,526 One called cs50.h and one called cs50.c 1351 00:57:18,726 --> 00:57:20,506 and in the latter we wrote a bunch of functions 1352 00:57:20,506 --> 00:57:22,156 that we thought would be useful to students 1353 00:57:22,156 --> 00:57:26,746 in the semester namely get int and get string and silly name, 1354 00:57:26,746 --> 00:57:29,296 but get long long but other functions still 1355 00:57:29,476 --> 00:57:32,236 that really simplify the process at least at the start 1356 00:57:32,236 --> 00:57:34,556 of the semester of getting keyboard input from users 1357 00:57:34,826 --> 00:57:37,826 but unfortunately its not sufficient just to tell GCC, 1358 00:57:37,886 --> 00:57:41,466 hey make sure you go get a copy of the header file 1359 00:57:41,466 --> 00:57:43,936 for cs50's library so I know that these functions exist, 1360 00:57:43,936 --> 00:57:44,906 because I've done that. 1361 00:57:44,906 --> 00:57:48,736 So one mistake might have been to omit this line together, 1362 00:57:48,736 --> 00:57:49,636 that would in fact be bad. 1363 00:57:50,256 --> 00:57:51,836 But that's not the only mistake that I made, 1364 00:57:51,836 --> 00:57:54,116 I now need to tell cs50's library 1365 00:57:54,416 --> 00:57:57,516 when you compile this you don't just get away 1366 00:57:57,516 --> 00:58:01,676 with turning one file, hi3.c into 0's and 1's 1367 00:58:01,896 --> 00:58:05,106 because you need access to one other file, called what? 1368 00:58:05,566 --> 00:58:08,626 Yes, so cs50.c so there's a couple solutions here, 1369 00:58:08,626 --> 00:58:10,436 I could email the core staff, and I could say, 1370 00:58:10,436 --> 00:58:14,126 hey could I have a copy of cs50.c or they'll be happy 1371 00:58:14,126 --> 00:58:16,066 to give it because there's no intellectual property there, 1372 00:58:16,066 --> 00:58:19,276 its just standard c stuff, but that seems kind of wasteful 1373 00:58:19,276 --> 00:58:21,326 if every time in the semester if you write a program you need 1374 00:58:21,326 --> 00:58:23,556 to copy the R file, put it in your own directory, 1375 00:58:23,556 --> 00:58:24,706 kind of bloat your own code, 1376 00:58:24,956 --> 00:58:26,446 well in fact there's an easier way. 1377 00:58:26,446 --> 00:58:31,756 You can tell GCC to link against our library 1378 00:58:31,756 --> 00:58:33,406 and long story short is what this mean, 1379 00:58:33,406 --> 00:58:37,106 is when GCC is converting hi3.c from source code 1380 00:58:37,106 --> 00:58:39,646 into object code, from human readable to 0's 1381 00:58:39,676 --> 00:58:43,486 and 1's it means, oh by the way, go get the library, -l, 1382 00:58:43,746 --> 00:58:47,546 called cs50 and merge the two at the very last minute 1383 00:58:47,586 --> 00:58:51,886 so that the file you get out hi3 or a.out contains 0's and 1's 1384 00:58:51,886 --> 00:58:53,256 from both of those files. 1385 00:58:53,506 --> 00:58:56,556 So again, another silly detail that's very easy to forget 1386 00:58:56,796 --> 00:58:58,626 but just to kind of drill it into your minds 1387 00:58:58,626 --> 00:59:00,276 if you ever see this semester, 1388 00:59:00,496 --> 00:59:02,566 a message about undefined references, 1389 00:59:03,046 --> 00:59:05,276 probably means you forgot something simple 1390 00:59:05,276 --> 00:59:08,126 like that library that you wanted to use, -lcs50. 1391 00:59:08,186 --> 00:59:11,216 there's others, -lm gives you a math library. 1392 00:59:11,216 --> 00:59:14,536 There's -lcrypt which gives you a cryptography library and stuff 1393 00:59:14,536 --> 00:59:16,236 like that, but it did work this time. 1394 00:59:16,236 --> 00:59:19,776 when I said go ahead and link in this file, I did in fact get hi3 1395 00:59:19,886 --> 00:59:25,616 so if I run hi3 state your name, we'll say Jensue, 1396 00:59:25,886 --> 00:59:28,096 well hi Jensue, well what about Yuki? 1397 00:59:28,936 --> 00:59:31,926 Okay so now I have, still kind of overwhelming right? 1398 00:59:31,926 --> 00:59:33,556 not anything to write home about 1399 00:59:33,746 --> 00:59:36,026 but at least it's a dynamic program that's starting 1400 00:59:36,026 --> 00:59:39,096 to do a little bit more so just to give you a sense now 1401 00:59:39,096 --> 00:59:42,626 of some formalities, one I've used here, Print F, 1402 00:59:42,706 --> 00:59:45,646 a couple of times, I've used it pretty simply to use things 1403 00:59:45,646 --> 00:59:48,936 like strings and ints but we can do more powerful things with it 1404 00:59:48,936 --> 00:59:51,946 and in fact you'll have to get used to sort of a new sort 1405 00:59:51,946 --> 00:59:54,266 of a world where people write things somewhat cryptically 1406 00:59:54,266 --> 00:59:57,046 sometimes, this is an excerpt from the man page, 1407 00:59:57,046 --> 00:59:59,556 the manual page for Print F and the way you, 1408 00:59:59,556 --> 01:00:00,646 the programmer know, how 1409 01:00:00,646 --> 01:00:03,466 to use a function is often frankly, by rtfm. 1410 01:00:03,746 --> 01:00:04,946 Just read the manual. 1411 01:00:05,266 --> 01:00:07,346 And actually look up what the instructions are, 1412 01:00:07,436 --> 01:00:08,486 Google it later if you'd like. 1413 01:00:08,836 --> 01:00:11,766 For those less comfortable and less familiar with the speak. 1414 01:00:12,576 --> 01:00:13,736 So what you'll find 1415 01:00:13,736 --> 01:00:15,806 on the courses website throughout the semester 1416 01:00:15,806 --> 01:00:17,356 under resources we have a bunch 1417 01:00:17,356 --> 01:00:19,676 of links we think you'll find useful and one 1418 01:00:19,676 --> 01:00:22,716 of them is this wonderful site called cppreference.com. 1419 01:00:22,716 --> 01:00:25,506 the only downside of it means cpp means c plus plus 1420 01:00:25,886 --> 01:00:28,676 so the author of the site actually made it fairly c plus 1421 01:00:28,756 --> 01:00:31,036 plus specific so under resources 1422 01:00:31,036 --> 01:00:34,276 on the courses web sit we have our own copy of this website 1423 01:00:34,276 --> 01:00:37,036 because he open sourced it to everyone so if I actually go 1424 01:00:37,036 --> 01:00:42,456 to cs50.net/resources what you'll see up here 1425 01:00:42,456 --> 01:00:44,686 under the link to c is a bunch of things, 1426 01:00:44,686 --> 01:00:50,226 most of which we won't need just yet, but if I go to reference, 1427 01:00:50,606 --> 01:00:53,686 this is a wonderful useful sight and this is one of the reasons 1428 01:00:53,686 --> 01:00:55,856 that you don't really need a book for a course like this 1429 01:00:56,076 --> 01:00:58,566 because so much good stuff is available on line. 1430 01:00:58,876 --> 01:01:01,006 If I know that I'm doing something with io, 1431 01:01:01,006 --> 01:01:04,336 that sounds cryptic, but it just means input output print get 1432 01:01:04,496 --> 01:01:06,696 stuff like that, notice if I scroll down here, 1433 01:01:06,926 --> 01:01:08,966 clearly we're only scratching the surface today 1434 01:01:08,966 --> 01:01:10,836 of what C can do and I was interested 1435 01:01:10,836 --> 01:01:12,236 in what Print F can do 1436 01:01:12,486 --> 01:01:14,906 and here's just fairly user friendly documentation 1437 01:01:14,906 --> 01:01:18,746 of the other codes you can use so we saw already, let's see, 1438 01:01:18,746 --> 01:01:21,586 percent d and there it is at the top for signed integers, 1439 01:01:21,686 --> 01:01:23,826 things that may or may not have negative sign in front, 1440 01:01:24,026 --> 01:01:25,796 percents for a string of characters 1441 01:01:26,016 --> 01:01:27,886 but there's some other stuff here too that we may 1442 01:01:27,886 --> 01:01:30,086 or may not use in this semester, but what's nice 1443 01:01:30,086 --> 01:01:32,126 about this site is it give you sample code, 1444 01:01:32,306 --> 01:01:35,416 it tells you what library, what header file you must include 1445 01:01:35,416 --> 01:01:36,606 at the top of your program 1446 01:01:36,866 --> 01:01:39,356 and then it tells you what the functions signature is, 1447 01:01:39,586 --> 01:01:41,626 what is sort of the canonical form of this thing 1448 01:01:41,626 --> 01:01:44,036 so you can begin to understand how you use it 1449 01:01:44,036 --> 01:01:46,296 and again you'll get much more acclimated to this 1450 01:01:46,626 --> 01:01:47,866 over the course of the semester 1451 01:01:47,866 --> 01:01:50,266 but we can do more interesting things with this. 1452 01:01:50,556 --> 01:01:53,496 So just as there's n for new line, 1453 01:01:53,796 --> 01:01:57,216 there's r for carriage return, so if you think back 1454 01:01:57,216 --> 01:01:59,676 to retro typewriters when you hit enter 1455 01:01:59,676 --> 01:02:01,056 on an old fashioned typewriter, 1456 01:02:01,286 --> 01:02:03,236 not only did the paper move this way, 1457 01:02:03,236 --> 01:02:06,616 the little typing head moved this way 1458 01:02:06,856 --> 01:02:08,906 so a carriage return moves the curser all the curser all the 1459 01:02:08,906 --> 01:02:11,656 way back to the left, a new line moves it down one line 1460 01:02:12,086 --> 01:02:14,526 so this is another sort of stupid thing 1461 01:02:14,526 --> 01:02:17,836 from religious battles past so in the Windows world, 1462 01:02:17,836 --> 01:02:21,196 most text files end with what's called a carriage return 1463 01:02:21,356 --> 01:02:22,836 and a line feed. 1464 01:02:22,836 --> 01:02:25,346 Crlf, which means rn. 1465 01:02:26,326 --> 01:02:29,036 in the Linux world people were much more conservative, 1466 01:02:29,036 --> 01:02:33,756 they just used new lines and n in many systems, Macs and, 1467 01:02:34,486 --> 01:02:36,266 actually not so much Mac sometimes, 1468 01:02:36,266 --> 01:02:39,546 so in Linux computers n means yes go to the next line 1469 01:02:39,756 --> 01:02:42,556 and go all the way to the left so I'm just kind of grimacing 1470 01:02:42,556 --> 01:02:44,426 at some of these details because these things too, 1471 01:02:44,646 --> 01:02:46,206 are little things you trip over early on. 1472 01:02:46,206 --> 01:02:48,136 they're fundamentally not that interesting but you get used 1473 01:02:48,136 --> 01:02:50,006 to little trivia or minutia like this. 1474 01:02:50,276 --> 01:02:53,936 But this does beg the question, if n means new line, 1475 01:02:54,326 --> 01:02:56,866 r means carriage return, 1476 01:02:57,166 --> 01:03:01,306 how do you just write a literal ? Well the answer is there 1477 01:03:01,306 --> 01:03:03,806 but this is a common pattern, what is it? 1478 01:03:04,776 --> 01:03:07,396 so unfortunately a little weird, you kind of have 1479 01:03:07,446 --> 01:03:09,476 to have a corner case like that if you want a literal 1480 01:03:09,686 --> 01:03:12,536 and a string you escape the but this notion 1481 01:03:12,536 --> 01:03:14,686 of escaping is something we'll see quite a bit. 1482 01:03:15,036 --> 01:03:16,356 Well, it turns out we can do stuff 1483 01:03:16,416 --> 01:03:19,776 with not just ints not just strings, there are chars 1484 01:03:19,776 --> 01:03:21,026 for individual characters, 1485 01:03:21,026 --> 01:03:23,616 doubles for floating point values and float 1486 01:03:23,616 --> 01:03:25,026 for floating point values and all 1487 01:03:25,026 --> 01:03:27,556 of these have some different meaning, in fact let me go ahead 1488 01:03:27,846 --> 01:03:29,986 to an example that's not just text space now, 1489 01:03:30,226 --> 01:03:32,406 but just to demonstrate, again, to underwhelm, 1490 01:03:32,406 --> 01:03:33,636 at first because we can definitely one 1491 01:03:33,636 --> 01:03:37,036 up ourselves easily that math is not hard and c. 1492 01:03:37,346 --> 01:03:40,066 so here's a program that assigns a variable called x, 1493 01:03:40,426 --> 01:03:44,486 I'll type integer the value of 1; 1494 01:03:44,556 --> 01:03:48,696 variable called y type is int, assign it a value of 2 right, 1495 01:03:48,696 --> 01:03:50,256 new syntax, not interesting. 1496 01:03:50,346 --> 01:03:53,476 Finally I declare a third variable called z its also going 1497 01:03:53,476 --> 01:03:56,516 to store an int, its going to store the sum of x and y, 1498 01:03:57,186 --> 01:04:01,406 looks pretty good but let me go ahead and let me run GCC 1499 01:04:01,576 --> 01:04:03,896 of math1.c okay, interesting. 1500 01:04:04,186 --> 01:04:07,036 So it turns out GCC, especially the way we have it configured 1501 01:04:07,036 --> 01:04:11,826 on nice.faf can also be a bit anal and a little cautious 1502 01:04:11,826 --> 01:04:13,466 on your behalf saying you know what, 1503 01:04:13,896 --> 01:04:15,186 odds are you screwed up here. 1504 01:04:15,296 --> 01:04:16,526 right? its saying it very nicely, 1505 01:04:16,576 --> 01:04:19,106 warning unused variable z but that's telling you 1506 01:04:19,106 --> 01:04:21,236 that you did something, you're not using it, 1507 01:04:21,486 --> 01:04:23,756 probably wasn't a very productive program you just 1508 01:04:23,756 --> 01:04:24,896 wrote, but let's try it. 1509 01:04:24,936 --> 01:04:28,866 let's be stubborn and let's run a.out enter, hmm. 1510 01:04:29,316 --> 01:04:32,876 Is it not working? 1511 01:04:34,226 --> 01:04:35,886 Its actually working perfectly. 1512 01:04:37,456 --> 01:04:40,256 Right, the math is correct, what is in z 1513 01:04:40,256 --> 01:04:41,496 at this point, oh, missed it. 1514 01:04:41,656 --> 01:04:44,146 the problem is what? 1515 01:04:44,146 --> 01:04:44,746 Simple answer. 1516 01:04:46,796 --> 01:04:49,736 I'm not doing anything with it, yes I created 3 variables, 1517 01:04:49,736 --> 01:04:52,406 all of them at some point in time, do in fact have numbers 1518 01:04:52,406 --> 01:04:55,036 like 1 2 and hopefully 3 inside of them 1519 01:04:55,236 --> 01:04:56,896 but if you don't do anything with it, 1520 01:04:57,166 --> 01:04:58,986 sort of the tree falling in the woods right? 1521 01:04:58,986 --> 01:05:01,876 no one knows that the answer you just computed was three unless 1522 01:05:01,876 --> 01:05:05,046 you print it, save it, do something with it, 1523 01:05:05,266 --> 01:05:07,096 so hopefully there's a solution here. 1524 01:05:07,096 --> 01:05:09,386 so let's take a look at the second, slightly fixed version 1525 01:05:09,386 --> 01:05:11,246 of this, so how do I print out the result? 1526 01:05:11,246 --> 01:05:13,316 Well again, let's just go into our little tool kit that's going 1527 01:05:13,316 --> 01:05:15,006 to start being filled with more function still, 1528 01:05:15,286 --> 01:05:18,086 Print F quote unquotes the quotes are important, 1529 01:05:18,266 --> 01:05:21,416 the percent means display this as an integer and then I pass 1530 01:05:21,416 --> 01:05:24,556 in z and so, okay, so now not only have I written a correct 1531 01:05:24,556 --> 01:05:28,456 program, I'm actually doing something productive with it, 1532 01:05:28,516 --> 01:05:32,876 a.out, now gives -- hmm, still a little weird, its not incorrect 1533 01:05:32,876 --> 01:05:34,906 but what's that explanation for this weirdness. 1534 01:05:36,366 --> 01:05:38,696 Right, so there's just no new line, so let me go in here 1535 01:05:38,946 --> 01:05:42,056 and I'm going to move over here, add between the quotes 1536 01:05:42,056 --> 01:05:45,686 but after the percentn let me recompile, 1537 01:05:45,686 --> 01:05:47,706 another little Linux trick, if you get tired 1538 01:05:47,706 --> 01:05:49,516 of typing the same commands over and over again, 1539 01:05:49,786 --> 01:05:52,006 you can either hit up and that will scroll 1540 01:05:52,036 --> 01:05:53,846 through the commands you previously typed 1541 01:05:53,976 --> 01:05:55,576 or you can hit ! 1542 01:05:55,746 --> 01:05:59,746 aka bang and type the first letter or so of the last command 1543 01:05:59,746 --> 01:06:01,696 that starts with that sequence and hit enter 1544 01:06:01,696 --> 01:06:04,016 and if I haven't forgotten some other g command I wrote, 1545 01:06:04,306 --> 01:06:07,086 it should remember, aha, it was gccmath2.c, 1546 01:06:07,086 --> 01:06:09,136 again little things you pick up over time. 1547 01:06:09,806 --> 01:06:11,706 and if I run a.out, 1548 01:06:11,706 --> 01:06:14,106 okay slightly prettier, slightly prettier. 1549 01:06:14,326 --> 01:06:16,946 So what else can we do here, well there's going 1550 01:06:16,946 --> 01:06:20,436 to be other data types still but there's also this format strings 1551 01:06:20,436 --> 01:06:23,206 so let's see if we can't do a little bit 1552 01:06:23,206 --> 01:06:25,356 of something that's more interesting underneath the hood 1553 01:06:25,356 --> 01:06:27,736 so it turns out we have format strings, 1554 01:06:28,006 --> 01:06:29,586 but there's also this approach here, 1555 01:06:29,586 --> 01:06:30,736 let me go back to the code. 1556 01:06:30,736 --> 01:06:32,666 This is a program, again, that you have a print out of, 1557 01:06:33,016 --> 01:06:36,046 called size of, and here's where we finally scratch the surface 1558 01:06:36,046 --> 01:06:37,786 of actual design issues. 1559 01:06:38,186 --> 01:06:40,586 Actual questions that you are interested to answer 1560 01:06:40,736 --> 01:06:43,436 because they really will affect the correctness of your program 1561 01:06:43,436 --> 01:06:44,726 and what you can do with it. 1562 01:06:44,986 --> 01:06:47,516 so this program, its not very long, its about half comments 1563 01:06:47,516 --> 01:06:48,546 and half actual code, 1564 01:06:48,806 --> 01:06:51,306 notice that at the top here I've declared some variables 1565 01:06:51,306 --> 01:06:53,656 in advance, so this is kind of interesting. 1566 01:06:54,036 --> 01:06:56,486 Even though thus far I've very efficiently, 1567 01:06:56,486 --> 01:06:58,706 very analy been declaring a variable on the left 1568 01:06:58,706 --> 01:07:01,436 and then doing something with it on the right, all in one line, 1569 01:07:01,836 --> 01:07:02,916 you don't need to do that. 1570 01:07:02,916 --> 01:07:04,686 If you know you're going to need a variable, 1571 01:07:04,886 --> 01:07:07,946 you can declare the variable earlier in your program 1572 01:07:07,996 --> 01:07:11,286 and then use it later and you'll learn over time when this is 1573 01:07:11,286 --> 01:07:14,486 and isn't appropriate, but here I've declared three types 1574 01:07:14,486 --> 01:07:15,046 of values. 1575 01:07:15,046 --> 01:07:17,466 A char, a double, a float and an int. 1576 01:07:17,976 --> 01:07:20,356 a float is a floating point value a real number, 1577 01:07:20,356 --> 01:07:24,276 something with a decimal point, what's a double perhaps? 1578 01:07:25,156 --> 01:07:26,086 Any ideas? 1579 01:07:27,156 --> 01:07:28,496 So its twice something, 1580 01:07:28,726 --> 01:07:31,786 its actually a floating point value that's twice 1581 01:07:31,786 --> 01:07:32,916 as big potentially. 1582 01:07:33,176 --> 01:07:35,916 So it turns out that in this language and in some others, 1583 01:07:36,126 --> 01:07:37,766 there are actually pre defined meanings 1584 01:07:37,766 --> 01:07:40,486 for how many bits are used to represent a data type, 1585 01:07:40,796 --> 01:07:44,816 so we have chars or 8 bits on a typical modern computer 1586 01:07:44,856 --> 01:07:48,396 with intel inside, a double is, and let's come back to that. 1587 01:07:48,396 --> 01:07:52,036 A float is 32 bits, now that's pretty big, 1588 01:07:52,036 --> 01:07:55,396 because if an int is also 32 bits, what did we say last week, 1589 01:07:55,396 --> 01:07:56,646 the biggest number you can represent 1590 01:07:56,646 --> 01:07:59,726 with 32 bits is, like 4 billion. 1591 01:07:59,726 --> 01:08:01,866 Now wait a minute, if you're omitting negative numbers 1592 01:08:01,866 --> 01:08:03,606 so if its negative numbers also then its 1593 01:08:03,606 --> 01:08:06,746 like negative two billion, to positive 2 billion roughly 1594 01:08:06,746 --> 01:08:08,056 but a total of 4 billion 1595 01:08:08,336 --> 01:08:12,106 so floating point values also use 32 bits but they use them 1596 01:08:12,106 --> 01:08:15,286 to have some numbers before the decimal point, 1597 01:08:15,596 --> 01:08:18,006 some after so you can't, it's a trade off 1598 01:08:18,006 --> 01:08:19,686 between how big a number you can represent 1599 01:08:19,686 --> 01:08:23,066 as a floating point value versus how many points you can have 1600 01:08:23,066 --> 01:08:26,476 after the decimal point but a double now is not 32 bits, 1601 01:08:26,566 --> 01:08:29,246 right, I'm kind of beyond the scope of my arms here, 1602 01:08:29,496 --> 01:08:31,656 its 64 bits and that's pretty damn big 1603 01:08:31,826 --> 01:08:34,296 so you'll have some discretion there so what does it mean 1604 01:08:34,296 --> 01:08:38,746 when I run this down here, Print F all right, so print an int 1605 01:08:38,746 --> 01:08:40,226 and a new line, but what do you want to print? 1606 01:08:40,556 --> 01:08:43,286 Well there's this operator in c, that's only occasionally useful, 1607 01:08:43,356 --> 01:08:46,596 today its marginally enlightening, tell me the size 1608 01:08:46,596 --> 01:08:50,046 of the variable c. tell me the size of the variable d, f, 1609 01:08:50,046 --> 01:08:53,886 and i so let me go ahead and run a GCC on sizeof.c now I'm going 1610 01:08:54,136 --> 01:08:58,176 to go ahead and run a.out, wait a minute, those are not 1611 01:08:58,176 --> 01:09:00,366 in fact values like 32 and 64. 1612 01:09:00,366 --> 01:09:01,296 what am I seeing? 1613 01:09:02,816 --> 01:09:03,746 Yeah, I'm seeing bytes. 1614 01:09:03,936 --> 01:09:06,196 That's kind of interesting, okay, so that's fine, 1615 01:09:06,446 --> 01:09:07,736 let me take a look at one other thing. 1616 01:09:07,736 --> 01:09:10,686 I saw on the screen a moment ago that there's this mention 1617 01:09:10,686 --> 01:09:15,106 of longs so let me say long is an l and let me print 1618 01:09:15,106 --> 01:09:20,586 out its length here, so long and I think its l, hopefully, %, 1619 01:09:21,336 --> 01:09:28,166 let's see, GCC sizeofc, oops not l, dammit, what is it? 1620 01:09:28,806 --> 01:09:29,716 is it also d? 1621 01:09:30,796 --> 01:09:31,586 yes all right. 1622 01:09:31,916 --> 01:09:35,576 if I run this now what do you think a long is going to be? 1623 01:09:35,576 --> 01:09:42,246 So int was 4, also 4, so again, sort of a historical artifact, 1624 01:09:42,386 --> 01:09:44,206 when I mentioned long longs before, 1625 01:09:44,536 --> 01:09:46,796 so if you want an int that's bigger than 32 bits, 1626 01:09:46,796 --> 01:09:49,446 you want a 64 bit int, its not a long, because a long is the same 1627 01:09:49,446 --> 01:09:51,646 as an int, it's a long long that you want. 1628 01:09:51,646 --> 01:09:54,536 So again, sort of random c trivia. 1629 01:09:54,886 --> 01:09:55,796 But I thought we'd end with this. 1630 01:09:56,206 --> 01:09:59,256 Here's a crazy looking program, I loaded this off the internet, 1631 01:09:59,546 --> 01:10:02,196 written by a really, either a really brilliant person 1632 01:10:02,196 --> 01:10:04,486 or a person with really a lot of free time. 1633 01:10:04,836 --> 01:10:07,836 so this is an obfuscated C program 1634 01:10:08,156 --> 01:10:10,756 and you are entering a community that you don't have to stay 1635 01:10:10,756 --> 01:10:14,246 in in perpetuity of people who like writing programs like this, 1636 01:10:14,246 --> 01:10:16,196 or have the ability to do it and there are contests 1637 01:10:16,336 --> 01:10:20,146 that people run for obfuscated c contests whereby you challenge 1638 01:10:20,216 --> 01:10:23,176 people to write the most confusing looking program you 1639 01:10:23,176 --> 01:10:25,876 could possibly come up with that no human should be able 1640 01:10:25,876 --> 01:10:28,076 to understand and then you challenge the audience, 1641 01:10:28,116 --> 01:10:31,116 the other geeks in the room, what that program actually does, 1642 01:10:31,686 --> 01:10:34,816 do you have any idea what this is going to do? 1643 01:10:34,816 --> 01:10:37,006 We will not be writing things like this. 1644 01:10:37,216 --> 01:10:39,606 This is an exercise in the opposite of good design, 1645 01:10:39,886 --> 01:10:41,986 but its kind of a powerful thing, 1646 01:10:42,196 --> 01:10:44,146 that if we run the compiler here, the warnings I'm going 1647 01:10:44,146 --> 01:10:45,276 to ignore for now, that's fine. 1648 01:10:45,566 --> 01:10:47,026 A.out, I'll give you the results 1649 01:10:47,026 --> 01:10:50,236 of your first obfuscated c program which is, 1650 01:10:51,546 --> 01:10:52,866 that's what that does. 1651 01:10:53,036 --> 01:10:53,796 We'll see you on Friday.