>> DAVID MALAN: This is CS50, and this is the start of week one.
And what we thought we'd do is pull back the curtain of a device that many
of you have probably walked past now for years, but perhaps never quite
known what it is.
So this device will probably look familiar to most of you by now.
>> [VIDEO PLAYBACK]
>> -A triumph of mathematical and mechanical skill is this great new
automatic calculator at Harvard University.
Intricate problems in mathematics put through the machine in coded form on
tape are accurately solved in a minute fraction of the time required for
human calculation.
>> Designed to expedite all forms of mathematical and scientific research,
the giant mechanical brain will work for the United States
Navy until war's end.
>> [END VIDEO PLAYBACK]
>> DAVID MALAN: So that's what you see in the Science Center, which is where
that machine is now housed.
You actually only see part of it.
Only a portion is currently on display, and that's a device called
the Mark I, and it was essentially a really huge calculator.
It was succeeded by the Mark II, and it's from the Mark II that we actually
get a bit of lexicon.
>> This is the first recorded instance of something you're about to become all
too familiar with over the course of the semester--
namely, a bug.
So it turned out at one point the Mark II device was not functioning
properly, and upon closer inspection, there was indeed a bug-- a moth--
that moth--
inside.
And ever since, we have referred to mistakes in programs as bugs.
So more on that to come.
>> Speaking of video as well, if you haven't already, know that CS50 is
obviously filmed.
The lectures typically go up right after class in sort of a pre-release
unedited format, and then a day or two later, we put up a higher resolution
and higher quality version.
You may recall Vanessa, who had the fortune of playing with Google Glass,
which I remembered to charge this time.
So if you want to come up after class and play, they should work today.
And if you'd like to see what Vanessa saw the other day, what you'll see
here-- let me raise the volume here on my laptop.
>> [VIDEO PLAYBACK]
>> -So let me queue this is up.
All right.
So touch this.
All right.
OK, Glass.
Record a video.
>> All right.
Vanessa?
>> [END VIDEO PLAYBACK]
>> DAVID MALAN: All right, so the reason that it didn't work for most of you at
the end of class was because I proceeded to record an hour of that
footage pointing at myself after I put the glasses down.
So today, I'll leave them off, but thank you to Vanessa for being such a
good sport.
>> Meanwhile, a couple of announcements before we forge ahead today.
So one, CS50 has a tradition of doing what we can to try to make a very
large class feel smaller.
And toward that end, most every Friday, we gather at a restaurant in
the square, Fire and Ice, with 30 or 40 of your classmates, myself, some of
the teaching staff, and we just have a very casual lunch.
We often invite friends from industry, alumni of the class, alumni of the
college, really just to chat each other up, talk about life after
college, life in college, and the like.
So if you would like to partake in this first such lunch this Friday at
1:15 PM, head to that URL at some point.
>> Space is limited, so we'll do first come first serve.
But we'll do this again on a recurring basis, so not to worry if you don't
make it into this first batch.
>> Now, in terms of the resources provided curricularly by the course,
there's a whole slew, and if you haven't pulled up the course's website
already, in particular, under lectures, you'll find that everything
we did last week is there, both in video and some sort
of electronic form.
But beyond the videos, you'll find that there are now full text
transcripts, for instance, of every lecture.
>> So if we actually go back to this screen here with Vanessa, and navigate
to the bottom corner of the screen, you'll find that actually, not for
Friday's lecture, but if we go back to Wednesday, since this feature takes a
few days, you'll find that you can actually pull up, for better or for
worse, every word that I or a volunteer on stage said.
And more than just that.
You can actually search it, you can click on any of those sentences, jump
to that point in the video, all toward and end of making the material all the
more navigable.
>> But if you'd rather not follow it in such detail, you'll find, for
instance, this little menu here for 1.5x speed, 2x speed, or i f I already
speak too fast for you, 0.75x speed.
So realize all of that is available there for you.
>> But beyond that, for lectures, realize that we also make these resources
available as well.
Slides, example code, anything that I do on stage or in advance of class,
we'll post there so that you can play along at home or in Sanders.
But what we also thought we'd do this year for the first time is also
provide you with walkthroughs of these examples.
Increasingly, as I've reflected on the value of CS50's lectures, I've begun
to question just how useful it is for you guys to sit there, for me to stand
here, and for me to talk at you, particularly as the material, the
examples, get more complex.
Because invariably, after some number of minutes, someone will zone out, and
then you miss some key insight of some example, and then you're essentially
gone for the remaining portion of class, which is not the best use of
your time, or, really, ours collectively as a class.
>> And so what we thought we'd try to do, particularly for some of the more
complex examples that I might not do justice to in class, that we might not
have time for in class, or you might just zone out during, is we'll produce
a series of walkthroughs of these examples so that if you go to, for
instance, last Friday's video page here under examples, you'll see that
for Friday, there's now this link to walkthroughs.
And the format of these will change over time.
For now, we're using a simple YouTube playlist.
>> But what I did with our production team afterwards was walk through each
and every one of those examples again, hopefully pointing out in much greater
clarity exactly what the takeaways could be or should be from each.
So you'll find, for instance, from last week, all of the scratch examples
we went through are done linearly in that fashion.
So feel free to engage or not engage in this material.
Really a theme of this particular course is that there's probably more
in the way of resources than you could possibly absorb over
the course of a semester.
>> But that's deliberate.
It's meant to be so that you as individual students can self-select
based on your learning style.
So if lectures work for you, great.
If sections work for you, great.
If walkthroughs work for you better, great.
It really will be up to you to choose what resources work best for you.
>> Finally, thanks to an alumnus of the course, we also provide a canonical
set of scribe notes.
So rather than have you guys heads down in lectures scribbling down
things that come up in class, we, the staff, will provide you with what we
feel are a canonical set of notes to free you of that distraction, so that
if you are here and engaged, you are truly engaged with what's going on,
with your classmates are doing up on stage, with what's up on the screen,
and not simply writing down verbatim what happened to have been said.
So realize all of those resources are available to you.
>> Sectioning, in answer to an FAQ, will begin this Wednesday.
We essentially wait until after most other classes are done so that we can
minimize the number of changes that we need to do.
But coming up this weekend, starting Sunday, will be a one time instance of
what we call super sections.
These will be filmed for those who can't attend, and essentially, the
course's heads will walk us through some of the C portion of the course,
toward an end of problem set one, which is going to be our first C based
problem set.
And these will just be optional sections in anticipation of what will
become recurring sections led by the course's 50 plus
teaching fellows weekly.
On Sundays or Mondays or Tuesdays, we have 90 minute sections in a very
traditional sense, which will be opportunities for hands on and more
intimate review of the course's material.
>> Without further ado, let me to introduce the course's heads, who are
behind the scenes with me, making everything happen.
If those here today could join me, Rob and Lauren and Joseph and Lucas, all
of whom have been with the course for some time.
RJ is the third such member of our team.
He couldn't be here today, but he asked me to show a photo of him.
I'm not sure he wanted this one, but there he is.
>> And let me just allow the team to say hello and introduce themselves so that
you get to know them as well as your own teaching fellow this term.
>> LAUREN CARVALHO: Hi.
My name is Lauren Carvalho.
I'm a resident tutor in Leverett House.
I'm also super excited to be a head TF for CS50 this year.
>> CS50 is a very challenging class as well as a very large class, so if you
have any concerns, if you feel like you're falling behind, feel free to
reach out to any of us at heads@cs50.het, or to me, if you'd
like to play with my golden retriever puppy.
Here's Lucas.
>> LUCAS FREITAS: Hey, guys.
My name is Lucas Freitas.
I'm a junior [INAUDIBLE]
computer science and linguistics.
I'm actually from Brazil, and I also realize east Asian studies.
So if you have any questions about computer science or language or
anything, just let me know.
Or CS50, especially.
>> JOSEPH ONG: Hi.
I'm Joseph.
I'm a senior studying computer science in Elliott House.
So, let's see.
You'll see me around carrying one of these huge cameras.
I'm the resident staff photographer for 50 as well, and students often
sometimes mistake me for an Asian tourist.
>> So if you see me with a camera, don't run away.
Just smile and don't be shy.
And I hope you enjoy 50.
I enjoyed it very much when I took it, and that's why I've been TFing for
these three years.
>> ROB BOWDEN: Hi.
I'm Rob.
This is my fourth semester with CS50.
I just graduated in May.
I was in Kirkland.
I'm excited for this semester, and I hope you are too.
>> DAVID MALAN: Thank you to this year's heads, and to RJ as well.
>> So a few final notes on resources.
One, p set 0 is already up.
This one exists only in standard edition, which is meant for the
entirety of the class.
Starting with p set 1, there will be hacker editions of most problem sets,
which, again, will cover the material with a bit more of a challenge, while
still touching on some of the same topics.
Do take a look at the course's website for that specification for p set 0.
>> Office hours, too, will begin this week, tonight, tomorrow, Wednesday,
and Thursday in various dining halls on campus.
Check out cs50.net/ohs for the office hours for the course, and realize this
will be very casual opportunities, certainly, at the start of the
semester, to come by with your laptop during brain break.
Bring some friends and chat up the course's TFs and CAs with any
questions that you might have.
>> And I can't emphasize enough, even for scratch and problem set 0, there
really is no dumb question.
There is no student too uncomfortable to ask questions.
Please feel free to take advantage of this resource.
It will be there recurringly throughout the semester,
as will CS50 Discuss.
This is the course's online discussion forums at that address there.
>> What we'll also do during lectures is also monitor this, thanks to the
course's teaching team.
And so if you have a question during lecture, because I said something
poorly or not at all, by all means, ask that in real time if you have your
laptop or phone on the website, and we'll do our best in near real time to
respond to that.
>> Are there any questions about CS 50?
Then one last notes of mine.
That issue of SAT UNSAT.
As you finalize your study cards, if you haven't already, realize that the
overarching vision of SAT UNSAT, available for all students, is really
to help take the edge off of a course like this, so to speak, so that if you
find yourself late at night, 2AM, 3AM, really banging your head up against
the wall where you know you're 99% of the way there to completion, but you
have so many other things to do, I think a better use, often, of
students' time is to indeed move on to those other things so that you can
optimize your time all around.
And SAT UNSAT is one mechanism via which you can take comfort in the fact
that 99% of the way there is still pretty darn good.
>> Similarly, too, if you've come into the course with no prior background or
not nearly as much background as you think your classmates might have, true
or false, realize that this is an opportunity to get your hands dirty
with the course, put your toes in the water, so to speak, much like I myself
did years ago with pass/fail.
As I said last week, had this course, like others, not been available
pass/fail at the time, I probably would not have ever stepped foot in
the class, even though I did, five weeks later, change my mind, which you
may as well, and switch over to or from letter graded status.
>> All right.
So we looked at things like this on Friday, and we're very quickly going
to transition to something that looks more cryptic today.
But it's nonetheless the same fundamental idea.
What was the general term that we used to describe a puzzle piece
that looked like this?
>> So, a statement.
And you can call this any number of things.
But we'll just call it a statement, and a statement
just tells the program--
or in the case of Scratch, the sprite--
to do something.
Say hello in this case.
>> Something like this we instead called what?
Yeah, so a Boolean expression.
A Boolean expression is just something that is true or false, and so Scratch
draws them with this shape, but also with a question mark to convey the
idea that the answer to this question is either true or false, yes or no
one, 1 or 0.
>> And we use these Boolean expressions inside of constructs like these, which
we called what?
So, condition or branch.
And the condition you see here in Scratch has a little placeholder for a
puzzle piece of that shape, the purpose of which is so that you can
drag it and drop it on top and then dictate to the program if this Boolean
expression is true, do this set of instructions, this set of statements,
else do this other set of instructions.
>> And recall that you can nest these things.
Even though there's not much room visually in that puzzle piece, Scratch
will grow and shrink to fit whatever puzzle pieces you drag and drop in
there so you can actually nest this and have a three way fork in the road.
If, else if, else.
And you can even go beyond that by nesting further and further as needed.
>> So lastly, we saw constructs like this, otherwise known as a loop.
And this is just something that does something again and again and again.
In this case, it happens to be a finite number of times, but we also
saw an example where the number of times came from a variable, so it
could change.
And we also saw another block altogether called a forever block that
allowed us to loop infinitely long.
>> So one other construct that Scratch 2.0 has-- and those of you more
comfortable with prior background are already well familiar with this
construct--
these things we called what?
So we called these functions, otherwise known as procedures.
There's a slight semantic difference, but we'll call them just functions.
>> And a function, in the case of Scratch, is a custom puzzle piece that
you yourself can create so that you have a new puzzle piece that, in this
case, would be called cough, that appears among all of the other
available puzzle pieces in Scratch.
Even though MIT didn't invent this puzzle piece, you did.
>> So a function allows you to create new behavior, give it a function name, and
then call it, so to speak.
Really use it in programs again and again and again without having to wait
for someone like MIT down the road to invent new functionality from Scratch.
You yourself can build these puzzle pieces yourself, and then reuse them
in your own programs.
And we'll see a recurring example of that today and onward.
>> So today, we transition now to something more arcane.
But at the end of the day, something more powerful and expressive and more
representative of the path that we'll be on all semester throughout a number
of languages--
that of code or source code.
And source code, it turns out, is not what a computer
typically actually runs.
Source code looks something like this.
>> So this is, again, perhaps the simplest program we can write in a
language called C. We will start to tease this apart before long, and if
this really does look like Greek to you right now, trust me.
Within just a week or two's time, this will be all too familiar and actually
quite representative of increasingly complex examples with which you'll
also get more comfortable.
>> But you can't just run this kind of code usually.
You have to turn it into something that the computer itself understands.
And so for that, we need something we're going to call a compiler.
A compiler is a program that takes source code, like you just saw, as
input, and it produces zeroes and ones as output, otherwise
known as object code.
And it's those zeroes and ones that might look quite like this that
ultimately are understood by your Mac or your PC.
>> If you've ever heard the marketing expression Intel Inside, that just
means that a company called Intel has manufactured the
brains of your computer--
otherwise known as the CPU, central processing unit--
and that's just the thing that understands patterns
of zeroes and ones.
And so by converting source code into object code through this process here
that we'll do with a couple of commands in just a bit, you are
creating patterns of zeroes and ones that the employees at Intel have
decided represent certain statements.
>> Now, I don't quite know which is which by just glancing at
these zeroes and ones.
Most humans these days don't.
But somewhere in there is a pattern of zeroes and ones that represents the
statement print.
Somewhere in there could be a different set of zeroes and ones that
represent the notion of forever or repeat 10 times or even meow, if it's
actually a program that can include some sounds.
>> So in short, humans have just decided, much like we did for ASCII--
for letters of the alphabet last week-- humans have decided that even
more complex patterns of zeroes and ones represent more complex behavior
like printing or saying or meowing.
And so for today, and largely onward in the semester, we'll take for
granted that someone has figured out how to do that mapping.
But we, consistent with this idea of layering on top of the work of people
who've come before us, will take for granted the fact that this is going on
underneath the hood.
But we're much more interested in building things that are more
interesting on top of all this.
>> And so indeed, the first program we wrote in Scratch was this super simple
one, Hello World.
And you can think of this as being the main program that governs Scratch's
behavior as of last Friday.
>> Today, we're going to start to translate Scratch puzzle pieces into
source code in this language called C so that what looked like this on
Friday, starting today onward, is now going to look like this.
Admittedly more cryptic.
It's pretty distracting and sort of mind numbing to see all of the quotes
and the semicolons and the parentheses and so forth.
>> But if you start to ignore anything you don't understand, and really look
at the essence of the white text on the screen, surely you see, like I do,
"hello, world." That lines up with the purple statement up top, and it turns
out that the keyword main is going to line up conceptually with when green
flag clicked.
>> So in other words, whereas in Scratch, we implement this Hello World program
with those two puzzle pieces, in C, we're going to implement it with these
four somewhat cryptic lines.
But as we dive deeper into this, you'll see that each one of those
white characters on the screen actually has some significant meaning,
and most of our programs are going to look structurally quite the same.
So a statement, to be clear, what looked like "say hello world" on
Friday is going to start looking like this on Monday.
printf ("hello, world").
And there's some weird backslash n, there's parentheses, there's
semicolons.
>> But at the end of the day, the key takeaway today is that all we're doing
is translating things from picture form to text form.
So a loop that looked like this last week is going to now look
something like this.
>> And let me disclaim, just like in Scratch, there's different ways to
achieve the same goal.
Same in C. I'm not showing you the only way to do things, but one way to
translate this forever block to a loop in C.
>> This one here, repeat 10 times.
This one really kind of bends the mind the first time you see it.
But to implement that henceforth, we're going to use a
for loop, so to speak.
And clearly there's some syntax that's a little complex there with I and
equal and 0, but once we dive into that today onward, you're just going
to see that that's all simply doing some basic arithmetic to get us from 0
on up to 9 or 10 in this case.
>> Lastly, with variables, you'll recall that a variable is a piece of storage.
Well, what we had on the left there last week is going to look a little
something like this this week.
But again, more on that to come.
>> We had Boolean expressions, which last week, looked like this.
This week onward, they're going to start to look a little like that.
>> And this one's actually a little reassuring.
It's actually almost as simple as the green blocks.
But here we have, again, some new syntax with ampersands, but you can
perhaps guess whereas in Scratch, you say A-N-D, apparently in C, we're
going to say ampersand ampersand.
But all of this will become natural before long.
>> Conditions.
Last week, it looked like this on the left.
This week, on the right, it's going to look like that.
But again, the key takeaway here is that as you see things like that on
the right, and especially if less comfortable, it's sort of over your
head, you get a little overwhelmed that I really have no idea how I could
possibly come up with that from scratch--
no pun intended--
just realize that it's really the same idea that probably felt so much more
natural the previous week.
And even if you haven't dived into problem set 0, I daresay you'll find
that the puzzle pieces are fairly intuitive, or at least
become so over time.
>> So let's dive in.
Let's get our hands dirty here with this first simple program.
And simple is, of course, relative.
To do this, I'm going to change screens to that device I called the
CS50 appliance.
The CS50 appliance is a piece of software that's running inside a
window on my Mac, and you can also run it in a window inside of your PC, that
allows me to have the same exact computer environment as every TF, as
every CA, as every student in the class.
Because the CS50 appliance is literally another operating system
called Linux--
specifically Fedora Linux--
and we, the staff, have configured the installation process for this
operating system in such a way that one, there's a little CS50 logo in the
middle, two, there's a custom menu at the bottom, and three, pedagogically,
we've simplified the user interface as much as possible so that when you
first boot this thing up on your own Mac or PC, you have the same
environment as everyone else, and you have all of the tools that you need
for the course without having to figure out how to install each and
every tool individually.
>> So this looks relatively like Mac OS or Windows.
In the bottom left hand corner, you essentially have a Start menu of
sorts, or an Apple menu.
And then you have what we suspect will be your three most common icons.
One is Chrome.
There's a browser inside of the appliance.
>> Two is a program called gedit, which we saw briefly last week, which is
just a graphical editor.
And three is a Terminal program, which is a black and white window from
yesteryear that allows us to type more arcane but more powerful commands at
our keyboard.
So I'm going to go ahead and click on gedit, and rest assured that problem
set 1, to be released on Friday, will walk you through all these same steps.
>> Notice that I have a pretty simple user interface with three parts.
On the left, I have a big empty window called source code.
That's where I'm going to see a summary of any code that I write.
On the right hand side, I have a big tab, currently unsaved, and that's
where my code's going to go.
And on the bottom, I have an embedded Terminal window, a black and white
prompt at which I can type commands to compile and to run my programs-- that
is, to make and to run my programs.
>> So let's start simple.
Let me go up to File, Save.
I'm going to go ahead and go into jharvard.
So the appliance, irrespective of what your name is, belongs to a guy named
John Harvard, who has a jharvard home directory inside of which all of your
files will go.
It's your own copy of his account, so we're not all sharing
the same hard drive.
You just have the illusion of John Harvard's own.
>> In advance, I actually installed Dropbox.
The appliance comes preprepared with Dropbox so that if anything goes wrong
during the semester, if you've configured your appliance with a free
Dropbox account, all of your files will be automatically backed up so you
can very easily recover them.
And I'm going to go into my Dropbox directory, and there's already going
to be a couple other things there.
>> But I'm going to go ahead and go to File, Save.
I'm going to go into jharvard, Dropbox, and up here, I'm going to
give my program a super simple name, hello.c, and then save.
So I have the same interface now.
The only thing that's changed is the tab up top.
So I'm very quickly going to recreate that program from the
slides a moment ago.
>> So into main(void), and then printf, "hello, world," a somewhat cryptic
backslash n, close quote, close parenthesis, semicolon, and
then Ctrl S to save.
And notice as an aside what just appeared here on the top left.
And this is not useful today, but will be useful over time.
You just see, again, a summary of the code that you've written.
And because I wrote something called main, that's why the word main popped
up over there.
But for the most part, we don't need that today.
>> All right, so I claim that this is my first program written in a programming
language called C. We know from Friday that what this thing is going to do
very uninterestingly when I run it is just display--
in other words, print, as a computer scientist would say--
hello world on the screen.
So to do this, you'll notice that I have to go down here to my terminal
window, and now I have a blinking prompt.
But this is now a keyboard version of my John Harvard home directory.
It's a keyboard way via which to navigate my hard drive.
>> So I can't actually type like I did last week make hello, because it's
going to say no rule to make target--
something's wrong.
I don't quite know what that message means, but that's because by default,
when this black and white window is opened, I'm inside of John Harvard's
home directory.
But where did I save that program?
In my Dropbox folder.
>> Now, all of us have grown up with computers where you just double click
on a folder's icon, it opens up, and voila.
That's where the file called hello.c would be.
So in fact, let's do that.
>> Let me go ahead and minimize gedit just like you would on
Windows or Mac OS.
Let me go into the Home folder at top left.
I'll see here a bunch of folders.
Here's my Dropbox folder.
The checkmarks means it's been synced.
And there's a few things in here, but notice that hello.c is
indeed right there.
>> And so in Mac OS or Windows, normally, I'd double click that, it opens up in
whatever program, and I'm good to go.
But now, at least starting today, we need to take a step backwards just to
give you some basic tools for your tool kit with which to do more
powerful things before long.
>> So at this prompt, I have to do the equivalent of double clicking the
Dropbox folder.
And to do that, I'm going to type CD for change directory, Dropbox, enter.
And now notice the parentheses at my so-called prompt now says that I'm in
tilde, the little squiggly symbol at the top left of
most keyboards, /Dropbox.
This is just shorthand notation for saying human, you are now inside of
the Dropbox folder, just as though you had double clicked on it.
>> So now if I go ahead and type make hello, enter, I see a very cryptic
command, but not an error message, it seems, and then
another blinking prompt.
And if you recall, ever so quickly on Friday, we did this. ./hello.
>> What you're about to see is the result of my running this program, or, in Mac
OS and PC terms, if I'd normally double click on a program-- a .app
program or a .exe, it opens a window and runs.
Typing ./hello is just like double clicking a program to run it, but
using my keyboard.
>> Enter, hello world.
And that's it.
So let's tease this apart to so it doesn't feel too much like Greek.
>> So ./ means what?
Anyone who's ever navigated a keyboard environment like this, what's dot
referring to?
The current directory.
>> So the current directory is just a synonym for a folder.
So by saying dot slash, I am saying, look for a program called hello in the
current directory--
in the current folder.
In other words, my Dropbox folder.
And that's why hitting Enter thereafter actually runs that program.
>> If I now type LS, notice that I see everything in my Dropbox folder.
Getting Started.pdf, which Dropbox gives you for free, my photos
directory, a public directory, Unsaved Document 1 which I made by mistake a
moment ago, foo.c, which I made earlier today, and then two lines--
hello.c and hello.
>> Hello.c is the program I wrote with gedit, and what is hello in green?
The hello in green is the executable.
In other words, when I ran this command a moment ago-- let me roll
back in time--
when I ran make hello, that's the middle step here.
And it took hello.c as input--
AKA source code-- and it produced a file called hello as output, which
contains all of these zeros and ones.
And that's why, in the end, I can actually run a command like ./hello.
Because of Intel Inside, my operating system, Linux, opens up those zeros
and ones, feeds them into the brains of my computer--
the CPU--
and that CPU knows, oh, here's a pattern of zeros and ones that says
print the following thing on the screen.
>> What's the following thing?
Quote unquote, "hello world."
>> All right, so a quick summary of some of these commands so that we can start
taking them for granted.
Let me skip ahead to where we left off.
And you'll recall that we had CD at our back end a moment ago.
These are, I dare say, for today's purposes--
maybe this week-- the only commands we might need to type at the command
prompt besides make.
>> So CD stood for what?
Change directory.
It's just the equivalent of double clicking on a folder.
And as an aside, if you get lost ever inside of your
appliance, such that Dropbox--
I want to get back to my jharvard directory--
when in doubt, CD, enter, with no other words, and it just zips you back
to where you started, which is a nice little shortcut.
>> All right, so now other commands.
LS.
I typed that a moment ago.
What do you think LS stands for?
>> So, list, right?
Back in the day, when humans were coming up with these commands, they
were trying to be efficient, and rather type out L-I-S-T enter, they
decided LS.
That's fine.
It sounds enough like list.
Let's just type LS enter, and that lists the files or folders in my
current folder.
>> mkdir, you can probably guess.
Make directory.
So you don't go to some file menu and choose New Folder in this environment.
Rather, you type the command mkdir space and then the name of the folder
that you want to create, and it will be done.
>> rm, you can probably guess.
Remove or delete.
So if you want to delete a file, you'll see in the problem set how you
can do this.
And rmdir, remove directory.
>> So, again, I'll tend to fly through some of these details, one, because
they're not all that much fun to spend time on, but two, we'll provide you in
much greater detail in any of the course's problem sets, particularly
the standard editions, any of the steps that you might need to type so
that you get more and more comfy with these things.
>> But now let me tease apart one other thing.
When we typed make hello a moment ago, we saw a cryptic sequence of commands.
We saw, again, make hello.
OK, let's do this.
I was about to make a mistake, but [INAUDIBLE] now admit I was about to
and do it deliberately.
>> What did I do wrong just now?
Yeah, so I'm not in the Dropbox directory, because a moment ago I
typed CD and just hit enter.
That zoomed me back to where I started, which is not where my file
is, so I really need to do CD Dropbox enter, and now I can do rm hello
enter, remove regular file hello.
I don't know why it's so cryptic.
That just means are you sure you want to delete hello?
You can type y for yes, enter, and now it's gone.
>> And now if I type make hello again, it's going to recreate my object code,
recreate those zeros and ones.
But what is the deal with this very long command?
Well, we'll tease this apart before long, but make--
I told a bit of a white lie before--
is not a compiler.
Make is a program that automatically figures out how to run a
compiler for you.
>> So the command that you really should be running in order to convert hello.c
into hello actually looks a little more like this.
And God forbid we have to actually remember to ever type that whole
sequence of commands out.
The purpose of make in life is to remember that degree of complexity for
us and automate what would otherwise be very tedious steps.
>> But I can approximate that.
So per this little cheat sheet here, I can approximate what that command is
doing by simply typing clang, which is the name of the compiler we'll be
using this semester.
If any of you have ever done iOS development for the iPhone or iPad,
Clang is the program you've used within xcode to make your apps.
Clang, though, is just another program whose name we can type at the command
prompt, and by introducing this now, I can introduce something that's a
little representative of commands to come.
So let me do this manually.
>> So rm hello, and now notice in case I, just as a sanity check--
./hello, what do you think I should see if I do this?
Some kind of error.
No such file directory.
So that one's actually pretty direct.
>> So now let me clear the screen.
Control S just clears the screen to remove some distractions.
And now I'm going to manually type clang -o hello hello.c.
So what is this about to do?
Exactly the same thing that make did for us, but I did throw away some of
the superfluous words.
>> Clang is the program that's going to take a .c file as input and produce
zeros and ones as output.
But -o henceforth will call a switch or a flag.
This is just techie jargon for saying this is a word you type at a prompt
that somehow influences the behavior of the program you're running.
So does anyone want to guess what -o conveys?
>> Output.
It's just someone decided -o means output a file called hello and take as
input whatever the last word on the prompt is, hello.c, so that if I hit
Enter now, nothing seems to happen.
And oddly enough, in this world of programming, if nothing happens,
that's probably a good thing because there's no error
message on the screen.
Hello, Enter.
My program has now run.
>> But I can do stupid little things like this. clang -o, hihihi, hello.c,
enter, and now I have an identical program named hihihi.
It behaves the same, but the name is different.
>> So this is only to say that if you want to influence the behavior of a
program, we're going to increasingly see things called
command line arguments.
This is a line.
You're typing commands.
Ergo, command line.
And command line arguments are just words or little pieces of syntax that
somehow influence the behavior of programs that we are running.
>> All right, that is perhaps the most boring program we can write and see.
Let's now do something more interesting.
Let me go ahead and create a new file.
I'm going to go ahead and save this in John Harvard's home directory as,
let's call it custom.c, to be a little custom program.
I'm going to zoom in at my top of the tabs here and start again.
>> Include standard stdio.h.
More on that in a moment.
int main(void), more on that in a moment.
printf ("hello, David").
Much more interesting, right?
>> So now if-- not because of my name, just because it's different--
make custom enter.
Custom, hello, David.
But even that, too, is a white lie.
This program might as well be called david.c, because it's not custom
behavior at all.
Really, I want a program that asks the user for their name.
>> So how do I do that?
Well, let me scroll up over here.
And before I use a statement in Scratch, before I use printf,
otherwise known as a function, let me first ask the user for some input.
>> So to do this, I need to do a couple of things.
One, I need a place to store the user's name after I've asked him or
her for it.
What construct do I need to use probably to store a value like that?
A variable.
>> So unlike Scratch, where you just get a nice puzzle piece, here, we have to
use, obviously, words to express ourselves.
And henceforth, anytime you want a word or a phrase, in programming,
we're going to call that a string.
So a string is a word or a phrase.
It's a sequence of zero or more characters, more formally.
>> And if you want such a variable to exist, you have to say give me one.
And the way by which you do that is to say string s semicolon, where s can be
anything you want.
In fact, s is a little underwhelming.
Let me go ahead and more clearly call it name.
Give me a string called name.
>> But now, what is the person's name by default?
Well, if the human doesn't type anything in, obviously, there's going
to be no value in name.
And it turns out in C, if you don't give a variable a value, it's going to
have a garbage value.
It might have some random zeros and ones, so you're just going to see
garbage, truly, on the screen if you try to display it.
>> But I can fix that by saying, actually, store inside of name the
following value.
The way you store values in variables is if you have a variable name on the
left, you have to somehow put what you want to store in it on the right, and
the syntax for that's going to be as follows.
>> We've not seen this before, but take on faith for a moment that there
exists another statement in the world that I'm going to again call function,
and this function is called get string.
And it does literally that.
It somehow gets a sequence of characters from the human and then
does something with it.
>> And this does look like cryptic syntax.
Normally, if you're using equal signs-- you're in the world of, say,
algebra, and you're actually saying x equals 1.
Here we're not saying that name equals get string.
We're saying store in name what's on the right hand side
of that equals sign.
The equals sign represents the assignment operator.
It's the syntax that says put what's on the right hand side inside what's
on the left.
So get string is a new piece of functionality that not only does
something--
asks the user for a string--
it also returns it, so to speak.
>> So let's see what's going to happen here.
I'm going to go ahead now and zoom out, and I'm going to get a couple of
mistakes at my prompt.
I'm going to type make custom, enter.
Wow.
A whole bunch of mistakes.
>> All right, so this is kind of overwhelming, certainly at first, but
it turns out all of these very admittedly cryptic error messages are
referring to some patterns that we'll start to see over time.
So this one I'll pluck off, because it's first.
Use of undeclared identifier string.
>> That just means that gedit, or Clang, in this case, doesn't know
what I mean by string.
And that's because in C, the programming language, there actually
is no variable of type string.
C does not know that strings exist.
>> But we, CS50 staff, do.
And so in advance of class, we created a file called CS50.h.
And inside of this file-- and more on the syntax to come--
inside of this file is a definition of what I just defined
verbally as a string.
So simply by concluding this one line of code in my program, I am now
teaching gedit and make and Clang all in turn what a string is.
>> I don't know how yet.
In a few weeks, we'll peel back that layer.
But for now, know that this teaches the compiler what a
string actually is.
>> So now let me clear my terminal down here.
Let me re-run make custom, and I solved all of those problems.
So this, too, is going to be a recurring theme.
I only wrote a 9 line program, and yet I had 18 lines of errors.
That's kind of intimidating.
But realize that they often cascade such that one little mistake triggers
the confusion for the compiler, and it looks much worse than it is.
>> So now an astute observer will know that I'm getting a string from the
user, but I'm not actually doing what with it?
I'm not actually doing anything with it.
So if I actually run custom again, it's going to just sit there.
What's going on?
My program seems to be stuck on what line, would you say?
>> So it's actually stuck on line 7.
And we've not seen this before, but on line 7, there's, again, this
statement, get string.
And it does what it says.
It's waiting now for the human to take the string.
This is completely unintuitive to me, the human, because I haven't been told
what to do.
>> But let me go ahead on faith and let me pretend to be Rob.
Enter.
That's not the program I intended.
So we have our first blog.
>> But let's try to reason through why this is the case.
First, let me fix this first problem.
Let me add a line here that says printf, what is your name?
Let's just do close quote, close parenthesis.
>> Now let me re-run the program.
Problem.
I'm not seeing that.
>> Right.
I didn't recompile it.
So in a language like C, because you change the source code does not mean
that you have changed your object code to generate new object code,
new zeros and ones.
You have to recompile.
If I now re-run custom, enter, ah, a little more clear.
My name is Rob.
Still buggy.
>> And I'd argue there's a couple bugs now.
This just looks hideous, frankly.
Like, minimally, I should have a space there, or move the
cursor to the new line.
>> And actually, new line.
That's kind of a key phrase here that we've seen but not defined.
The way in which you get a text to move onto the next line,
you don't do this.
This is confusing to the computer.
This looks like two separate lines of code, neither of which
is perfectly correct.
You instead have to be more explicit and say, give me a new line, which we
represent in c with backslash n.
>> So there's a few such characters.
Backslash n will fix that aesthetic.
Let me recompile my program.
Let me re-run my program.
>> This looks better.
It's still pretty primitive, but it's also still buggy.
So there's one--
even if you've never programmed before, you can probably take a guess
at why this program is still flawed.
What do I need to do that I haven't done yet?
>> So I have to actually do something with name.
Just like in scratch, you might actually assign a variable like n or
counter like we did last week-- a value.
But if you don't do anything with it, nothing's going to happen.
>> All right, so let fix.
Let me type my variable there, and let me go down here.
Let me recompile, let me re-run, Rob.
Now I'm just an idiot, right?
>> So this is not really getting me any further.
But that was a perfectly natural instinct, I'd argue, right?
If the variable is called name, and I want it to go there, why don't I just
type it there?
>> Well, of course, we have to distinguish now
between what is a string?
Notice that quote unquote here, hello name?
This actually itself is a string.
It's hard coded.
It's not from the human.
It's from me, the original programmer, but it's still a string.
>> So if you literally write N-A-M-E inside of a string, what's going to
get printed?
Well, N-A-M-E. We instead need to tell printf, don't print
out literally name.
Print out the value of name.
>> And to do that, we do the following.
And this is just a human convention.
We instead say %s, and that stands for string.
And then at the end of my close quotes, I'm going to put a comma and
pass a second argument into this statement.
I'm going to type name there.
>> So now notice we have a slightly new syntax.
We still have two parentheses, and henceforth let me say that the stuff
between parentheses represents arguments into a function--
inputs that are somehow going to influence its behavior.
So by that logic, how many arguments does printf seem to be
taking at this moment?
>> So it seems to be taking two, and that's indeed implied
by the comma here.
So this is one argument in between quotes.
Even though it has a comma in it, everything's in quotes, which means
it's one long string.
Comma, variable name.
>> So now let me zoom out.
Let me re-run it.
Rob.
Damn it, what did I do wrong?
>> All right, so you have to recompile.
So again, easy mistakes to make early on again and again.
So now re-run custom, Rob, enter, and voila.
We now have a custom program.
>> So I now have a program that using a few characteristics
that are worth noting.
One, I'm using CS50.h, otherwise known as the CS50 library.
And inside of the CS50 library are functions that other people wrote--
namely the staff--
for you to use, and get string is one of them.
>> Inside of line 2 is stdio.h, and why is this there?
Take a guess.
What exists inside of standard IO parent?
Printf.
So printf, you don't see it anywhere else on the screen.
It must come with the computer somehow.
Where does it come from?
It's inside of-- it's declared, so to speak, inside of
a file called stdio.h.
>> Now, for this stuff, we'll come back to eventually, because it's a bit
distracting for now.
This refers to our return type.
This refers to an argument.
But for today, we're focused just here on these lines.
>> So string name, just to recap, what did this do for me?
So it declared a variable of type string.
So it's meant to be for words, not for numbers.
Just as an aside, if I did want it to be a number, I would say
something like int.
If I wanted to be a floating point value, something with a decimal point,
I would say float.
But for today, I've just said string.
>> And I've given this variable a name of name, but I could
have called it anything.
In fact, originally, I called it S.
>> This here is just another statement.
How many arguments is this statement taking?
So it's just taking one, and that is simply an aesthetic detail of
displaying something on the screen to me.
What is this line 8 doing in layman's terms?
>> It's getting a value from the user-- namely, a string--
and what's it doing with it?
It's essentially handing it over to the guy on the left hand side of the
equals sign so that in this case, the name variable can actually store it.
>> And then printf is another instance of a function.
And we say to call a function.
To use a function is to call a function.
This thing apparently takes two arguments.
This one, comma, this one.
>> This one itself is just a string that contains a placeholder.
%s means put another string here.
And so by putting name after the comma, that's inserting
that thing for me.
>> So I can do other things still.
Let me go ahead now and do a bit of a silly program, but let me delete this,
and let me create a loop.
While true.
The symbol true is obviously always going to be true.
So what kind of loop am I inducing by writing a line like this?
>> So an infinite loop, right?
Just while true.
There's no way I can change the word true to be anything else, so this is
just going to run forever.
So if you've ever actually had a program in your Mac or PC that seems
to have locked up, or it's doing something and something, and you just
can't get the thing to quit unless you reboot your computer or pull up the
Task Manager or the like, here's such an example.
>> I can say, "I am a buggy program." Close quote, close
parenthesis, semicolon.
So again, notice the patterns.
Even though some of our syntax is new, the keyword while, the keyword true,
notice I have the same kind of curly braces.
I've got a semicolon and parentheses.
>> So now let's go ahead and compile this.
Make custom, custom.
So at this point, you do not need to restart your appliance, all right?
So the easy lesson here is with two fingers, Control C,
we'll quit that program.
But you can see exactly why this thing was repeating itself again and again.
>> I can do something a little more complex as a teaser.
I'm going to go ahead and say the following.
For int, which is an integer, i is just a generic name we give to most
variables in programming when you're just counting, equals 0.
Let me go ahead and say while i is, let's say, less than 100, i plus plus.
We'll back to this syntax, but this just means
increment i again and again.
>> And what do I want to do here?
Printf, "I can count to i." This is a buggy program.
What did I actually intend here?
>> Percent not s, but you wouldn't guess this. %d is a decimal integer.
Comma i.
Let me roll back.
>> Let me now recompile with make.
Let me now rerun with custom.
I can count really damn fast when I write a program like this.
>> Now let's do something a little unintentional.
How about let's do this while i is greater than or equal to 0.
What's this going to induce?
So logically, this is not so wise, because now if I rerun this, recount,
I can now count really, really high.
Unfortunately, about as high as I can count is 4 billion, so this is going
to take a while.
>> So why don't we leave this as our cliffhanger, promising that on
Wednesday, we'll see if this program is done.
Two, we'll introduce how you yourself write your own function so that very
quickly, by Wednesday, we'll, as we transition from Scratch into C, start
writing ever increasingly complex programs that do much,
much more than this.
We will see you then.
>> [APPLAUSE]
>> SPEAKER 1: At the next CS50, Puzzle Day was a success.