DAVID J. MALAN: So this is CS50. And this was CS50's own Colton Ogden, for more of who's music you can download at soundcloud.com/cs50.
So today we focus all the more on the art of programming. And we take where we left off last week, focusing on Scratch, which was this graphical programming language. And take things down to a lower level, using a more traditional programming language known as C. But along the way, realizing that the same ideas we talked about last Friday will recur not only in this language C, but in most every other that we look at this semester.
So we called this thing here what last time? This is representative of a statement. So we called this a statement. And it does something. It's an instruction that a computer or Scratch might execute. And henceforth, let's also start calling something like this a function, for reasons we shall soon see.
Meanwhile, we saw things like this. And these are generally known as what construct?
DAVID J. MALAN: So a loop. So pretty straightforward. It literally does what it says. And in Scratch, if you want to cram more puzzle pieces in there, the piece will grow to fit it. And we'll see in C that we can do the same thing.
Another type of loop, though, in Scratch might be forever, or there's any number of other approaches. But let's take a more generalist look, with a face that's likely quite familiar, at loops more generally. MARK ZUCKERBERG: One thing that computers are really good at is repeating commands. As a person, you'd get really bored if you had to do the same thing lots of times in a row. But a computer can do the same thing millions or even billions of times, and not get bored, and be able to carry that out really well.
So for example, if I wanted to wish everyone on Facebook a happy birthday by sending them an email, it might take me more than a century to actually write out all of those emails to everyone. But with just a few lines of code, I can have a system send an email to everyone on Facebook wishing them a happy birthday. So that's what loops are and why they're valuable and something that computers can do very well.
DAVID J. MALAN: So if you've been the recipient of some of CS50's and Dropbox's space of late, that's actually precisely what we do, using code quite like that. We essentially have a big spreadsheet into which folks have been inputting their names and email addresses. And we wrote a tiny bit of code that can iterate over those addresses and spit out unique addresses in case anyone submitted multiple times. And thereafter, we send an automated email from CS50's bot, including the coupon code.
Now we also looked at this construct last time. And this is a particular example of what? So, yeah, a Boolean expression. And the shape is meant to capture that as well. All such questions of the form true or false in Scratch will look a little something like this. And we call this a Boolean expression. That's indeed true or false, yes or no. It's a way of answering a question.
And Meanwhile you can use these Boolean expressions inside of constructs like these, which of course are conditions or branches, proverbial forks in the road. And they can look not only like this, but they can also have elses associated with them. And you can actually have a three way fork in the road or a four way fork in the road, simply by nesting these things, as you'll see in Scratch if you haven't already. And as you can do in C as well.
Let's take another generalist look at an alumnist, as well, who might present a different approach to these things called conditions.
BILL GATES: People make decisions every day. For example, before you go outside you kind of have an if statement that says, if it's raining then I need to get my jacket. And computers are amazing once you decide those kinds of statements, that they can reliably execute those things at unbelievable speed. And so a computer program really is a little bit of math and some if statements where the decision gets made.
DAVID J. MALAN: So as you may know, it was folks like Bill Gates, Paul Allen, and others that truly kicked off the so-called personal computer revolution some years ago. I thought, before we dive into some administrivia, we'd relate a tale from the Albuquerque, New Mexico museum of Natural History and science where some of Bill Gates' and Paul Allen's earliest stories are recounted how we have now today's PCs and Macs and more. For this, though, we need two volunteers who have very good narration voices to read a script aloud.
All right, how about in back there. Come on up. And how about in front here. Come on down. Take your places here. As you guys come up, a couple of administrative announcements. sectioning, the process of choosing a section, will start this Wednesday through Friday. More on that this coming Wednesday in lecture.
Super sections, meanwhile, are something that we offer next week whereby the entire class is invited to participate in fairly large scale sections, one more comfy, one less comfortable. And we will announce the particulars of that, as well, later this week.
Meanwhile sections, themselves, will start in week three of the course, zero index, which means those are a couple of weeks out. So not to worry. You have not yet missed anything along those lines. Meanwhile in the meantime, if you have any questions administratively, email myself and Devon and Gabe and Rob at this address here.
Lastly, problem set 0 is, of course, out. You may have noticed it on the course's website. It should provide you with all answers to questions you might have along the way for getting started with the problem set. But if you find yourself in need of a helping hand, by all means attend office hours, Monday, Tuesday, Wednesday, Thursday of this week in the evening in four dining halls. See that URL there for the particulars of where office hours shall be.
Now Let's meet our two guests here today. What's your name?
JAY PAUL: Jay Paul.
DAVID J. MALAN: Jay Paul, nice to meet you.
DAVID J. MALAN: Hikari?
DAVID J. MALAN: Hikari. Nice to meet you as well. Jay Paul and Hikari have in front of them on these music stands two scripts that I printed out this morning from the New Mexico museum of Natural History and Science. And what I thought I'd do on the overhead is accompany your recitation of the script and this history with some visual images, no surprises.
And what I've done in advance is prehighlight on your script what you should read, and what you should read. And we'll essentially just alternate paragraphs. So it's much like you might have done in English class. That's really compel the audience to believe in your tale here. Shall we? So the story here is, we have a BASIC, and it begins in Cambridge.
JAY PAUL: It was winter, 1974, in Cambridge, Massachusetts where winters can be cold. A bearded, long haired college dropout named Paul Allen was drudging across Harvard Square, absorbed in his thoughts. His main preoccupation in those days was how to get his friend, Bill Gates, to quit school and go into business with him. HIKARI: The two had already gone through a number of business ventures together, beginning at Lakeside school in Seattle where they were paid in free computer time to test a PDP-10 computer at a local time sharing company. The most recent plan had been to quit school and form a software company. Allen had left Washington State University. But at the last minute, Gates decided to stick with Harvard.
JAY PAUL: That day, crossing Harvard Square, Allen spotted the January, 1975 issue of Popular Electronics with the earth stopping headline, World's First Minicomputer Kit to Rival Commercial Models. Beneath the headline was a picture of a small box adorned with lights and switches. It was called the MITS Altair 8800. And Allen knew this was what he had been looking for.
HIKARI: Days of discussion followed. Allen and Gates understood the significance of the Altair. They had talked often about microprocessors and were waiting to see what would be done with them. Now there was a minicomputer kit on the cover of Popular Electronics. It apparently had no software yet. They imagined a nation of programmers descending on MITS. And so they called Ed Roberts, the head of the company, claiming to have a version of the BASIC programming language almost ready for the Altair. They didn't. And Roberts must have known they didn't. He was getting 10 calls a day from people who had a BASIC almost ready. And his stock response was, "The first person who shows up with a working BASIC gets the contract."
JAY PAUL: Gates and Allen had never seen an Altair. They had never even seen the Intel 8080 microprocessor at the heart of the Altair. But a couple of years earlier Allen had written a program on a mainframe computer that emulated the operation of a previous Intel microprocessor. And this time around they would do the same thing.
HIKARI: With an Intel 8080 manual at his side, Allen sat down at a Harvard PDP-10 computer and wrote the emulator and software tools necessary to do the programming. Meanwhile Gates stopped going to classes and devoted himself to designing the BASIC, using every trick he knew to get the size down below 4 kilobytes.
JAY PAUL: Out in Albuquerque, Ed Roberts got a call from Gates asking for details about how the Altair handled specific routines. No one had ever asked that before. And Roberts began to get interested.
HIKARI: With the development tools and the design ready, Gates and Allen wrote the code on the PDP-10, enlisting another Harvard student, Monte Davidoff, to write math routines. After a final night of programming, Allen got on a plane to deliver their BASIC to MITS. He spent the plane ride out worrying. And back in Cambridge Gates was worrying. They had tested their BASIC and it had worked on the emulator. But what if the emulator was wrong? JAY PAUL: As the plane approached Albuquerque, Allen realized that their BASIC, now neatly contained on a small roll of punched paper tape, would be useless without a separate program, called a loader, that would tell the Altair how to read the paper tape being fed into the teletype machine. He took out his notebook and quickly scribbled down a loader program in Assembly language, then manually translated that into the 1s and 0s the Altair would understand.
HIKARI: Allen was expecting a clean, little, high tech company run by men in business suits. So he was surprised when Roberts met him at the airport looking like a ranch hand. Roberts was also surprised. When he dropped the Harvard programmer at an expensive hotel, Allen had to confess that he couldn't afford the room.
JAY PAUL: MITS, itself, was located in a dusty strip mall. Inside on a cluttered workbench was an Altair loaded up with 5 kilobytes of memory and connected by a cable to a teletype machine. Roberts and Chief Engineer, Bill Yates, waited expectantly as Allen toggled his loader program into the Altair. The teletype began chugging as it pulled the paper tape through the tape reader. It took perhaps 15 minutes to load the program. Then the teletype abruptly printed a memory prompt, then a ready prompt, and Allen began typing a few test commands. To everyone's amazement, the software worked. There were bugs, of course. But the main thing was it worked.
HIKARI: Later, on their way to a $3.00 lunch at Pancho's, a wasp flew in the window of the pickup truck and stung Allen on the arm. But at that point nothing could spoil the mood. The Altair now had its BASIC, the first commercial software for a home computer, made by Paul Allen and Bill Gates, doing business as Microsoft.
DAVID J. MALAN: Many thanks to our two readers.
DAVID J. MALAN: Now you have your choice, before you leave, of a sparkly or a glow in the dark-- oh, wait one sec, wait-- sparkly and a glow in the dark elephant from CS50's own Cheng Gong. HIKARI: Aw, thank you. DAVID J. MALAN: All right, thank you so much. [APPLAUSE] DAVID J. MALAN: So true story. This is to say that all of these machines and the ease with which we use them now and take for granted, really started here, just a few paces from this actual theater. And now today, if you've not been, this is Maxwell Dworkin, the computer sciences building, also around the corner. And on the second floor of this building does hang three pages from the original source code that Bill Gates and Paul Allen wrote. In fact, if use you zoom in you can see not only their names in the original type, but also their signatures that they adorned a few years back when they last visited.
But what was particularly compelling about this is what they set out to do was to write a program that would enable other people to write their own programs. At the time all there was with this Altair machine made by MITS. And all they needed was some way of making it easy for hobbyists and people like us in this room to actually program that without necessarily understanding Assembly code, or machine code, or God forbid, 0s and 1s. We programmers, like those in this room, we want to be able to express ourselves much more like pseudo code even if it's a bit more nit picky than we talked last week. We don't want to write code like this, which Paul Allen and Bill Gates did. We, instead, want to write code that looks a little more user friendly.
Now this is a language known as BASIC. And the line numbers there are what you use to simply number the lines of code that you're writing. We don't even have to do that these days. But you can see here how PRINT "hello, world" would indeed presumably print just that. And so what Bill and Paul did was empower people to write code like this instead of-- if you take a look at the computer science wall-- code like this.
In fact, CS50 recently had an opportunity to sit down with Professor Harry Lewis in the Computer Science department, who actually taught Bill Gates some years ago, and is standing here before those three pages of excerpts. Let's take a look.
HARRY LEWIS: What you have here is a listing of an early piece of software written by Bill Gates and Paul Allen, the founders of Microsoft. So the code is interesting for two reasons. First of all, it became Microsoft's first product, which was an interpreter for the BASIC programming language. And secondly, this was one of the first attempts to create an interpreter so ordinary people could use personal computers.
So Bill Gates was an undergraduate at Harvard. I started teaching at Harvard in 1974. This was done in 1975. So it was early in his career and early in my career. I actually taught Bill in a course around this time. Paul Allen was not a Harvard student, but he had been a high school classmate of Bill Gates. If you come and look at the listing, you'll actually find a third name, Monte Davidoff, who was Gates's classmate here at Harvard.
OK, so here's an interesting comment up here. It says "In 4K can delete square root but for loops should still work." OK, so what that means is that there were two ways to compile this program. One was to run on a version of this Altair computer that only had 4K words of memory, 4,096 words of memory. But the big version had 8K. And so what this says is that in the 4K version you had to delete some code to make it fit. And one of the things that would be deleted would be the square root routine. But apparently the for loops should still work even when you're compiling down for just the 4K version of the computer. DAVID J. MALAN: So one of the themes, as we'll see in computer science, is this notion of layering and abstraction, and really standing on the shoulders of folks who have come before us, not unlike some of the Bill Gates and Paul Allen narrative here. And what this means is that today we can take for granted that things are actually easier for us to write code. Indeed code, as we said on Friday, is more technically known as source code. And it's this English like syntax that's more proper and more well defined than something like pseudo code, for which there is no formal definition.
And today what we're going to focus on is source code that looks like this, which admittedly at first glance looks completely cryptic. And frankly, it's way more aesthetically complex than the underlying program is. All this program does, recall, is what did we say? It just prints "hello world." And yet look at all of the stuff we need to wrap around that very simple phrase. But before long all of these lines and more will make much better sense to you.
And the flow is as follows. What we need at our disposal is to take code, like source code that we just saw. And we need a new program to run it through. We need algorithms that can convert source code, like this, into object code, 0s and 1s. In other words, what we're going to learn today is how to do this, write source code up top, pass it as input to a special program known as a compiler, and we're going to produce object code. Which is just a fancy way of saying we will take something that looks like this, pass it into a program called a compiler, producing object code that looks like this.
So these are literally the patterns of 0s and 1s that are understood by an Intel computer these days, that if interpreted by the CPU, the brains inside of a computer, will literally print out, quite simply, "hello world." Now there's way more 0s and 1s than you would hope might be necessary for that. But that's because we're building on a good deal of complexity that we can henceforth take for granted. In other words, a lot of smart people have given us a lot of cool tools and powerful software with which we can now make projects of our own.
So let's get started. Whereas last Friday, and for Problem Set 0 this week, you'll be playing in a world that looks like this. Starting today and for Problem Set 1 next week, the code is going to look a little more cryptic, but functionally is going to be the same. So notice, top is Scratch, bottom is C. Frankly Scratch is a lot more user friendly and accessible. But if you now compare visually top to bottom, there's kind of a one to one correspondence between the puzzle pieces and the language we're about to play with. In particular, say, the blue puzzle piece up top, is apparently equivalent to what keyword or special phrase in this language called C? AUDIENCE: Printf. DAVID J. MALAN: Printf. That means formatted printing. And that's just a fancy way of saying printf in C is going to be a statement or a function that prints something to the screen. What does it print to the screen? Whatever you tell it to inside of quotes, inside of parentheses. So again, admittedly, there's a lot of stuff that you have to keep in mind, parentheses, quotes, semicolons, and all of this. But all of that is sort of beside the point. The interesting takeaway for now is that the say block in Scratch is pretty much equivalent to the printf statement in C.
Meanwhile, the yellow puzzle piece up top, when green flag clicked, is probably equivalent to what keyword here? AUDIENCE: Main. DAVID J. MALAN: So main. Maybe int, maybe void. But main kind of sounds like it's important, and indeed it is. So when green flag clicked is, again, the puzzle piece that kicks off an entire Scratch program. But we are going to now start calling that main, this function known as main.
Now meanwhile we'll generalize this as follows. We might call say a function. And it might look specifically like this. Well, what about loops? If we want to start converting or translating more in Scratch to C, this thing here apparently just says "hello world" forever. So if you associate these scripts with a cat in Scratch, it's just going to keep saying in some kind of cartoon bubble, "hello world, hello world, hello world," ad nauseum, forever. If we want to do something similar in C, it's going to look a little cryptic. But we can achieve the same results, as we'll eventually find out, with syntax like this. I'm going to use printf again, because at the end of the day I want to print "hello world." And I'm apparently using a keyword that isn't forever. It's instead the word while. But just semantically in English, while kind of suggests some kind of loop or cycle.
So that happens to be the keyword that C uses. And while true, why does while true effectively mean forever? Well, while, as we'll soon appreciate all the more, has this parenthetical next to it where you can put a Boolean expression. And so long as that expression is true, this code, as denoted between these two curly braces as we'll call them, will just keep running again and again.
So true is true. So while true means just do this forever. It's almost a stupid construct to express yourself. But there was no forever keyword in C. There was while. And there was a very simple Boolean expression like true. And so this, we'll see, achieves the same result.
How else might you implement a loop? Well, in Scratch you might hard code a specific number of iterations in the loop here. And so that's exactly what we might do in this version of C, whereby we have a so-called for loop. And the for loop here is going to iterate, somewhat cryptically, from the value 0 on up to but less than 10.
Now variables in Scratch. You might not have had occasion to use these. But what's the point, in general, of a variable did we say last week? What's that?
DAVID J. MALAN: To point? Oh, points. OK, keeping track of points, for instance, in a game. Or more generally doing what with the variable? What's the utility of them?
DAVID J. MALAN: Yeah, so memory. It's for storing something. And the sort of silly visual I used last time was like this glass bowl. And if we wanted to store something inside of a variable, for instance right now the value is six because there are six ping pong balls in here. It's just some kind of storage container that underneath the hood is implemented with bits, 0s and 1s, however a computer happens to do that.
So in Scratch, if we want to have a variable, we can use an orange block like this. We'll call that counter in this particular case. I initialized it to 0. And what it I then do? Forever, say counter, which if you play with that in Scratch, you'll just see Scratch. The cat or whatever costume you put on him will speak the number in question.
Change counter by 1 is like incrementing by 1. And so this is going to count from what so what? From 0 til infinity, or until Scratch breaks, or until you sort of lose interest in watching how high he can actually count.
So how might we convert this to C? It's going to look a little cryptic. But again, if you look at each of these lines individually they kind of lineup generally. So apparently int is going to have some special meaning in C. We'll see that again. As an aside it means integer. So it just means number. So that's the type of glass bowl I want, one that can store numbers, not ping pong balls. And I'm going to use the equal sign there to assign it a value of 0. So that's quite like set counter to 0, but in C.
Meanwhile, while true, that was equivalent, of course, to forever, even though it's a little cryptic, and then inside of the curly braces. And you can think of these curly braces, which you can type on your keyboard, as really being like the curvature in these yellow loop blocks in Scratch. It embraces multiple lines of code.
Printf is getting a little scary now because I see not only quotes in parentheses. But what else is new syntactically in this example? There's a percent d. And then backslash n is there. We did see that before, even though I didn't mention it. Then there's a comma. And then there's a counter.
But we'll see in just a moment that this is just a standard way of saying print a "decimal number," quote, unquote, some decimal number. But I'm going to tell you later what that decimal number is. So the fact that there's a comma in this line saying printf, means go ahead and print some decimal number. Oh, and by the way, the number is whatever the value of this variable is. And so to make this more clear, we'll see an example before long involving exactly printf in the context of real C programs.
Now just to wrap up some of these constructs. Boolean expressions, you might not have occasion to use these in your program but you might very well, especially if using conditions. And so these two examples from Scratch mean if x is less than y, or if x is less than y and y is less than z, how might we translate that?
Well, in C it's just going to look like this. A little cryptic, more parentheses, some weird ampersands, but fundamentally the ideas are as simple as they are in the puzzle piece world. We're simply checking if something is less than something else.
How about conditions? In a nutshell in English, what does this chunk of Scratch code do would you say?
AUDIENCE: Compares two numbers.
DAVID J. MALAN: Compares two numbers, and if x is less than y it says as much. If x is greater than y it says as much, else if x is equal to y it says as much. Now where did x and y come from? Who knows. This chunk of Scratch code is out of context. But what we want to do now is translate this for just a moment to see.
So as you get comfortable this week in Problem Set 0 playing in this world on the left, realize that the ideas aren't changing this week or next or beyond. We're simply going to start writing things in a different way with our keyboard instead of our mouse.
So if x is less than y, and there's some parentheses there, then there's some curly braces again to kind of encapsulate it just like the yellow puzzle pieces do. And I'm going to printf x is less than y and so forth.
What is nice about C, as you can see here, is that you don't get this nesting, nesting, nesting that's necessarily going to push your code to the right. You can instead have everything lineup neatly like this. But that's just an aesthetic detail we'll see again before long.
All right. So that brings us back to this cryptic looking program. Let's actually write some code. Now how do you go about writing code? All these years that you've owned a Mac or PC, desktop or laptop, you've actually had the capability to start writing programming code. But you're probably missing a special type of program. You can certainly write code. But you can't necessarily, out of the box, convert that source code into object code, 0s and 1s without what on your computer? AUDIENCE: Compiler. DAVID J. MALAN: So a compiler, right. Now most of you probably don't own a compiler, have never downloaded a compiler. But you'll see you can download it like most any other piece of software. In the world of Mac OS, you might download or have downloaded already something called Xcode or GCC or Clang. If you come from the Windows world you might have downloaded Visual Studio, Visual Basic, environments like that. There's dozens of compilers these days that you might use. But the short of it here is that it would be a pain and, frankly, a technological nightmare for hundreds of people with different computer configurations to all configure their machines in exactly the same way so that we can all be on the same page.
So what we, instead, do in CS50 is we give you a standard environment, a Linux environment that's going to look a little something like this. You'll see more of this in the Problem Set 1 specification which will go online this Friday night.
And what this means is that you henceforth are going to be able to download and install a program called the CS50 Appliance. And you're going to download and install another program called a hypervisor, which is just a fancy way of achieving this. Whether you own a Mac or PC or a Linux computer or a Solaris computer, or whatever crazy operating system you're running, you're going to download a program called the hypervisor that's going to run the CS50 Appliance on your computer no matter what your operating system is. A hypervisor in other words converts our stuff to whatever language, whatever instructions your own computer understands.
So this is a layering. And again, this is kind of thematic in computer science, the building on top of things. You might have Windows. You might have Mac OS. But starting next week you're going to have downloaded a free piece of software called the hypervisor. You're going to have downloaded a piece of software called the CS50 Appliance.
That's going to run in the hypervisor, which is going to run on your computer. And the end result is that all of us will have a simple window like this on your Mac or PC that gives us the illusion of all running the same operating system, without any other impact on your computer. And you can full screen it and essentially behave as though you are running an operating system called Linux, which is what the CS50 Appliance is based on.
So let's actually use this now to write a program. You can write a program using Microsoft Word. You can write a program using TextEdit, or Notepad, or WordPad, or pretty much any word processing program you've ever used. But the reality is you shouldn't use really any of those programs. You certainly shouldn't use something like Microsoft Word, which has bold facing and italics, and bigger fonts and smaller fonts, because the computer doesn't care about any of that. All the computer is going to care about is English like instructions written in C.
So what we'll do inside of the CS50 Appliance is use a program freely available, and it's pre-installed in this appliance, called Gedit. And Gedit is just a super simple text editor like Mac OS's TextEdit, like Windows Notepad that's going to look a little something like this.
So let's actually not look at slides of this. But let's actually go into the environment itself. I'm going to go ahead and log in, in this other window, to my CS50 Appliance, which I've pre-installed on my laptop here.
Notice that, like Windows and Mac OS, it's got some menus. Like Windows it puts it down there instead of Mac OS, which puts it down there. But the reality is it's all kind of the same. And in here is a whole bunch of software. There's Dropbox. There's a whole bunch of accessories, graphical programs that we'll use later in the term for problem sets. There's a web browser built in so that this is a full fledged computer inside of a computer.
But I'm going to click this leftmost white icon here, which is the icon for Gedit. And it's going to open a window that has three panels to it, a left one, a top one, and a bottom one.
Now it's in this top right one that I'm going to actually write some code. So let's do this,. I'm going to go to File, Save. And you might not ever have seen this particular window before. But this is like Mac OS or Windows. It's just a window with all of the folders that I have inside of this computer.
And I'm going to save the file as hello.c. And I'm going to go ahead and click Save. And now notice I have a tab at top left called hello.c. So probably pretty familiar even if you've not used this particular program before.
And now I'm going to type in those fairly cryptic sequence of commands that we saw a moment ago. Include, standard I/O.h. More on that soon. Int main void, open curly brace, closed curly brace. And then inside of there I think is where we had "hello world," semicolon. And now save.
So this is a program written in C, written, therefore, in source code. But I can't just run this program. I kind of want to double click on an icon somewhere, but no icon exists other than the source code file. What, again, is the process now that I need to run this file through? AUDIENCE: Compiler. DAVID J. MALAN: So a compiler. So in different computers you do this in different ways. But what we've done here in the CS50 Appliance that's representative of how you might do this on many different systems, is I'm simply going to run a command called make. And make is literally going to do that. Make me a program.
So make is going to run a compiler for me. And the name of the program I want to make is hello. Now make is a program. And make is smart enough to realize that if I say make hello, it's going to look automatically for a file called hello.c just because that's the way it's configured.
So when I now hit Enter here, this crazy cryptic line just got executed. And trust me, before long, within just days, you'll understand what all of that nonsense means. But for now, just know that make triggered execution of a compiler. In other words, it found a compiler on my computer that's pre-installed on the CS50 Appliance that we'll hand to you in Problem Set 1. And it then took hello.c as input, and produced apparently 0s and 1s as output.
And by default what it does is it saves those 0s and 1s in a file called hello. And though this syntax too might be new to you, simply by saying dot slash hello is going to be the means by which I run this program.
For now and for at least a couple of weeks, almost all of the programs we write are going to be in black and white text windows. No mice, no clicking, no windows, no icons. We're going to keep it simple and focus on the underlying ideas initially before we get to something higher level, for instance, like the break out game that we talked about in the first lecture last week.
So when I hit Enter here, it's equivalent in a text only environment to double clicking an icon called hello. What do you expect will happen when I hit Enter then?
AUDIENCE: It'll print "hello world."
DAVID J. MALAN: Hopefully it will print "hello world." And indeed it did. Now there's been some cryptic syntax here. Let's rewind for just a moment and see if we can't start inferring just by tinkering, trying and failing, as to what's going on here. What if I get rid of the backslash n? Now some of you who've programmed before probably know instantly what that means. But for those of you who've never programmed before, at least think to yourself what is going to change when I rerun this program? So I'm going to go back down to my little black and white window, which is the window in which I can compile this. I'm going to recompile hello. We're going to see the same cryptic sequence of commands. And I'm going to do dot slash hello. And now someone, if you would, who's never programmed before, what might be different this time? Yeah.
AUDIENCE: It won't stop printing "hello world."
DAVID J. MALAN: It's going to-- sorry?
AUDIENCE: It won't stop printing it?
DAVID J. MALAN: It won't stop printing "hello world." So not a bad idea. Other ideas? Yeah.
AUDIENCE: Error message?
DAVID J. MALAN: Error message, OK, could be. What else? AUDIENCE: It might print the bracket with the semicolon as well? DAVID J. MALAN: Might print the bracket with the semicolon as well. So maybe that backslash then is some kind of terminous that's important. Any other thoughts?
So all good ideas. And in fact, it's going to be an error message. That's probably most likely to be the answer in general for the next few weeks as we learn to code here.
But for now remember that computers only do what you tell them to do. Much like the ridiculous peanut butter and jelly example. Our human computers were only supposed to do what you told them to do. So in this case, if you don't tell the computer to move that blinking cursor to the next line, it's not going to do it. So when I run this program now, notice the difference.
Looks like a bug. It's an aesthetic bug, perhaps. But what is different about this output versus the last one obviously? Yeah.
AUDIENCE: It didn't do a new line.
DAVID J. MALAN: It didn't do a new line. Now those of you who have maybe made web pages before, you might know of the BR tag or the paragraph tag, very similar in spirit. A web browser will ignore you until you tell it exactly what to do. Similarly, is a language like C only going to do what you tell it to do.
So the reason that all of these examples, thus far, have kind of casually had this backslash in there, that's the means by which you express yourself as a new line character, so to speak. And you can kind of appreciate, perhaps, that this would look kind of stupid if nothing else. If I wanted a new line, just hitting Enter and then kind of butchering the code like that shouldn't really rub you the right way. And even if you don't really care at this point, you will realize that this is not a particularly good looking piece of code.
And so what the world decided years ago is that when you want to put the cursor onto a new line explicitly, you must explicitly say new line, backslash n. And there's some other symbols like that. But for now we'll just focus on backslash n.
Now let's make the program a little more interesting. I'm going to go ahead and this time open up a new file. I'm going to save this as hello-1.c. And just for kicks, I'm going to go put it into my Dropbox folder.
As you'll see in the CS50 documentation for the appliance, later this week for Problem Set 1, we'll encourage you to use Dropbox or some equivalent service, because then all of your code's going to be backed up automatically. And so that's why I've gone into this folder here.
And now I'm going to write a slightly different program. Include, standard I/O.h, int main void. And then in here printf, hello world, which is exactly the same as before.
But now I want to print something that's a little different. I want to print out, say, "Hello, David." All right, so obviously, it should hopefully be the case that if I recompile this program, rerun it, it's going to say "hello, David."
But what if I want to introduce this sort of variable, the notion of a container that's going to store D-a-v-i-d and not hard code it into my program. Well what if I start doing something like this?
String s, so this is a variable. If you want a string, a.k.a. a word or a phrase. A sequence of characters is what we in programming call a string. We're just going to generically call it s, because it's a nice simple name. But I could call it anything.
I'm going to say string s equals quote unquote, "David," semicolon. And now I want to insert D-a-v-i-d into what I'm printing. And before we saw a teaser on the screen of some special syntax that allowed us to substitute in a value. What was the special symbol a few slides ago?
So percent. At the time it was percent d for decimal number. That doesn't really seem relevant here. Turns out there's another percent symbol, which is percent s, which stands for a placeholder for a string.
So now, very simply, I'm going to go make hello-1, because this file's called hello-1.c, and hit Enter. And I screwed up. What's going on? Well, here's where we have to begin to appreciate that we're in this command line environment, this text only environment. There's no clicking on icons like folders right now. And think back a moment ago. In what folder did I say I was saving my code? So the Dropbox folder. Could have been called anything. But it happens to be called Dropbox. So I somehow need to double click on that Dropbox folder in order to get into it and get at my code called hello-1.c. In fact, let me go ahead and minimize this window for just a moment. Just like Windows and Mac OS, there are folders in Linux. There are folders in the CS50 Appliance. It's just right now we're confining ourselves to this text environment.
But if I double click on Dropbox, notice there is the file that I want to compile. But I need this black and white terminal window, so to speak. But I need, therefore, to move into that folder or directory. So slightly arcane, but you'll get used to this too.
In the world of Linux, which again is the operating system we're running inside of the CS50 Appliance, there's a command called CD. Which means I can do CD, space, Dropbox. CD, change directory. Right. Back in the day when people were inventing computers and operating systems like this, they wanted to type the fewest keystrokes possible. So the easiest way to say change directory was CD.
So if I hit CD, space Dropbox, notice what has changed here. Inside of parentheses the appliance is kind of humoring me and reminding me where I am. So the open folder is Dropbox. If I now type ls for list, again succinct, because people didn't want to type back in the day l-i-s-t. So they instead made it ls.
Enter. Notice I see two things, hello-1.c, and then this cryptic thing, source 1m. That's just my way of saying source code for week 1 Monday. That's a folder I downloaded from CS50's website that I made earlier today and just put it into the appliance in advance.
But for now the only thing we care about is making this program. So when I type make hello-1, Enter. Damn it. Something went wrong. So let's tease this apart. And unfortunately this is where things get a little stressful at first, at least if you've never programmed before.
My god. I wrote a two line program and I have four lines of errors. So what's going on here. First and foremost always scroll back up and find the first error message, because oftentimes compilers just get confused by what you and I do. Compilers are pretty dumb. They'll only do what you tell them to do. And if you confuse them, they're just going to kind of throw up their hands and maybe throw more error messages than are actually relevant.
So let's look at the first. Super cryptic at first. But notice, here's the name of the file in which I screwed up apparently. Colon 5, colon 5 just means on line 5 at the fifth character. So fifth column of characters, if you will, error. Use of undeclared identifier string. Did you mean standard n?
No, I meant string. And then it's kind of copying and pasting what I typed to really draw my attention to where I screwed up. So for some reason C, or at least the compiler does not understand the word string. And that's because we made it up. So string does not exist in C. What CS50 does, for the first few weeks only of the class, is we provide some training wheels, so to speak. And we put these training wheels inside of a special file called CS50.h.
So this is the second of two file names that apparently end in dot h. Let's rewind. Printf is a statement or function that apparently prints something to the screen. But you didn't see me implement printf, right. Someone years ago implemented printf. In what file would you wager he or she put the implementation for printf, the code for printf?
In a file called standard I/O.h. In fact, it's probably in two files, standard I/O.h, which stands for header file, and standard I/O.c, which stands for C source code. So he or she some years ago plopped the code that they wrote into that file so that people like us years later can include it, so to speak, in our own programs.
And indeed, that's what the pound sign followed by the word include does. It looks on the local hard drive, finds the file called standard I/O.h, and then effectively copies and pastes it inside of my own file. So now my program knows how to print to the screen.
So by that logic, where is string defined?
DAVID J. MALAN: CS50.h. And indeed, that's what we, the core staff, have done. We've invented a few data types, so to speak, like a string, in addition to ones you get for free, like an int. And we'll see others like a char for a character and a few more. Inside of CS50.h apparently is at least some mention of string.
So now let me go ahead and rerun make hello-1. I'll zoom in again and cross my fingers. Now by having made one change I fixed most things. But damn it. More percents than data arguments. What did I do wrong this time?
So it's still pretty cryptic. But this error is on line 7 and character 21. So let's go look up here. It's a little subtle. But if you think about what the fundamental ideas here are, perhaps we can tease this apart.
So printf is the name of the function. Parentheses, thus far, is just like what we put around the stuff we're passing as inputs to a function. All right, just an arbitrary human convention. Use parentheses. Inside of those parentheses we've been putting double quotes, and then a string like "hello world."
But in that earlier example very briefly did we look at with Scratch. And we had the percent d, what else was inside of those parentheses that I called out verbally? Yeah.
AUDIENCE: So it's what you're taking [INAUDIBLE] from within [INAUDIBLE] what these [INAUDIBLE]? DAVID J. MALAN: Exactly. So we had the percent d. But then we had close quote, comma, counter. Right. We specified what we want to do as the placeholder. So what I'm going to do here is comma, what do you want me to put? AUDIENCE: S. DAVID J. MALAN: S, because s in this case is the name of the storage container. It's the name of that glass bowl. Whereas before it was counter in that simple Scratch example we looked at. So now having made two changes, let me zoom in and try once more to compile this program.
Now I see that cryptic line. But that's actually the name of the compiler. Clang is the name of the compiler. Make is just saving me the headache of ever typing that long crazy command out. So now if I do dot slash hello-1, I should see "hello, David."
Pretty underwhelming, though, right? We could have done this a lot more simply without talking about variables and CS50.h and all of that. So let's make it a little more interesting.
In addition to CS50.h, having things like string declared, the CS50 library also has a few functions. So just like years ago, someone wrote printf and put it in standard I/O.h and some other file.
We, the CS50 staff, wrote a function called GetChar, GetDouble, GetFloat, GetInt, GetLongLong, GetString, and we put those inside a file called CS50.h and CS50.c. And we put them inside of the CS50 Appliance. And people can also download them online if they want to put them on their own computers as well. Which is to say that we have created functions that get input from the user. I don't know what all of these data types are. GetInt is kind of straightforward, like get an integer somehow from the user. And GetString is probably like get a word or a sentence from the user.
So let's focus on that. And I'm going to go back into the appliance and I'm going to go ahead and save this file as, let's call it hello-2.c as my second version. And let's make a couple of changes.
This time instead of hard coding David, which makes an incredibly consistent but underwhelming program, what if I instead do GetString? Now notice GetString has an open parenthesis, closed parenthesis, because it doesn't need any input. It's just going to go get a string from the user.
And now a word on other syntax. Semicolons just end a line of code. You don't need them everywhere. But that just means I'm done with this line of code. Let me move onto another statement or function. String declares a variable. String is like saying give me a bowl, please. And let me put a string in it.
And now the equal sign. What is the equal sign essentially equivalent to?
DAVID J. MALAN: Yeah, assigning a value. So if I, for instance, call this function GetString, and we'll see in a moment that's going to prompt the user for a string. This is like letting me then write D-a-v-i-d on a piece of paper. And when I say string s equals GetString, equal doesn't really mean equal in C programming. It means assign from the right hand side to the left hand side.
So I've gotten a string. And the equal sign means put it in the storage container called s. And Then pass this off to printf to actually do its thing.
So the end result then is going to look a little different. Let's do make hello-2. Either could work. You're following along. Make hello-2 worked. Dot slash hello-2, Enter.
I seem to have an infinite loop or something. Nothing's happening. Why?
AUDIENCE: It's making an infinite loop. DAVID J. MALAN: It is an infinite loop. It's kind of waiting for me to actually provide it with some input. So let me go ahead and type in David, and hit Enter. And now it says "hello, David." If I run it again. Let's type in Rob, "hello, Rob."
Now this is the worst user interface ever. The user's apparently supposed to know what to do. But no matter. Using these same building blocks just like in Scratch, we can solve that problem and say something like your name please, colon, space, closed quote, close parenthesis, semicolon. So a lot of again silliness with the syntax.
But notice I've just added a puzzle piece above this one and above this one. So now if I rerun this, hello-2, Enter. Wait a minute. What's wrong? It's not behaving any differently. Yeah.
AUDIENCE: You didn't run make again.
DAVID J. MALAN: I didn't run make again, right. So I've changed my source code. But again, there's that flow. Source code through the compiler gives you new object code, or 0s and 1s. So I need to actually rerun make hello-2 Enter. OK, something seems to have happened. Dot slash hello-2. Your name please. And to be clear now, why is the cursor on the same line? Exactly. I didn't put the backslash n up here in my code. So now I can write something like Daven, Enter. I can run it again and type something like Gabe, Enter, and we get a different program again and again and again.
Now ultimately we're going to need to use a few different capabilities. We need to introduce ultimately some conditions to do things conditionally. Maybe loops so we can do things again and again.
Maybe it would be nice if we could implement our own functions, like we could implement our own printf or our own version of GetString and GetFlow, because ultimately even using this command line environment can we do even the most visually interesting of things.
Indeed in conclusion let me do this. I'm going to go ahead and close these windows and open this icon here, which is just a bigger version of that embedded terminal window. So Gedit has not only the place for my code, but also a built in terminal window, the black and white window where I can run commands.
I just happened to open a bigger version of this. And now I'm going to go into the folder that I've already put in advance on the course's website. And I'm going to go ahead and open a file called thadgavin.c, which was written by someone else.
And if we look at this, this is not the kind of code we'll be writing since the goal of this code was to write the prettiest looking code that he or she could, irrespective of whether or not another human being could ever understand this code.
Indeed, every year there's what's called an obfuscated C contest, which is for real geeks who write code that no one else can read, but that does something either really simple or really amazing. And we thought we'd conclude with this look at something that's pretty amazing you might have seen once before. But we'll end on this note, dot slash, thadgavin. This then is what awaits.
That's it for CS50. We will see you on Wednesday.