>> All right, welcome back to CS-50.
This is the start of week 1, so it's this week that we dive
into actual programming with a modern programming language
and begin to tease apart some of the basic building blocks
that frankly will carry us through the whole semester.
More on that in a moment as to what that means,
but quickly one, you do have a printout today
of all the source code
that we'll use today and also on Friday.
You'll find that they lines are numbered,
so if you've ever heard
of a language called basic that's actually a language
from yesteryear where you actually as the human how
to type line 10, line 20, line 30,
and then you wrote your code to the right of this.
So that was kind of a humanities clever approach to leaving room
in case in the future you wanted to add some more lines of code.
You could put it between lines 11 and 19,
didn't really scale particularly well.
These are just printouts, line numbers,
so you won't be writing those numbers there
but I'll often refer to them.
Office hours have been in progress.
If you would like to come hang out in either the Quad
or Quincy Hall dining hall this evening
or next evening, do drop by.
The teaching fellows will be there with the CAs
to just chit chat about the course,
help you with your scratch projects or just generally hang
out and spend the evening in more of a communal environment.
And then sections, we will have the sectioning process
officially begin tonight around midnight,
so check CS50.net's home page
and we will have an announcement there as to how you section.
But so that we have some breathing room
and we don't start off the semester on too rushed a foot,
what we're going to do is not start in intimate sections
where 12 to 15 students and one CF this coming week,
we're going to start those in two weeks time
and this first week, so this coming Monday Tuesday Wednesday
we're going to have super sections led by pairs of TFs.
There's just going to be larger sections for the first week
that you're welcome to attend any of those.
The locations will be announced, so go to CS50.net/sections
to see the current Google calendar for that.
And then finally we do have a bunch of sensor boards
if you would like to take on the hacker edition of PSET-0,
but do realize that will always be the case we expect,
we encourage almost everyone in the course
to pursue the standard edition, so by no means should you feel
like you are falling behind or cutting corners
by just doing the standard edition that's meant
for most folks.
So with that said, we spent and a number
of you have spent already some time playing with scratch
and we actually have kept an eye,
partly just out of curiosity, on what kinds
of submissions have been coming in already.
Now, not to worry if you think it's a little premature for --
little bug sometimes, the mouse stops responding so see
if the screen starts responding again.
There we go.
All right.
So I went ahead and dug up this so if only for curiosity's sake,
only about 10 percent of the class has submitted already.
So here too don't worry
that you're already behind given how quickly some folks have
started submitting.
About 30 people submitted.
We found this is slightly statistically interesting.
So as might be expected, most of you,
many of you are running Windows.
What was kind of interesting was that neon of you have gone out
and got and installed this brand new shiny operating system
by Apple, Snow Leopard, and we'll see if some
of those numbers change.
Or maybe someone back there, so we'll see
when those numbers come in.
Some of these questions you'll find
in PSET-0 survey are really just for statistical purposes.
We'll try to do some fun things
with the data throughout the semester.
We don't fundamentally need to know what city you were born in,
but it's kind of interesting if we can later visualize
that with something like a Google map.
So don't be too thrown off by those.
If you're worried that everyone in this room has an i-Phone,
very much not the case.
Most of you have, or most of you
that have submitted already have a normal mobile phone,
which is perfect because it actually means the time Willy
Chow CA and myself has spent on shuttle boy voice
of late will actually get used.
It's kind of a retro approach
to making an application using touchtone prompt,
but indeed if you call 617-BUG-CS50
that service is now live.
You can get out of here quickly to the Quad or to matter
after class where it operates 24/7 as well.
And coming soon are going to be a whole bunch of other features.
So right now it's just option 1 for shuttle boy,
but coming soon will be an option for wakeup calls,
which we suspect will be kind of useful
around mid-term time, exam time.
We're mildly nervous because I think this could backfire
completely if we only discover bugs the morning you have
midterms but we'll try to avoid that.
Coming soon too will be dining hall menus.
If you want to find out what is for lunch today,
what is for dinner today, thanks to a former teaching fellow,
Kato Yuchiama [assumed spelling],
we'll have an option online there as well,
and then soon you'll be able to find
out when the next CS-50 office hours actually are.
Well, people tend to like where they live.
This is a little slide that gives you a sense
of where PSETs have been submitted from.
Freshmen are kind of slacking it seems thus far,
although Holeworthy and Matthews seem
to have some decent contingencies.
And then here among the houses what's striking is
that the only house that is apparently doing other things
this week has been Leverett, which ironically is one
of the largest houses and last year the data was completely
the opposite.
We had more students from Leverett house than any
so perhaps we're now paying that price.
So just random fun facts here from CS-50.
So let's now dive in.
So this week and beyond is largely
about this language called C and when you start playing
with C you're playing with a real,
a traditional programming language.
so PSET 1, which will be realized this Friday
on the course's website, will have you tackle something
that may be unfamiliar to many of you,
this operating system called Linux [assumed spelling],
and also this language called C and we'll spend much
of the semester on C. What's fun I think particularly
about this language is that unlike Java
and unlike scripting languages that some of you might know,
PHP, Python, buzzwords like this, it's really low level.
It's just shy of something called assembly language,
which we'll glance at later in the semester,
and by that I mean it really gives you find-grained control
over where stuff is going in memory,
like where in RAM are you actually using strings
and numbers and databases and stuff like that.
And it allows you at the same time to do really bad things.
So many of these exploits that you read
about if you read these kinds of publications or websites
that involve websites being hacked over servers being taken
over by hackers and so forth, to this day a lot
of those compromises are either the result
of just stupid mistakes by humans
or sometimes more subtle mistakes made
because the website or the program, whatever it is,
was written in C or C++ or some related languages as well
because with this power comes as they say great responsibility.
You can do a lot of damage to your own data,
to your own security by coding improperly and not being mindful
of certain security issues.
And what's actually fun, I think, as a result,
about talking about this language
for the next several weeks is you really understand just some
societal issues like that, like why did this website get hacked?
How could someone possibly do that
and then what are more intelligent solutions to some
of the problems we ourselves will see.
So scratch meets C. so hopefully,
even if you haven't sat down to do PSET 0 yet, which is fine,
still plenty of time, you hopefully got a sense last week
of what some of the basic building blocks are
of programming.
There's like a statement, do this,
whether it's say something, think something, move somewhere.
There's conditions.
Do this if the following is true.
There's variables if you need to keep around a piece of data,
and then we saw some slightly more sophisticated topics
like threads, multiples scripts and events, this ability
to send messages from one script
to another while all these same building blocks exist in C
as they do in most languages.
And even though what you're going to start writing
for PSET 1 onward is going to look more like the below
and less like the top, it's really the same ideas.
And if you take one important point away from today,
realize that you will, when you first sit down to program,
maybe for the very first time and most likely with C
for the first time, you will make some stupid mistake
and you'll stress over some meaningless detail
like why is my code not working, why is not compiling,
and odds are it's going to boil down to something stupid
like see that semicolon on line 4 there,
like you'll accidentally have omitted that
or you will have left off a single quote somewhere
or double quote.
And frankly this is one
of the reasons we start off the course just last week
with scratch, right?
You don't have to worry about this uninteresting minutiae
but realize now that we're going to be programming real computers
and we have to be ever more precise and it's this level
of precision, this level of nitpickiness
that actually does matter.
Because you'll find
that a computer will do literally what you tell it
to do, and if you don't say semicolon there
in this particular language, you're not telling the computer
that this is the end of the statement,
and you're making things ambiguous.
So ambiguity tends to be bad in programs as we will see.
So what are these things going to start looking like today?
Well, a quick teaser, a statement that last week looked
like this, oh, hi world, with the little purple block
and C onward is going to look a little more cryptic
but at the tense of the day the same exact idea, Print F,
which means a formatted printing mechanism, Ohi kind of a world
and then backslash N, I think we said last week,
what does that mean?
New line. So you have to be even ever so explicit
about where you want the enter key to be hit
and /N represents the act of hitting enter on the keyboard.
So that's what the statement's going to start looking
like shortly, a bouillon expression,
so this was something that could evaluate to true or false 1
or 0 on or off, however you want to view the world.
Well now what used to look like orange and green
up top there is instead going
to look a little more high school math,
but parenthesis X less than Y, closed parenthesis.
And then if you want to join these things together we're
going to see things like ampersand ampersand,
just happens to be the nomenclature in C
for the bouillon idea of and.
And the notion of or, if you want to say this or this,
it's not going to be or it's going
to be a vertical bar a vertical bar.
And it's stupid things like this too,
where we'll inevitably get an FAQ this semester
like where is the vertical bar on my keyboard?
This is not a character many of you usually use,
it's usually shift and above the enter key
on the typical keyboard that would vary.
So again don't get hung up early in the semester
on stupid details like this because they are stupid
and they're intellectually uninteresting
so just accept the fact that this is what
at least initially some languages are like.
Well, what about more interesting constructs,
conditions.
If X is less than Y last week we wanted to say something.
Alt we wanted to say something else.
Well, what looks like this nested puzzle piece construction
at left this week onward is going to look
like the stuff at right.
And again it's getting a little messier
but it's still basic building blocks,
if space open parenthesis, X less than Y close parentheses,
curly braces, essentially represent what scratch does
graphically by embracing puzzle pieces similarly
to embrace chunks or lines of code with these curly braces.
Print up is again formatted print.
We've got some familiar quotes now, backslash and semicolon.
So already this is old hat.
Else if now we have a second branch here, else,
a third branch, and notice again the symmetry throughout
this code.
If we open a curly brace we close it and things seem
to be done symmetrically.
So that will become important as well.
So loops, we saw things like forever
and repeat some number of times.
Well, these two look a little weird at first glance
but at the top right there we have while 1 and this is kind
of a stupid loop in a sense because while 1,
well what does that mean?
Well, 1 is synonymous so far as we've seen
with the notion of true.
So while true what does this suggest is going to happen
with that loop at top right?
It's going to go forever, right because true is true,
it's not going to change.
One is not a variable, it's not X,
it's not Y. It's not foo it's not far.
It's not some placeholder, it is literally a value of truth
so this is an intentional infinite loop.
Now probably not the best use
of an infinite loop here unless you literally want
to write a program that when double-clicked or run
and a prompt just says ohi ad nauseum forever,
literally until the cord is pulled from the computer.
So sometimes we'll see infinitive loops are good
but infinite loops if you can somehow get out of them
and the only way to get out of this is to do the equivalent
in Linux will see of force quitting
that particular program.
And to be clear, these are not real programs,
just as the left-hand blocks don't have
that when green flag clicked block
which started a script last week,
these are not complete programs,
these are just snippets thus far.
We'll see what we need to wrap them
in to make them real programs.
Now bottom right's a little more cryptic still
but it's a pattern you're going to get very familiar
with because it's terribly useful.
Four int I gets 0.
So this says declare variable called I, initialize it to 0.
Do the following so long as I is less than 10,
so that middle piece there
between the two semicolons says it's a condition,
so it's similar in spirit to what's in parentheses up above,
uninteresting though that parenthetical was,
so while I is less than 10, do the following
and then you recall from last week what plus plus tends
to denote?
Increment, so the effect in English of this bottom loop is
to do what in layman's terms?
Print ohi 10 times.
Right. It's not an interesting program,
but again what's otherwise cryptic looking syntax just gets
a very simple job done.
And so that will become now a new building block.
Well what about variables?
Well, this snippet of scratch code
at top declares a variable called counter.
It initializes it to zero
and then apparently this scratch code forever said the value
of counter and this little cartoon bubble on the screen
and then it changed counter by one
and we said last week that's the equivalent of plus plus.
So that loop up there, that scratch construct has the effect
of counting from 0 on up.
Well, how do we implement that same idea in C?
Well, it's going to look a little something like this.
To declare a variable in C we shall see will be a relatively
simple saying what kind of variable do you want?
I want an integer, aka int.
what do you want to give its name as?
Counter. What do you want to assign it a default value of?
Gets 0 so = 0 semicolon.
Declares a variable called counter, it says I'm going
to put an int inside of this thing, what Int?
I'm going to put 0 inside of it initially.
Now this loop we saw before while 1 means do this ad
infinitum, print F. Now this is a little interesting
and we'll look at more of this today and on Friday, percent D,
this is what the F is in Print F.
When we say Print F it means print a formatted string,
well what does it mean to format a string?
It means to tell the computer display this value somewhat
differently based on what type of value it is.
Now this is kind of an uninteresting case
because what do we want to print?
Just that value of counter, because I want
to essentially implement the scratch thing up above,
so percent D henceforth, decimal stands for integer.
Put in integer here.
What integer?
Well, put whatever integer is inside of the variable
that comes after the comma.
So again, basic building blocks.
You're certainly welcome to take little notes on things like this
but it will recur so frequently
that this will very quickly become old hat.
And finally counter plus plus, and that simply means increment
that counter and do it again.
So we have scratch, we have C,
functionally they're exactly the same.
I think you'll find in week zero and for PSET 0,
just a lot more fun to get something up
and running using the approach up top, but you're going to run,
some of you, into sort of ceilings with scratch,
things you just can't really do
because it's not expressive enough of language.
We'll be able to do much more powerful things with C,
much more powerful things with PHP and with java script,
so we are on, as you shall see, an upward trajectory.
And finally we have this thing called an inventory.
What program was it last week for which I used an inventory?
Yeah, it was that RPG, the role-playing game
where I moved the little guy up, down, left,
right and collected fruit and then presented it
to the guy at the counter.
Well, I was putting fruit into an array, aka a vector,
aka a list, although technically we'll see this semester those
actually have different useful meanings.
In scratch we just have a variable called inventory.
We can add a string, like orange, and when I say string,
incidentally, this is computer science speak for a sequence
of characters that represent a word, a sentence, whatever.
So that's going to be represented in C,
perhaps most cryptically still is char star inventory,
open bracket size closed bracket semicolon.
And we won't spend much time on arrays this week,
but in C because you're so close to the hardware, so to speak,
because you have so much control over RAM
and where the computer puts stuff,
you have to be more explicit in C than in scratch.
You have to say I want an array of what size?
Well, you tell the computer by way
of this bracket notation how big you want the array to be,
so size is actually a variable for some constant value
that must be defined somewhere else in my program.
It's simply not on screen.
But then when you want to put something at a specific location
in an array, the IFE [sounds like] location, we'll say,
I'll stop using finger quotes today
because all these terms are new, I'm sure.
So the IFE location in inventory is expressed
with inventory bracket I closed bracket gets or equals
and then whatever value I want to put in there.
and now notice I'm trying even today, this day zero of week 1
to be careful with terms like equals because we'll find
that this equals sign that you're familiar
with from algebra in most programming languages is not
equal, it's not equality, it's assignment.
It means take what's on the left and put it inside
of the thing on the left.
Wait, take what's on the right and put it inside
of the thing on the left.
And that's why we've seen this pattern array.
So this begs an interesting question
and invites an interesting bug.
How do you compare two values?
Is X equal to Y?
Well, you can't say X equals Y because that would seem
to put Y's value in X,
so hopefully there will be a solution to that.
So with that said, here comes C. when you write a C program,
as I did on the fly on my Mac last week, recall that I opened
that like terminal window, it was this little blinking cursor.
I quickly whipped up the code, the source code,
at top left there, and then I ran a command.
Well, trivia what was that command?
GCC. So this is a term you will not forget after term's end
because you're going to be running it every day
that you're tackling problems.
That's GCC, is a compiler.
A compiler is a program
that takes something called source code
and turns it into object code.
Now what does that mean?
Well, nicely enough this course is not
about writing stuff like this.
There was a time and there were --
there are people still roaming this campus who had
to essentially write programs at the level of zeros and ones,
or if you've heard of this thing called a punch card,
very similar in spirit, and there was a time
when the science center was a center of science.
You would go into the basement, you would hand
in your punch cards and then the person running the mainframe
computer in the basement
of the science center would essentially run your homework
for you overnight, because it would usually take a while,
by literally putting a stack of cards somewhere into a machine.
So not the most efficient process, and I've talked
to current faculty who grew up this way on a campus,
not the best time of day to find out that your program is buggy,
it has some mistake in it, right?
Because that's probably happening
at 9 a.m. the next morning when you go back to the center
of science and pick up the results of your program
and it's just one big error message.
So fortunately we've come a long way, and all of your fighting
with bugs and implementing cool things will happen much more
interactively within seconds in front of your own computer
or the lab computer, but the process is essentially still
the same.
At the end of the day a computer, whether it's a PC
or a Mac or whatever you guys own at home,
it only understands 0s and 1s.
Now the world has done some really clever things with 0s
and 1s but you still have to turn your programs into 0s
and 1s and a compiler does exactly that.
So you'll be writing stuff that looks like this at top left.
You'll run that file, which is really just a text file,
through a program called a compiler,
and that compiler will take care of the task of turning it
into 0s and 1s like this.
So where do you do this?
Well, last week I went ahead and ran this program,
so just a quick show of hands then we'll have the data
for sure, how many of you have Macs in the room?
All right, so that's actually statistically kind of a lot,
so it looks like more than half of you
in the room this semester, and that's pretty consistent
with terms past, what's neat is that all the stuff we're going
to start doing this week and beyond,
you actually could have been doing on your own computers,
your own Macs, this whole time.
Now terminal is not a program that's usually
in the little dock here as it is in mine
because most users don't need to know or care
about this little terminal.
But you may at least know anecdotally
that Mac OS these days is based
on an operating system called UNIX.
And back in the day UNIX was essentially a command line
interface, CLI is the buzzword there,
and this means you don't double click on icons to run programs,
you don't go to pretty menus that have sub menus
and graphical icons and all of this fanciness that developed
over the years, you if you want to run a program,
you type the name of the program and hit enter.
And if you want to influence the behavior of that program,
you provide it with commands line argument, words or numbers
after the program's name, and then hit enter.
If you've ever seen DOS on a computer back home,
it's very similar in spirit.
Usually a black screen, white text, and you can't do much
of interest with it unless you know what's there underneath.
So you're not going to write for the most part programs
in this course on your own Macs or on your own PCs per se,
because it's quite hard for us to standardize the configuration
of 300 plus people's laptops.
So instead what we do is provide everyone
with a uniform environment throughout the semester.
Initially this is a cluster
of servers called nice.fas.harvard.edu.
And this nice cluster does so happen to live
in this very idyllic building here.
So it's in the basement, unfortunately,
the cluster of servers.
It's also a bit of a misnomer
because NICE new instructional computing environments has been
NICE for about 10 years now, and there isn't even an old version
so they've kind of painted themselves into a corner here.
We're using the same NICE cluster as always.
So nice.fas.harvard.edu, it's not a website,
it's not something you type into a browser, but it's going
to be the name of a server that you access by way
of your own Mac or by way of your own PC.
Well, what do I mean by that?
well, a lot of you these days have college.harvard.edu
addresses, but you also have the so-called FAS accounts,
and these are the user names and passwords you use to login
to the basement of the science center, kiosks and all of that,
frankly it will be a few more years, I think,
before the university converges on just one account.
But for now you have these accounts, though they're
of decreasing utility.
But for this course at least at the start
of the semester we do acquaint you with nice.fas.harvard.edu
because what it means is
that after the semester you'll actually still have a Linux
server environment on which you can run and write code,
whether it's for an economic class in the further
or a chemistry project or anything
where you need the ability to write programs and you're not
in CS-50 anymore, you'll at least have this resource
for some number for a good amount of time.
What we will start using mid-semester, though,
and this is the extent of my Photoshop ability,
we will use the so-called Cloud.
So this is another one of these buzzwords these days,
Cloud computing refers to the ability
to rent server space somewhere out there on the Internet,
you don't know or care who or where the servers are
or who is running these servers.
So we're essentially going to implement this same idea.
CS-50 has its own cluster of servers now
in the computer science building called Maxwell Dworkin
[sounds like].
We've got a couple of really big Dell servers
that have a whole lot of CPUs, 32 gigabytes of RAM inside
of them, 6 terabytes of storage space.
So we can hold our own against HCS here.
We have a couple of those.
We have an Apple X Serve and long story short all
of this stuff is wired together in such a way
that soon this semester you will have your own accounts
on cloud.cs50.net, and what this will mean is you'll start using
at some point this cluster instead of nice.fas
but it's really the same thing.
Now what do I mean by cluster?
In the basement of the science center, and also in the basement
of Maxwell Dworkin, there's a bunch of computers.
They're all networked together using Ethernet cables
into a thing called a switch.
They are exposed publicly on the Internet by way of IP address
or these names, nice.fas.harvard.edu,
and there is this illusion that when you connect to one
of these servers you're connecting to just one,
but in fact there's like six computers
in this nice.fas cluster, but they've set them up in
such a way that you the user don't know or care
which particular one you're
on because your files are all visible on any one
of those machines and all
of the commands are identical across the same.
So when it comes time to tackle PSET 1, what you're going to do
if you're a Mac person is something like this,
I just ran the terminal program,
I'm going to type a command called SSH.
This stands for Secure Shell,
and this just means make an encrypted connection
between my laptop, wherever I am in the world, and this server.
And so what I'm going to do is type SSH.nice let's do Malan
at nice.fas.harvard.edu.
I'm going to get a prompt then, looks like I am indeed connected
to Harvard University server.
It's asking me for a password.
Fortunately it won't show you on the screen what the password is
but now I'm at the so-called blinking prompt
that these days no longer blinks
but a blinking prompt it once was.
So I apparently have three unread messages,
which has been some time since I never check this email
account anymore.
So Malan at nice and then there's this thing
in parentheses.
Well this is the command line, so to speak,
where you can type anything you want.
Well, that command was found.
And this is where initially we're going to be writing things
like C source code and compiling it.
So what does that mean exactly?
Well I'm going to do the following just
to give you a sense of some basics and all
of this realize will be repeated in problem set 1 own PDF.
So your hand will be held certainly early in the semester
so you needn't try to memorize or scribble
down all these commands but just to give you a sense
of what we're doing here, if I type the command LS,
this is list, this means list the files in this directory,
this folder, well there's nothing there.
So when I'm at this prompt it's equivalent conceptually
to being inside of a folder on your own Mac or your own PC,
it's just graphically very underwhelming.
And it's more mechanical at the keyboard
than it is mouse oriented.
If I want to make a directory called CS-50 for the duration
of the semester, MKDIR is make directory.
Back in the day people didn't like to write long commands,
pretty reasonable, so it's just MKDIR CS-50 hit enter,
nothing seems to happen but if I type LS now for list notice aha,
there's a directory as implied
by the slash there, that's called CS-50.
Well, I can't, unfortunately, it's a little blue
on this screen here but that indeed says CS-50 if I type LS.
Notice that I can't, and this is kind of a neophyte mistake,
can't really double click and nothing really happens there
so you have to change directory CD into CS-50 and then hit enter
and now my parentheses, this is just a special feature
of the way we've configured your accounts, you'll see.
This just reminds you where you are, which is nice
when you start to have lots of folders open.
Now if I type in LS here there's nothing here, so if I want
to write my first program I need to open a text editor
and the text editor that we'll encourage most students
to use is this little program called NANO.
If you've ever used notepad on PCs or text edit on Macs,
this is like the most user friendly text editor you can
find but as such it's also lacking a number
of useful features as some of you
with more experience will see.
So let me go ahead and write hello.c. Notice
that I have just a simple window.
It's a little cryptic at the bottom,
but again this is just stupid stuff
that once you know it you kind of never forget.
The carrot symbol means hold the control key,
so we'll get to that in a second.
But let me go each and recreate really quickly what I did
last week.
So I type this cryptic sequence of characters,
include STDIO.h in angle brackets.
I then set into main ent arg C char star,
delete key is not working, we've got to fix that.
Well, that's interesting.
Wow. All right, I'm going to do this again
without making any mistakes.
Standardio.h ent main ent arg C char star, here it comes,
arg v bracket parentheses open curly brace 1234 Print F
and I'm just going to say hello, world.
I'm going to make it look pretty at the end with the backslash n,
closed parentheses, semicolon, close.
Okay. we're not going to indent properly here we go,
closed brace.
So that's my first C program,
and this is the thing we completely underwhelmed you
with last week.
But what does this mean I'm going to do now?
I'm going to hit control X
because down here it said carrot X for exit
so now it's saying save modified buffer.
This is the un-user friendly way
of saying do you want to save your changes?
I hit Y for yes.
It then checks what name do you want to give this file?
Again it's just reminding me what it's already called
in case I want to change it.
I hit enter and voila, I'm back at the prompt.
But if I now type LS what should I see?
Hopefully hello.c and this time it's all white
because it's not a directory so now I need to run this thing,
so I could try doing hello.c or again the newbie mistake
of really that's not going to do anything,
hit enter, permission denied.
But that makes sense because what's inside this file?
This is not 0s and 1s clearly, this is just source code.
So source code is the human readable form of this stuff
but I remember from last week GCC hello.c. Let me go ahead
and hit enter now, nothing seems to happen, but wait, LS.
Interesting.
Again foolishly named default name for a program anything
like hello.exe would seem a little more reasonable,
but a.out is sort of the standard but we'll be able
to override that soon.
So a.out has an asterisks which just means I am executable.
I have 0s and 1s inside of me.
In fact if I get a little adventurous and now open a.out
with nano, nano being a text editor,
a.out being a binary file, binary meaning just 0s
and 1s inside, so that's the program I wrote.
Now here's too where you can do very bad things very easily.
If you so much as hit the space bar accidentally
as you're flailing about in the basement
of the science center trying
to fix whatever problem you've just created,
you just inserted aski characters,
random text into what's otherwise a binary file.
Why does this look so cryptic?
Well, nano is again a text editor.
It doesn't know -- it's not supposed to show you 0s and 1s.
It's supposed to show you the letters and the numbers
and the punctuation that those 0s and 1s represent.
So when you see all this crazy stuff on the screen,
that's just nano's misinterpretation
of what's really 0s and 1s, not necessarily aski letters.
Fortunately control X will save me here,
save modified buffer, no this time.
So I hit N, I go back to the prompt
and now I can run a.out just by typing this command here
and in fact that's my first repeated C program.
So that's it.
that's the basic building block of writing a C program.
Hopefully we can do much more interesting things than that
but that covers the basic workflow.
Now just to give you a sense that what we're doing
in this course is ultimately pretty much operating system
agnostic, here we have Mac, here we have PC, running Mac OS,
running Windows, on the Windows world it's not quite as easy
as going to a terminal window because Windows does not ship
with that little program called SSH
with which I created the secure connection, but there's plenty
of free tools that do that.
One thing you'll see
on the course's website is program called putty.
So if you're a PC user you run a program
like this it's a little more user friendly if overwhelming
with all of its options.
What I'm simply going to do here is say, you know what,
I want to connect to nice.Fas.harvard.edu.
so if I were doing this for the first time I would simply do
something like that, typing in its host name or its IP address.
Now I'm going to go ahead and click --
actually I'm going to load something there
so I get the bigger font size.
I'm going to click open and now I get this black window again.
I'm going to log in as Malan.
I get a similar prompt, enter, notice I'm back in here.
if I type LS there's CS-50, CD CS-50, aha,
there's my a.out program.
Same exact thing.
So this is actually powerful.
Even though I'm on a PC here, a Mac over there,
I've accessed the same account so that's one of the upsides
of having this central server.
You'll be able to access your code anywhere.
If you go home on break, if you get sick, if you're just hanging
out a friend's you can access your code in this course
from any computer on the Internet and you'll find
that for better or for worse to be at least useful.
So just to give you a sense of what else you can do
on a typical Linux server, so the machine I connected to,
nice.fas, it's running at Linux,
which is again an operating system that's larger text-based
but there are goes, graphical user interfaces,
and in fact if you headed to the basement
of the science center someday as you might for office hours,
they've got like 33 percent Linux computers
in the basement there.
they do have Windows, they do have menus,
things you can click, because the world has come
up with more user-friendly interfaces to Linux.
But you'll get your hands directly this semester with some
of the building blocks because frankly when you get comfortable
with this stuff you can do so much more, so much more easily
and so much more efficiently just
by telling the computer precisely what you want.
So CD we saw, CP, any guess?
CP, copy, kind of the shorthand version of copy,
and in fact it is, CP we'll use to copy code this semester.
LS is list, MK-Dir is make directory, MV, move,
so that's synonymous, we'll see renaming a file,
so to rename a file you'll say MV space the original name space
the new name, not terribly hard.
PWD. Yeah, so print working directory.
What this means here is I'm back on the nice.fas server.
If I kind of get a little confused sometime
and I type PWD it will tell me the full path to where I am
on the server, so right now I'm logged in as Malan
and Malan does not have access to everything on the server
because there's like 20,000 other people with fas accounts.
So in fact, fas took the approach years ago,
very intelligently,
of organizing things hierarchically.
So notice there's a directory,
the root of the hard drive apparently has a directory
in it called NFS, network file system, home,
which is where everyone's home directories live.
And a home directory is just your personal storage space.
M/A just looks like there's a whole hierarchy of names there
to keep things clean, slash CS-50, this is the full path
to where I am, and realize that I can definitely go poking
around if I get curious.
This is not a bad thing.
If I type CD DOT DOT it doesn't go this way it goes backwards
so to speak or higher up in this tree, so now notice I'm back
in my so-called home directory.
Where am I going to be if I do it once more?
Yeah, the directory called A. So with all
of the people whose usernames start with MA,
which if I do CD it's getting a little curious here.
Maybe will this backfire?
Maybe if we could put the camera down here for just a moment
so that we're not podcasting this to the Internet,
these are all of the people on fas.harvard.edu whose names,
sorry folks at home can't see this, that begin with MA.
If we go back one more directory keep the camera underwhelming
down here, there's the whole alphabet.
We can see people that start
with probably no MZs I'm guessing, CDZ, LS, oh there are.
So it's not a full name, it's first letter
of the first name, last name.
So again you can poke around all you want and see a whole bunch
of stuff on this computer, but Linux,
like most operating systems, has various permissions
and fancy control features so you really can't do bad things.
you can't access ,for instance, if I get nosy,
let's say home/a/y idida
for Jansue's [sounds like] home directory.
Let me, I'm forgetting the day, so I'm in it.
Thankfully, permission denied.
So things are, in fact, configured correctly.
So again just random little trivia
that ultimately gets useful
because just understanding the world
in which you're playing I think lets you get yourself
out of problems and actually solve more interesting ones.
So there are some other commands that we'll see,
and again every piece that's pdf will walk you
through any of this minutiae.
You're not going to have to figure it out or figure
out from week 1 what the heck was the command for this.
So we're going to see things like GCC, GDB,
commands like more and then someone got clever.
More is just a program
that shows you the contents of a file.
Well, someone wanted to be clever and kind of one
up that person so they'd called their program less
but it does really the same thing.
Man for manual, which is something you'll get familiar
with as well.
So what does it take to write a program, so it's three steps
and we've done it twice.
Last week and this, one, open up a text editor like nano
in a Linux environment and call the file whatever,
hi1.c or hello1.c, using the dot C convention for namesake.
GCC the name of the file and then you run the thing
by simply running the default name.
Well, what if we want to do something more interesting,
give it a non-stupid name like a.out?
So there are again these things called command line arguments
or switches with which you can control the behavior
of code more interestingly.
So if I go back over here into my CS-50 account,
my CS-50 directory, here's hello.c. my goal now is
to create a more interesting name than a.out,
so per that previous slide I say dash 0 for output.
Let's call the program hello
and then let's say compile this particular program, LS.
Now I have a binary, 0s and 1s called hello up.
So you'll notice little keystrokes that save you time.
If you hit tab it will do auto complete which saves you time.
Unfortunately it also reveals all the other commands
on the server which start with he, in this case head, hello,
help desk, help desk server, help z tags and [inaudible],
none of which I care about so hello, enter,
same exact program even though it's called something
different still.
So more interesting but again just a fundamentally
uninteresting building block.
But your programs are going to get more complicated over time.
Most of your programs by semester's end are not going
to be this big in a simple text file,
they're going to be multiple files.
And they're not going to be terribly long each,
but we'll see one of the principals we'll preach
in the course is design, and good design,
and as some of you may have experienced in scratch already,
if you're implementing a script that's kind of this big
and then you add some more features and then it's this big
and then this big and this big and God forbid this wide
and wide and wide because you keep adding ifs and its and ifs
and its you're probably crossing the boundary at some point
of what we'd call good design.
If you're scrolling up, down, left right just
to see your own program there's probably a better approach.
Now for PSET 0, they don't need to worry so much about this
because we haven't really told you what good design is,
but among the features will be chopping up your code
into much more manageable bite-sized pieces,
putting it in separate files, putting it in scratch
in separate scripts, and you'll see that a command
that will facilitate this is called make.
So you'll see both of these approaches in PSET 1,
simply typing Make and then the name of the program you want
to make, in this case, hello.
Notice I'm not typing DOT C, I'm saying make hello.
Make is a program that's going to infer, well,
if this guy wants to make a program called make it's
probably based on a file called make.c
so if I hit enter it tried to recompile it but it realized --
and this is what's cool about make -- it realized, oh,
you haven't changed the program,
why should I waste my time regenerated identical 0s and 1s.
So in fact it didn't bother.
But if I do LS to see my files how do I remove hello and a.out?
Yeah, rm, hello, enter, it's going to ask for a sanity check.
Remove regular file hello question mark?
Yes. RM a.out, yes for that too.
So now I just have my source code, make hello enter,
so the command's going to look a little more cryptic because we,
just to get you started at term start,
are configuring these accounts in a way
that just makes some things easier
and we'll take those training wheels off over time,
but it achieves the same results because I now
in fact have a program called hello in that directory as well.
So what are these programs you can use?
So nano is by far the simplest editor you can use
at least at term start.
On my computer it doesn't support the delete feature
but we will fix that so that you don't need
to fail the very first time you make a program like I did,
but there are other tools out there.
Those of you might know programs like VIM and in fact most
of the time I'll use a program called VIM just
because that's what I was taught in CS-50 years ago.
I kind of grew up on it.
EMAX is another popular one, but more on this in the future.
I mention it now only so that you don't get confused
or puzzled when you see this TF used this program,
this TF used another.
It's just like using pages versus Microsoft Word
versus Word Pad, it's all the same kind of stuff
in this world as well.
So in a moment we're going
to actually tease apart what it means
to write a more interesting program than hello world.
The basic building block for almost any C program is going
to start with this construct here, but why don't we go ahead
since this is a lot to digest,
and take a five minute break first.
All right.
We are back, so Yuki [assumed spelling]
and Jantsu [assumed spelling] informed me
that sectioning will indeed debut this evening,
go to CS-50.net, take a look
at any instructions you'll find on the home page.
Check around midnight or certainly by morning,
and note this, so per last week's announcements
in the syllabus we do offer three tracks of sections,
just like First Nights does for our students who are more
and less comfortable with music, so we do the same.
It's just the idea of being in CS-50,
so there's no steadfast rule
as to what makes you less comfortable,
what makes you more comfortable
and what makes you somewhere in between.
To be honest, it's a term you can probably just slap
on yourself based on just how comfortable you actually are
having returned this week to CS-50.
You've seen what the percentage break-downs are so we get plenty
of folks from each of the buckets,
so when you use fas's sectioning tool,
realize that because we have these three different types
of sections you're going to see a long list
of about 30 sections, since we have
about 30 teaching fellows and thus 30 sections.
and in the comments fields
of the sectioning program you'll see
if that section slot has been designated
for those more comfortable, those less
or those somewhere in between.
If you're on the fence as to
where you belong you cannot go wrong being
in the somewhere in between section.
That's really for folks that just don't feel themselves
at either extreme, but certainly feel free to chat with me or any
of the TFs if you're curious
as to what this all means or where you belong.
Now what does this mean in real terms for sections?
Frankly it just means that especially
for those less comfortable, it just means you're going to be
in a room full of people who feel as apprehensive
or as inexperienced as you and it's sort of a safer space
in which to put up your hand
and ask a question you think is the proverbial dumb question.
The sections for those more comfortable are meant
to digress intentionally from say the course's syllabus,
from the week's problem set and really go off
in any interesting technical direction that some
of the savvier students might
like to explore even though it's not going to help them per se
on that particular week's problem set.
But again if you don't feel yourself needing that sort
of comfort zone here or you don't really think
that you'd want to try holding your own with some
of those more comfortable then the sweet spot,
just like the standard editions for most students is
that for those somewhere in between.
All right, so let's do something a little more interesting now.
I've gone ahead and per the handout you have I have a whole
bunch of code already in this account.
So I'm going to go back to my screen here
and you'll see again commands lli this on PSETs.
What I'm going to do for a moment is copy, tilda, CS-50,
so henceforth the Tilda character
which on most U.S. keyboards is
on the top left near the escape key, Tilda CS-50 means go
into CS-50's home directory.
Now I'm in Malan's home directory at the moment,
so go into CS-50 is just like I tried to idida's home directory,
so go into CS-50's and then I know
because I'm reading this off of a pdf or whatever
that there's a pub directory, that there's a source, src,
again, nicknames in Linux, source directory,
and then I know there's a lectures directory in there
and then we're in week 1.
So it turns out there is, in fact,
a directory called 1 inside of lectures, inside of source,
inside of pub, inside of CS-50's home directory and my goal is
to copy it from there to here because I, now a student
in the class, decide that I want to push ahead, I want to do this
after class and play around.
I want to copy David's source code
into my own account just so I can play.
Perfectly fine, notice that DOT represents this directory just
as DOT DOT represents the parent directory.
So if hit enter this is actually flawed,
notice that something bad happened and then
if I type LS I didn't get any files.
So realize, too, sort of FAQ early on is if you want
to copy a directory with CP you need to do it recursively,
which is a keyword that we'll revisit in the term,
it means to do it not only to that directory
but all the files inside and all the subdirectories inside again
and again and again until you bottom out.
So CP-R means copy the file recursively
into my current directory, enter, took a second
because there are, in fact, a bunch of files.
In fact if I go into my one directory now here are some
examples that I've pre-prepared so that we can go off
in a known direction here.
So one direction we already went in was hi-1.c. but your version,
on paper looks a little different from the quick
and dirty thing I've been whipping up.
there's a lot more purple on the screen here, so all the stuff
at top is fundamentally irrelevant to the computer,
but just like scratch does if you've discovered it yet,
lets you put little comments here and there,
sort of sticky notes, so to speak, post-it notes,
so does C in most any high-level language.
and by high-level language henceforth I mean something
that's not 0s and 1s,
not something called assembly language.
It means ignore all
of the following this is useful only to me the human.
So in C the means by which you write a comment is
to do a forward slash star and that means anything after that
on this line or below will be ignored by the compiler, GCC,
and the comment stops when you hit the opposite
of that, star slash.
So the fact that I actually have all of these stars and I've kind
of boxed things in is really just some OCD on my part.
It's not technical I just wanted to make a nice pretty purple box
to box in all of my code.
But one key takeaway is that I made sure that this line
of stars and the slashes does not exceed the width
of my window.
So another lesson that's important early on is
to make sure your -- the code you start writing is
pretty printed.
And this just means it actually displays nicely
on a typical window.
In real terms this means you should never at least
for the C part of this course, have a line of text
that is longer than 80 characters long.
And even that's pushing it, 76 is kind of the geek's rule
of thumb as to how long a line should be.
How do you know what's 76 or what's 80 characters?
Well, right now my window has essentially been expanded to be
as long as I wanted it to be, but you get into the habit
of knowing when to hit enter to move things onto the next line
and nano and programs like it will actually force your lines
to wrap if they start to get long.
And this is just a matter of good style,
because as you could imagine, if I wrote my whole program
on just one line it would still work,
the computer would not care, but you the humans probably would.
In fact, this is hi1.c, let me go ahead
and delete all that stuff.
I'm going to go ahead and do this, you know, this,
I really don't need that there.
Let me go ahead and put that there.
Right, if I'd given you a printout of this, I mean,
rightly so would things start to look pretty damn complicated.
Now some of these spaces are important
so that you can distinguish what's ints
and what's [inaudible], but if I go ahead and save this
with control X, hit Y and now go ahead and run GCC of hi1.c,
the program thankfully still compiles.
I still have eight odd out, so it still works, this version,
but it's certainly not very pretty printed
and not very legible.
So what else is inside of this file?
Well, hmm, I didn't really want to do that.
Let me go ahead and go back into CS-50, Pub source lectures 1,
let me just get a new copy of this.
So now it's going to prompt me, wait a minute, overwrite hi1.c,
little sanity check, say yes.
So now I have the prettier version back.
I'm going to start using a program called VI or VIM just
because frankly I'm more comfortable with it than nano,
even though nano is simpler, do as I say not as I do.
Hi1.c, still kind of pretty prints everything
and color codes it.
And the color is actually a feature that you don't get
if you trying writing your first programs with notebad.exe
or other similar client side programs.
It's just making some inferences about the different roles
that some of these keywords play,
so I didn't write the word int in green.
I didn't write the word char in green, it just knows
that that represents the type of a variable.
Let me draw David's attention to it
with this default color scheme.
So what's interesting here?
Well, this stuff at the top is all comments,
and actually now we get to use a fun little toy.
this stuff up here is all comments.
All right.
So it's fundamentally irrelevant but useful
for documenting your code.
This things here I'm going
to briefly call today a pre-processor directive,
which is just a fancy way of saying tell the compiler
to go get the contents of some other file
that often someone else wrote.
So what we don't have here is any kind
of specification of what Print F does.
Like who wrote Print F?
I wrote this program called main, but who wrote Print F?
Right, I'm kind of cutting corners here.
Well, it turns out that when Linux shipped with the compiler,
someone else, some really smart people spent a lot
of time getting this very basic important function implemented
in their own source code.
So by saying sharp include open angle bracket standard io.h
closed bracket, that's just telling GCC, you know what,
the following code has some functions, some programs
that other people wrote.
Those programs happen to be inside
of a file called standardio.h. please go grab the contents,
paste them in here so that I don't have to do it myself.
So I'm kind of oversimplifying there
because in real terms what we're using here is a library.
So a library of code henceforth is just a bunch of code
that someone else wrote or maybe you wrote but that you factored
out into separate files so that you don't have to copy
and paste it into every future program you write.
To technically my white lie I just told is
that these program Print F these functions,
actually as they should really be called, are not actually
in that file, standardio.h,
inside of that file will eventually see is just a minimal
amount of information, essentially a summary of all
of the functions in that file because as you might imagine,
where is the actual implementation, the actual lines
of code that implement Print F?
What would the file name probably be called?
Standardio.c, so in fact, yeah, there is a file out somewhere
on the system called standardio.c
that someone else implemented but there's a lot of lines
of code in there, a normal human doesn't want to read through it
so we have these header files which are called .h files
and then c files which have actual code
and initially I'm not making any header files myself,
we'll get to that eventually but just realize,
some of these minor tripping points early on are just
about keeping programs relatively clean, so this,
to be short, simply says, go get the contents of this file
so that I can use functions like Print F
and henceforth let me clarify.
So I am writing a program, this program is arbitrarily going
to be called hello, but I can override that as we've seen
with the -o flag from GCC
so programs are made up of functions.
one or more functions and these are kind
of like miniature programs just
like your scratch projects are probably made
up of multiple scripts.
A script is a function so a function is just a piece of code
that does something and as you can have multiple scripts
in scratch you can have multiple functions in c and just
as the default block or script and scratch is
that thing called wingreenflag clicked,
so in c is the default script,
the default function called main.
Now why, if its just called main do I need to write something
like int and then ints and then RGC, char or this asterisks,
this RGV this bracket notation, there's just a lot of crap
at the beginning of a program when you're writing it in c,
those of you who took app computer science know
that there's classfoo public static, voidmain, its even worse
in Java and frankly this is just
because these languages were not designed with week 0
of a programming class in mind.
So again, its stupid stuff, you get past it quickly.
For today we're going to wave our hands at details like that
but to say for now it's a copy paste thing.
To start writing a function you literally write int main
and all the rest of that stuff but then the fun part
or at least the intellectually interesting part happens
in between the curly braces for now.
So that's how I would go about printing this simple program.
Let me actually take a look at a more interesting version,
thought only slightly called high2.c
and you have a printout of this.
So what's the fundamental difference between version one
and this one version two?
Hopefully it jumps out at you.
yeah, so if bit egotistically this jumps out at you
that this program has my name immortalized in it,
but how did I do this?
Well it looks like I've got this additional line
and so David is mentioned but what's going on?
well even just using the jargon I started using earlier, string,
because it comes before the name
of a variable, means what precisely?
Yeah, it's the type of variable.
So the variable is called main, what's going to be inside
of this variable, well its not going to be an int,
because David is not an int, it's a string, it's a word,
it's a sentence so I'm going to specify that the type
of this variable is string,
now the equal sign again is the assignment operator,
it means put the value of the thing on the right
on the inside the thing on the left so the value
on the right is itself a string and in c as in many languages
if you want to have a literal sequence
of characters be treated as one unit,
you put them in all double quotes.
You can use single quotes but only around single characters,
so those of you who know a little
of html probably know it doesn't matter if you use single
or double, it does matter.
In many programming languages including c,
so strings are double quotes.
All right, then what do I do on the second line?
Well, we've not quite seen this syntax
but in laymen's term can someone explain what the percent s
there represents?
String. Right, really not that hard.
Percent d happened to represent int, percent s represents string
and so the thing after the comma means what do you want
to substitute in for that formatting code, the percent s,
put the name of the variable.
All right, so more interesting, so GCC hi2.c enter,
now I'm going to run a.out, ohi David
but unfortunately its really not that interesting
if your program is completely deterministic and always spits
out the same result because really, I mean,
there are so many easier ways to just say something like this
than this set up so let's take a look at the third version
of this which is a little more interesting
and finally we get a compelling use for a keyboard,
so this program differs for the comments up top
in that there's a third line of code here
that uses a new function, so we've been using Print F
to send output, it turns out that you can also,
nicely enough, get input from the user.
So not by way of the mouse, at least in this context but by way
of the keyboard and what's the new function,
just to take an obvious guess here, get string.
As the name implies, the function get strings purpose
in life is to get a string from the user and do what
with the result apparently?
Store it. so functions we now see right away,
have this ability not only to do something aesthetically,
Print F has this so called side effect of displaying something
on the screen but functions can also return a value
so you can think of a function kind of as a black box
that does some work and then can maybe hand you some answers
and its up to you, the programmer,
to decide if you're going to do anything with that answer.
How do you do something with it?
well one of the things you could do is keep the answer around.
So by having string name on the left
that says give me a variable called name, it's a type sting,
what are you going to put inside of it?
well, whatever the result is.
The return value so to speak of the thing on the right
so get string returns now a value.
How does this all behave in reality?
So GCC hi3.c, I'm getting a little tired of a.out,
how do I give this a better name?
yes, so -o for output, and then let's call it hi3 though I could
call it anything I want, oh, oh, did I create a problem there?
its kind of a coincidence, so still the same error, yeah,
so correct, fancy correct answer so there's a problem here
in that get string is a function,
yes but it wasn't written by the original authors
of the c programming language, in fact just getting input
from the user in c is really a pain.
You have to use a function among others called scanf
and we'll use this eventually, there's other tricks too,
but long story short, in week one of a course,
especially this course, to complete a bunch of hurdles,
you have to jump through as simple as get a character
from the keyboard, even in Java, those of you familiar
with the scanner class, even that is a pain
to actually use I'd say in the first week of a class
so notice what did I do in this file?
I did one other thing besides mention get string.
There's one other key difference outside of the comments.
What else is new here?
yeah, so I'm not just using the standard io library,
it turns out that the cs50 staff a few years ago wrote our
own library.
And we wrote what that means, two files.
One called cs50.h and one called cs50.c
and in the latter we wrote a bunch of functions
that we thought would be useful to students
in the semester namely get int and get string and silly name,
but get long long but other functions still
that really simplify the process at least at the start
of the semester of getting keyboard input from users
but unfortunately its not sufficient just to tell GCC,
hey make sure you go get a copy of the header file
for cs50's library so I know that these functions exist,
because I've done that.
So one mistake might have been to omit this line together,
that would in fact be bad.
But that's not the only mistake that I made,
I now need to tell cs50's library
when you compile this you don't just get away
with turning one file, hi3.c into 0's and 1's
because you need access to one other file, called what?
Yes, so cs50.c so there's a couple solutions here,
I could email the core staff, and I could say,
hey could I have a copy of cs50.c or they'll be happy
to give it because there's no intellectual property there,
its just standard c stuff, but that seems kind of wasteful
if every time in the semester if you write a program you need
to copy the R file, put it in your own directory,
kind of bloat your own code,
well in fact there's an easier way.
You can tell GCC to link against our library
and long story short is what this mean,
is when GCC is converting hi3.c from source code
into object code, from human readable to 0's
and 1's it means, oh by the way, go get the library, -l,
called cs50 and merge the two at the very last minute
so that the file you get out hi3 or a.out contains 0's and 1's
from both of those files.
So again, another silly detail that's very easy to forget
but just to kind of drill it into your minds
if you ever see this semester,
a message about undefined references,
probably means you forgot something simple
like that library that you wanted to use, -lcs50.
there's others, -lm gives you a math library.
There's -lcrypt which gives you a cryptography library and stuff
like that, but it did work this time.
when I said go ahead and link in this file, I did in fact get hi3
so if I run hi3 state your name, we'll say Jensue,
well hi Jensue, well what about Yuki?
Okay so now I have, still kind of overwhelming right?
not anything to write home about
but at least it's a dynamic program that's starting
to do a little bit more so just to give you a sense now
of some formalities, one I've used here, Print F,
a couple of times, I've used it pretty simply to use things
like strings and ints but we can do more powerful things with it
and in fact you'll have to get used to sort of a new sort
of a world where people write things somewhat cryptically
sometimes, this is an excerpt from the man page,
the manual page for Print F and the way you,
the programmer know, how
to use a function is often frankly, by rtfm.
Just read the manual.
And actually look up what the instructions are,
Google it later if you'd like.
For those less comfortable and less familiar with the speak.
So what you'll find
on the courses website throughout the semester
under resources we have a bunch
of links we think you'll find useful and one
of them is this wonderful site called cppreference.com.
the only downside of it means cpp means c plus plus
so the author of the site actually made it fairly c plus
plus specific so under resources
on the courses web sit we have our own copy of this website
because he open sourced it to everyone so if I actually go
to cs50.net/resources what you'll see up here
under the link to c is a bunch of things,
most of which we won't need just yet, but if I go to reference,
this is a wonderful useful sight and this is one of the reasons
that you don't really need a book for a course like this
because so much good stuff is available on line.
If I know that I'm doing something with io,
that sounds cryptic, but it just means input output print get
stuff like that, notice if I scroll down here,
clearly we're only scratching the surface today
of what C can do and I was interested
in what Print F can do
and here's just fairly user friendly documentation
of the other codes you can use so we saw already, let's see,
percent d and there it is at the top for signed integers,
things that may or may not have negative sign in front,
percents for a string of characters
but there's some other stuff here too that we may
or may not use in this semester, but what's nice
about this site is it give you sample code,
it tells you what library, what header file you must include
at the top of your program
and then it tells you what the functions signature is,
what is sort of the canonical form of this thing
so you can begin to understand how you use it
and again you'll get much more acclimated to this
over the course of the semester
but we can do more interesting things with this.
So just as there's n for new line,
there's r for carriage return, so if you think back
to retro typewriters when you hit enter
on an old fashioned typewriter,
not only did the paper move this way,
the little typing head moved this way
so a carriage return moves the curser all the curser all the
way back to the left, a new line moves it down one line
so this is another sort of stupid thing
from religious battles past so in the Windows world,
most text files end with what's called a carriage return
and a line feed.
Crlf, which means rn.
in the Linux world people were much more conservative,
they just used new lines and n in many systems, Macs and,
actually not so much Mac sometimes,
so in Linux computers n means yes go to the next line
and go all the way to the left so I'm just kind of grimacing
at some of these details because these things too,
are little things you trip over early on.
they're fundamentally not that interesting but you get used
to little trivia or minutia like this.
But this does beg the question, if n means new line,
r means carriage return,
how do you just write a literal ? Well the answer is there
but this is a common pattern, what is it?
so unfortunately a little weird, you kind of have
to have a corner case like that if you want a literal
and a string you escape the but this notion
of escaping is something we'll see quite a bit.
Well, it turns out we can do stuff
with not just ints not just strings, there are chars
for individual characters,
doubles for floating point values and float
for floating point values and all
of these have some different meaning, in fact let me go ahead
to an example that's not just text space now,
but just to demonstrate, again, to underwhelm,
at first because we can definitely one
up ourselves easily that math is not hard and c.
so here's a program that assigns a variable called x,
I'll type integer the value of 1;
variable called y type is int, assign it a value of 2 right,
new syntax, not interesting.
Finally I declare a third variable called z its also going
to store an int, its going to store the sum of x and y,
looks pretty good but let me go ahead and let me run GCC
of math1.c okay, interesting.
So it turns out GCC, especially the way we have it configured
on nice.faf can also be a bit anal and a little cautious
on your behalf saying you know what,
odds are you screwed up here.
right? its saying it very nicely,
warning unused variable z but that's telling you
that you did something, you're not using it,
probably wasn't a very productive program you just
wrote, but let's try it.
let's be stubborn and let's run a.out enter, hmm.
Is it not working?
Its actually working perfectly.
Right, the math is correct, what is in z
at this point, oh, missed it.
the problem is what?
Simple answer.
I'm not doing anything with it, yes I created 3 variables,
all of them at some point in time, do in fact have numbers
like 1 2 and hopefully 3 inside of them
but if you don't do anything with it,
sort of the tree falling in the woods right?
no one knows that the answer you just computed was three unless
you print it, save it, do something with it,
so hopefully there's a solution here.
so let's take a look at the second, slightly fixed version
of this, so how do I print out the result?
Well again, let's just go into our little tool kit that's going
to start being filled with more function still,
Print F quote unquotes the quotes are important,
the percent means display this as an integer and then I pass
in z and so, okay, so now not only have I written a correct
program, I'm actually doing something productive with it,
a.out, now gives -- hmm, still a little weird, its not incorrect
but what's that explanation for this weirdness.
Right, so there's just no new line, so let me go in here
and I'm going to move over here, add between the quotes
but after the percentn let me recompile,
another little Linux trick, if you get tired
of typing the same commands over and over again,
you can either hit up and that will scroll
through the commands you previously typed
or you can hit !
aka bang and type the first letter or so of the last command
that starts with that sequence and hit enter
and if I haven't forgotten some other g command I wrote,
it should remember, aha, it was gccmath2.c,
again little things you pick up over time.
and if I run a.out,
okay slightly prettier, slightly prettier.
So what else can we do here, well there's going
to be other data types still but there's also this format strings
so let's see if we can't do a little bit
of something that's more interesting underneath the hood
so it turns out we have format strings,
but there's also this approach here,
let me go back to the code.
This is a program, again, that you have a print out of,
called size of, and here's where we finally scratch the surface
of actual design issues.
Actual questions that you are interested to answer
because they really will affect the correctness of your program
and what you can do with it.
so this program, its not very long, its about half comments
and half actual code,
notice that at the top here I've declared some variables
in advance, so this is kind of interesting.
Even though thus far I've very efficiently,
very analy been declaring a variable on the left
and then doing something with it on the right, all in one line,
you don't need to do that.
If you know you're going to need a variable,
you can declare the variable earlier in your program
and then use it later and you'll learn over time when this is
and isn't appropriate, but here I've declared three types
of values.
A char, a double, a float and an int.
a float is a floating point value a real number,
something with a decimal point, what's a double perhaps?
Any ideas?
So its twice something,
its actually a floating point value that's twice
as big potentially.
So it turns out that in this language and in some others,
there are actually pre defined meanings
for how many bits are used to represent a data type,
so we have chars or 8 bits on a typical modern computer
with intel inside, a double is, and let's come back to that.
A float is 32 bits, now that's pretty big,
because if an int is also 32 bits, what did we say last week,
the biggest number you can represent
with 32 bits is, like 4 billion.
Now wait a minute, if you're omitting negative numbers
so if its negative numbers also then its
like negative two billion, to positive 2 billion roughly
but a total of 4 billion
so floating point values also use 32 bits but they use them
to have some numbers before the decimal point,
some after so you can't, it's a trade off
between how big a number you can represent
as a floating point value versus how many points you can have
after the decimal point but a double now is not 32 bits,
right, I'm kind of beyond the scope of my arms here,
its 64 bits and that's pretty damn big
so you'll have some discretion there so what does it mean
when I run this down here, Print F all right, so print an int
and a new line, but what do you want to print?
Well there's this operator in c, that's only occasionally useful,
today its marginally enlightening, tell me the size
of the variable c. tell me the size of the variable d, f,
and i so let me go ahead and run a GCC on sizeof.c now I'm going
to go ahead and run a.out, wait a minute, those are not
in fact values like 32 and 64.
what am I seeing?
Yeah, I'm seeing bytes.
That's kind of interesting, okay, so that's fine,
let me take a look at one other thing.
I saw on the screen a moment ago that there's this mention
of longs so let me say long is an l and let me print
out its length here, so long and I think its l, hopefully, %,
let's see, GCC sizeofc, oops not l, dammit, what is it?
is it also d?
yes all right.
if I run this now what do you think a long is going to be?
So int was 4, also 4, so again, sort of a historical artifact,
when I mentioned long longs before,
so if you want an int that's bigger than 32 bits,
you want a 64 bit int, its not a long, because a long is the same
as an int, it's a long long that you want.
So again, sort of random c trivia.
But I thought we'd end with this.
Here's a crazy looking program, I loaded this off the internet,
written by a really, either a really brilliant person
or a person with really a lot of free time.
so this is an obfuscated C program
and you are entering a community that you don't have to stay
in in perpetuity of people who like writing programs like this,
or have the ability to do it and there are contests
that people run for obfuscated c contests whereby you challenge
people to write the most confusing looking program you
could possibly come up with that no human should be able
to understand and then you challenge the audience,
the other geeks in the room, what that program actually does,
do you have any idea what this is going to do?
We will not be writing things like this.
This is an exercise in the opposite of good design,
but its kind of a powerful thing,
that if we run the compiler here, the warnings I'm going
to ignore for now, that's fine.
A.out, I'll give you the results
of your first obfuscated c program which is,
that's what that does.
We'll see you on Friday.