CS50 Video Player
    • 🧁

    • 🍮

    • 🍎

    • 🍿
    • 0:00:00We will begin shortly
    • 0:08:08Introduction
    • 0:12:39CS50 IDE
    • 0:15:06hello, world
    • 0:18:25Compiling
    • 0:22:27Functions
    • 0:26:07Return Values
    • 0:26:29Variables
    • 0:31:55Format Codes
    • 0:35:55CS50 Library
    • 0:43:45main
    • 0:44:53Header Files
    • 0:47:28help50
    • 0:52:52Style
    • 0:54:16style50
    • 0:58:30check50
    • 1:02:29CS50 IDE's File Browser
    • 1:05:05Break
    • 1:12:30CS50 IDE's Command-Line Interface
    • 1:21:40Types
    • 1:24:05Format Codes
    • 1:24:57Operators
    • 1:25:50addition
    • 1:31:02Integer Overflow
    • 1:36:39truncation
    • 1:37:30Typecasting
    • 1:44:06Syntactic Sugar
    • 1:46:31Conditions
    • 1:53:09agree
    • 1:56:41Boolean Expressions
    • 1:58:14Loops
    • 2:14:32get_positive_int
    • 2:18:44Scope
    • 2:21:33Mario
    • 2:29:25Floating-Point Imprecision
    • 2:33:17Integer Overflow
    • 0:02:19brian you can open the second room too there we go
    • 0:08:10all right this is cs50 and this is week one our second week and today
    • 0:08:16recall that we'll we'll focus on this other programming language called c
    • 0:08:19and we gave you a little glimpse of this last time
    • 0:08:22where and i propose that this code here on the screen is something that you
    • 0:08:26will soon know how to program if not already but suffice it to say it looks
    • 0:08:29quite a bit dissimilar to what we looked at last week which of
    • 0:08:32course was scratch which was much more playful much more graphical
    • 0:08:36and so allow me to disclaim right from the get-go today that
    • 0:08:39uh for most of us today will feel like a bit of a fire hose in fact pictured here
    • 0:08:43is an old hack from mit wherein some industrious seniors hooked
    • 0:08:47up an actual fire hydrant to a water fountain
    • 0:08:50uh the saying being that getting an education from mit is like drinking from
    • 0:08:53a a fire hydrant today will feel a little
    • 0:08:56bit like that because this is sort of a special occasion
    • 0:08:59that you don't really have occasion to describe very often but it's one in
    • 0:09:02which we're all going to learn a new language and indeed that's not
    • 0:09:06something we do every day and so at the first glance it's going to look like a
    • 0:09:09lot of new syntax a lot of new ideas and yet
    • 0:09:12allow me to reassure that what will soon look like this
    • 0:09:16this c code here is fundamentally the same thing that you've
    • 0:09:20seen and now experimented with last week by way of scratch
    • 0:09:23that is to say within this other programming language c which is more
    • 0:09:26traditional which is more text based more keyboard based
    • 0:09:29we're still going to see functions conditions boolean expressions loops
    • 0:09:33and so on they're going to all look a little different but
    • 0:09:36the ideas are the same and so much like when uh walking into someone's home for
    • 0:09:42the very first time and getting the lay of the land and seeing a lot of things
    • 0:09:45that you haven't seen before you typically don't care about all of
    • 0:09:48those visual details you might just simply
    • 0:09:50walk forward and sit down or perhaps uh uh and begin your conversation similarly
    • 0:09:55today we're about to see a whole lot of details in the world of this programming
    • 0:09:59language but the goal at hand is to ignore things
    • 0:10:02at first glance that we don't necessarily
    • 0:10:04understand and latch on to those ideas that are familiar
    • 0:10:07from last week so how do we go about actually
    • 0:10:11writing computer programs how do we go about uh writing them well
    • 0:10:15and so allow me to propose that there's a few guy a few
    • 0:10:18guiding lights that should guide writing of code one of course is correctness and
    • 0:10:22we explored this last week whereby the correctness of your code just speaks to
    • 0:10:26does it work as intended when you double-click some icon when you run some
    • 0:10:30command does the program that you or someone
    • 0:10:33else wrote behave correctly does it do what it says
    • 0:10:36but there's other aspects to writing good software
    • 0:10:40and writing good programs and that has to do with design and we alluded a
    • 0:10:44little bit to this last week but with design it's more of a qualitative a more
    • 0:10:48subjective measure just how well written your code is so
    • 0:10:52imagine for instance from taking a class where you have to write
    • 0:10:55essays you could certainly make very correct arguments
    • 0:10:58but you could make very correct arguments by writing very long
    • 0:11:01rambling sentences repeating yourself and generally not
    • 0:11:05writing a very good essay or paper now it might be correct
    • 0:11:08there's nothing in that paper for your english class history class or whatever
    • 0:11:11it may be that you said that was incorrect but you
    • 0:11:14might not get very good marks on it because it's just not very well
    • 0:11:17designed and so similarly in the programming world is there this notion
    • 0:11:20of actually writing not only correct code
    • 0:11:23but well-designed code wherein you don't repeat yourself
    • 0:11:26you write code that's fairly efficient it doesn't do more
    • 0:11:29uh more work than it actually needs to and then lastly let me propose for today
    • 0:11:34onward in this class that there's a third access you should
    • 0:11:37keep in mind when it comes to writing good code and that has to do with style
    • 0:11:42this is much more of an aesthetic so this in the analogous world of
    • 0:11:46writing an essay would be are you using good punctuation
    • 0:11:49capitalization are you indenting new paragraphs and
    • 0:11:52those kinds of aesthetics that fundamentally don't change the
    • 0:11:55correctness of what you're saying don't change necessarily the quality of
    • 0:11:59the arguments that you're making but the style of your essay much like
    • 0:12:03the style of your code makes your code much much more readable
    • 0:12:07so when it comes to writing good code you want it first and
    • 0:12:11foremost to be correct but also well designed
    • 0:12:13and also well styled much like again you would when writing an essay that you
    • 0:12:18would hope would reflect well on your capabilities as well
    • 0:12:23so when it comes to writing code like this for instance this first c program
    • 0:12:27that someone last week proposed quite simply
    • 0:12:29prints out on the screen hello world well how do we go about writing this
    • 0:12:33code last week we wrote code by going to scratch.mit.edu and then
    • 0:12:37dragging and dropping puzzle pieces today is going to be a
    • 0:12:39little different we're going to use a different tool here on
    • 0:12:42out and we're going to use our keyboard much more than our mouse to actually
    • 0:12:45program but to do so we're going to go ahead and
    • 0:12:47introduce the first of several tools this semester
    • 0:12:50this one known as cs50 ide ide is an acronym a term of art in
    • 0:12:55programming that stands for integrated development environment which is just a
    • 0:12:59fancy way of saying in this context that cs50 ide
    • 0:13:02is cs50's own web-based programming environment and it's not
    • 0:13:06specific to cs50 per se we've simply added a number of educationally useful
    • 0:13:10features on top of a third-party cloud tool
    • 0:13:14that anyone on the internet can use and our own version thereof lives at this
    • 0:13:18url ide.cs50.io so you're welcome to follow along at that url today but you
    • 0:13:23need not during lecture itself but on this coming
    • 0:13:26upcoming problem set and beyond will you actually use
    • 0:13:29and get more familiar with this tool hands-on so let me go ahead and open up
    • 0:13:33this tool here so i've already logged in in advance
    • 0:13:36and what you see here is the basic user interface that's available to you
    • 0:13:41and fortunately there's only a couple of salient features that we need point out
    • 0:13:44right now so at the top of the screen here is just
    • 0:13:47a big black rectangle that in a moment is going to be filled with code much
    • 0:13:51like using google documents or something like that where you can create new tabs
    • 0:13:55and create new files this is where i'm going to do my
    • 0:13:57programming along the top of the screen and along the bottom is what we're in a
    • 0:14:01moment going to start calling our terminal window it's in this
    • 0:14:04terminal window that i can actually run commands and
    • 0:14:07ultimately run my actual code but let's go ahead and
    • 0:14:10write our very first program in this environment
    • 0:14:13and realize that this tool indeed is not very cs50 specific it's meant to be
    • 0:14:16representative of a very common popular programming
    • 0:14:20environment where you have a so-called text editor
    • 0:14:22or tabbed windows where you can write code and a terminal window where you can
    • 0:14:26actually run commands ours happens to exist in the cloud but you can
    • 0:14:30alternatively program certainly on your own mac or pc
    • 0:14:33or any other device these days but frankly it tends to involve just a
    • 0:14:36non-trivial number of technical difficulties early on especially when we
    • 0:14:39all have different versions of mac os and windows and the like
    • 0:14:42so this cloud-based environment just ensures that on day zero
    • 0:14:46we can all have the same exact programming experience
    • 0:14:49so i'm gonna go ahead and do this i'm gonna go ahead and go up to file and new
    • 0:14:53file and this is gonna create a new tab by
    • 0:14:56default called untitled not very interesting so i'm going to now
    • 0:14:59go up to file and save and by default i'm going to save this
    • 0:15:02file as for instance the name hello.c so i want to write my very first
    • 0:15:07program in this language called c i'm going to
    • 0:15:10call my file hello but i'm going to end it in a file extension
    • 0:15:13called dot c and that's indeed the convention when writing
    • 0:15:17c programs they should end with dot c just like scratch programs as you may
    • 0:15:21recall end in dot sb3 so i'm going to go ahead and simply
    • 0:15:25click the green button here that's called save nothing is
    • 0:15:27really going to change except for the name and the tab there
    • 0:15:30now i see a top left that this tab is called hello.c
    • 0:15:34and now i can start typing anything i want and frankly i'm just going to type
    • 0:15:37from memory the very first program we saw last week
    • 0:15:40and just a moment ago i'm going to do include standard io dot
    • 0:15:43h whatever that is for now i'm going to going to do int main void
    • 0:15:48whatever that is for now i'm going to use a curly brace and then
    • 0:15:51close that curly brace so to speak thereafter and in here i'm going to go
    • 0:15:55ahead and do printf quote unquote hello world followed by
    • 0:16:00a semicolon now that was a whole lot of text right off the top of my head
    • 0:16:05this is the kind of muscle memory that you'll soon develop when writing a
    • 0:16:07program i've of course done this many times before so i was able to just
    • 0:16:11do it off the top of my head but in a moment
    • 0:16:13we'll tease apart what all of the various lines and characters that i
    • 0:16:16typed actually do but what i'd now like to do
    • 0:16:19is run this program we conjectured last week
    • 0:16:22that this is just going to print hello world but how
    • 0:16:25well in the world of our macs and pcs and phones we would all just tap an icon
    • 0:16:30if we want to actually run a program that's not going to be the case today
    • 0:16:34because now we're in more of a traditional programming environment
    • 0:16:38the environment that we're now in requires that i use my keyboard
    • 0:16:42a little bit more or what's known as a command line interface or cli
    • 0:16:46this is in contrast with a graphical user interface or
    • 0:16:50gui or gui which is what describes mac os windows ios and
    • 0:16:54android but in a command line interface i have to do everything
    • 0:16:57at a so-called command line and by that i'm referring to this blinking prompt
    • 0:17:03along the bottom reported along the bottom of my screen
    • 0:17:06here again i described as the terminal window
    • 0:17:08where i'm going to type commands and this is my command line interface
    • 0:17:12anything i type here is going to get sent to this computer
    • 0:17:15and hopefully execute uh on its own hardware so
    • 0:17:20how do i do this and what do i do well the catch of course
    • 0:17:24is that when writing code in c or python or java or bunches of other languages
    • 0:17:29that happen to exist that's really meant to be written and
    • 0:17:32understood by me and you the humans but per last week what is the only
    • 0:17:37language in a sense that computers understand brian could we
    • 0:17:40call on someone for this what language do computers
    • 0:17:44only speak because i think there's a disconnect
    • 0:17:49between where we left off last week and where we're currently at
    • 0:17:52if i'm writing code that now looks like this cindy
    • 0:17:55what language do computers speak would you say
    • 0:17:59um binary binary and just elaborate a little bit what do you mean by binary to
    • 0:18:03recap yeah so they use ones and zeros to
    • 0:18:07represent everything exactly they use ones and zeros to
    • 0:18:10represent everything and last week we focused on only things like numbers and
    • 0:18:15letters then colors and images and videos and sound and so forth
    • 0:18:18but we didn't all actually speak to built-in functionality
    • 0:18:21which computers also use zeros and ones to represent that is to say computers of
    • 0:18:26course have the ability these days to print something on the screen and
    • 0:18:31that notion of printing that function that functionality also is
    • 0:18:35represented underneath the hood of a computer so to
    • 0:18:37speak by some pattern of zeros and ones which is to say that everything i just
    • 0:18:42typed even though it kinda sorta looks like english and kinda sort of
    • 0:18:46clearly says print hello world the computer
    • 0:18:49ironically does not know what it is i have just typed
    • 0:18:53in order for to understand what i've just typed i need to actually convert it
    • 0:18:56to zeros and ones and so indeed the next step in this process is to take what
    • 0:19:00i'll describe as my source code written here in c and last week too we
    • 0:19:05saw source code it was just graphical it was those puzzle pieces in scratch
    • 0:19:08this is my source code that even though cryptic is something i at least the
    • 0:19:12human eventually can read and write but i need to literally convert it to
    • 0:19:16patterns of zeros and ones that the computer can understand
    • 0:19:19now how do i get to this point well thankfully we have a mental model from
    • 0:19:22last week that involves problem solving and here's
    • 0:19:25a problem how do i take source code written in this language supposedly
    • 0:19:28called c and convert it to zeros and ones that
    • 0:19:31the computer understands well my input i dare say is going to be
    • 0:19:34my source code and my uh output ideally is going to be
    • 0:19:39what we'll call machine code machine code is just a term
    • 0:19:42of art describing the zeros and ones that computers understand
    • 0:19:45we didn't use that expression last week but this just refers to zeros and ones
    • 0:19:49on the right and c code on the left so that invites
    • 0:19:52the question well what is between my source code
    • 0:19:55and machine code if i take my source code and feed it into the proverbial
    • 0:19:59black box how do i get out of this black box the
    • 0:20:03zeros and ones that the computer understands
    • 0:20:04well i need a special program that we're going to start calling a compiler
    • 0:20:08a compiler is a program that you can download for free
    • 0:20:12or pay for back in the day that is a program design
    • 0:20:15to convert source code to machine code so all i need do
    • 0:20:19if i want to actually solve the problem as stated earlier whereby
    • 0:20:23i have written this code in c and i now need to convert it to zeros and ones i
    • 0:20:27just need to give myself access to a compiler and it turns out one of those
    • 0:20:32exists within cs50 ide and this is a tool you could download on
    • 0:20:35your own mac or pc or the like and for now we're going to describe that
    • 0:20:39tool as being quite simply called make literally if i want to make my
    • 0:20:44program i'm going to go ahead and type make
    • 0:20:46hello and then i'm going to run it with a little bit of a cryptic syntax
    • 0:20:49dot slash hello but let's see that in action to tie this all together
    • 0:20:54i'm going to move my cursor down into my terminal window
    • 0:20:57or my command line interface and i'm going to literally type hello
    • 0:21:00notice i am not typing make hello dot c i'm typing the name of the program i
    • 0:21:06actually want to make and i just want to call this program
    • 0:21:08hello the compiler is going to infer from this command
    • 0:21:13that i actually intend to compile a file called hello.c
    • 0:21:16i'm going to go ahead and hit enter there's some crazy cryptic output more
    • 0:21:19on that another day but the fact that i don't see any big
    • 0:21:22scary red error messages is a good thing this means that my program compiled
    • 0:21:27successfully why there's just no yellow or red messages
    • 0:21:30to say otherwise but now if i want to actually run this
    • 0:21:34program i need to type a different command that's the analog of double
    • 0:21:37clicking an icon on your mac or pc or phone
    • 0:21:40i'm going to type literally dot slash dot slash hello essentially is like
    • 0:21:45saying go into the current folder on the computer i'm using and look for a
    • 0:21:49program called hello i'm going to go ahead and hit enter and
    • 0:21:52voila hello world now i see again a dollar sign and some
    • 0:21:57other text on the screen and we'll tease apart in just a bit what this means
    • 0:22:00but notice this dollar sign is just a constant visual reminder
    • 0:22:04of where my prompt is where i can type more commands
    • 0:22:08and the computer has done literally what i asked it printed out hello world and
    • 0:22:12now it's waiting for my second command so that was the analog of just printing
    • 0:22:15hello world out of the cat's mouth last week but surely we
    • 0:22:19can do more than this but let's tie it back to
    • 0:22:22what we did last week as well so that not everything here is all that
    • 0:22:25dissimilar so recall that last week we had
    • 0:22:28functions and it turns out we had something called
    • 0:22:31arguments even though we didn't necessarily describe them as such so a
    • 0:22:34function is like a mini program it's an action or
    • 0:22:37a verb that you can use when writing your own program
    • 0:22:40that does something we saw the say block last week we saw the wait block last
    • 0:22:44week those were verbs or actions or more generally known
    • 0:22:47as functions but functions can also take inputs recall and we did see that last
    • 0:22:51week and nowadays we're going to start calling
    • 0:22:53inputs to functions arguments so to speak another term for them is
    • 0:22:58parameters but for all intents and purposes those are
    • 0:23:01synonyms for our purposes arguments are the inputs to functions
    • 0:23:04so let's consider then from last week this say block that we saw last
    • 0:23:08time which simply is trying to say out of the cat's mouth hello world
    • 0:23:12well let me go ahead and convert this if you will to corresponding c code
    • 0:23:16just to emphasize how similar fundamentally
    • 0:23:20these two languages are even though syntactically c absolutely looks
    • 0:23:24uh visually different it turns out that if you want to say something
    • 0:23:27in c using this text-based language like i just did
    • 0:23:31you're not going to write se you're instead going to write print that's
    • 0:23:34actually a bit of a white lie you're not going to say print you're actually going
    • 0:23:37to say print f for reasons we'll eventually see
    • 0:23:39it means print a formatted something or other so printf is the
    • 0:23:43analog in c of say in scratch now notice in c i've
    • 0:23:47got this open parenthesis and closed parenthesis that nicely enough are kind
    • 0:23:50of ovular in shape and notice that they kind of mimic the white oval
    • 0:23:54into which we provided input last week so in between those parentheses
    • 0:23:58are going to be my inputs to this function printf otherwise known as
    • 0:24:02arguments but they're going to be a little different this week i yes i'm
    • 0:24:05going to say hello world with a comma in between grammatically
    • 0:24:08just like last week but in the world of c
    • 0:24:11i have to be a little more particular i also have to add double quotes
    • 0:24:15on the left and the right and somewhat annoyingly i also have to add
    • 0:24:19a semicolon at the end of this line of code so just like in english or in a lot
    • 0:24:23of human languages you end a sentence for instance with a period
    • 0:24:26in many lines of code you will write and see
    • 0:24:29you've also got to finish your thought in this case with a
    • 0:24:32semicolon so this then on the right is the closest way of translating
    • 0:24:37this thing on the left from scratch to c respectively
    • 0:24:41so the ideas are still the same but the syntax looks a little different and we
    • 0:24:44just got to ingrain in ourselves ultimately
    • 0:24:47what these patterns are and what these human conventions are but notice that
    • 0:24:50what we just did follows the same paradigm
    • 0:24:53as last week but let's add a little more terminology this week
    • 0:24:56last week we described the black box as potentially being algorithms
    • 0:25:00initially and then we started calling them functions functions are just a
    • 0:25:04programmed version of an algorithm the implementation of an algorithm in code
    • 0:25:09in software so a function might be represented here as taking inputs
    • 0:25:13otherwise known now as arguments but it turns out
    • 0:25:16that functions can do at least two different types of things
    • 0:25:19in the world of programming and we've seen these things already but we didn't
    • 0:25:23describe them quite as particularly as we will today
    • 0:25:26when a function takes inputs that is to say arguments just like
    • 0:25:30hello world is an input to the say block in scratch or the hell
    • 0:25:34or the printf function in c functions can have what are called side
    • 0:25:39effects and recall we did see this last time
    • 0:25:41when we used the say block it did output something but more technically it had a
    • 0:25:47side effect a visual side effect when i used the say block last week
    • 0:25:50and the printf function this week you see something on the screen
    • 0:25:54and that is yes some form of output but it's a little different from a different
    • 0:25:58form of output that we saw last time so a
    • 0:26:00side effect of a function is often something visual that happens
    • 0:26:05on the screen like text or audio in that case but
    • 0:26:08there's this other feature of functions that we're going to see and leverage
    • 0:26:11today known as return values where a function can
    • 0:26:14really just hand you back a value it's not going to say it on the screen it's
    • 0:26:18not going to vocalize it audibly it's going to just
    • 0:26:21pass it back to you in a way that you the programmer can reuse
    • 0:26:25whatever the output of that function was ideally storing it even in a variable
    • 0:26:29so for instance recall last week that we asked the human their name
    • 0:26:33by way of this ask block and the input to the ask block
    • 0:26:36in this white oval was what's your name and then recall that this ask block was
    • 0:26:40a little special last week because it gave us access to whatever
    • 0:26:44the human ultimately typed in and that is to say that the ask block
    • 0:26:49last week essentially returned a value it didn't
    • 0:26:53just blindly display whatever word the human typed in
    • 0:26:57on the screen no it instead returned it in some sense
    • 0:27:00metaphorically and stored it in a special variable
    • 0:27:04called answer and so again that's the difference the
    • 0:27:06say block literally says something on the screen and there's an immediate
    • 0:27:10visual effect with the ask block after you type in
    • 0:27:13your name you don't see your name printed or displayed again
    • 0:27:18on the screen instead your name is sort of tucked away in a variable just like a
    • 0:27:21mathematician would store a number in a variable like x or y or z the onus
    • 0:27:26was then on us the programmer last week to eventually
    • 0:27:28do something with the value my name that was in that variable called answer
    • 0:27:34so how are we going to translate last week's ask block to c this week
    • 0:27:38well it turns out there's different ways to do this in c
    • 0:27:41none of which are very easy unless you use what's called a library
    • 0:27:45a library is code that someone else has written and indeed one of the things
    • 0:27:48we'll use today is the so-called cs50 library which is a
    • 0:27:52bunch of code not terribly much that the staff and i
    • 0:27:55wrote just to make it easier to do simple things these are training wheels
    • 0:27:58of sorts that will take off completely within a few weeks time but
    • 0:28:02in order to get started quickly it's going to make it easier to do things
    • 0:28:05like getting text from the user so string is a term of art in the
    • 0:28:09programming world a string is text it's a word it's a letter it's a
    • 0:28:13paragraph it's a page of text it's just text in some form string is what a
    • 0:28:18computer scientist would call text getstring is a function that we wrote
    • 0:28:23that we will provide to you that does take inputs notice per the
    • 0:28:26parentheses here in c it can take input what might that input
    • 0:28:30be well just like the ask block it's going to be a prompt
    • 0:28:33that the human should ultimately see so there's a bit more involved though than
    • 0:28:37just using this function when you use get string in c as we will
    • 0:28:42soon see in a live demo you want to do something with the
    • 0:28:45human's name and to do something with the human's name it's not quite
    • 0:28:48sufficient to just trust that scratch will put it in a variable for you
    • 0:28:52in c as with most programming languages it's a lot more pedantic like if you
    • 0:28:57want something to end up in a variable you've got to do it yourself mit is not
    • 0:29:01going to magically put it in an answer variable for you you have to do it
    • 0:29:04yourself so to do this you simply come up with
    • 0:29:08the name of the variable that you want be it x or y or z or more compellingly
    • 0:29:12answer and you use an equal sign a single equal sign
    • 0:29:16and even though in math this generally implies equality
    • 0:29:19in the context of c in most programming languages
    • 0:29:23the equal sign actually means what we'll call assignment
    • 0:29:26it means effectively copy whatever is on the right
    • 0:29:30into whatever's on the left so if on the right hand side there's a function whose
    • 0:29:34purpose in life is to ask the human what their name is
    • 0:29:38that name is going to get copied from right to left ultimately
    • 0:29:42into this variable called answer mit did that automatically for us in scratch
    • 0:29:47in c you have to do it yourself but you have to be a little more particular too
    • 0:29:52it turns out that in the world of c you can't just have variables you have to
    • 0:29:56tell the computer in advance what type of variable you want
    • 0:29:59and specifically i'm going to tell the computer that the type of variable i
    • 0:30:03want is going to be a string and the convention for doing so
    • 0:30:06is you literally write the name of the type that you want
    • 0:30:10string being the only one we've seen thus far then you write the name of the
    • 0:30:13variable and then again to assign a value to that
    • 0:30:16variable from right to left we have to use the single equal sign
    • 0:30:21here and now just a quick pause brian if we could call on someone for
    • 0:30:25this even if you've never programmed before
    • 0:30:27if you've taken to heart one of my pieces of advice earlier
    • 0:30:31i'm still missing something how might i want to
    • 0:30:34finish the translation of this scratch code to see on the right hand side what
    • 0:30:38is missing from what you can tell if you've
    • 0:30:42programmed before odds auto will jump right out if you've never programmed
    • 0:30:45before you'll have to think back on what i said
    • 0:30:47earlier jacob what do you think uh semicolon exactly there's just one
    • 0:30:52single stupid semicolon missing at the end of the line
    • 0:30:56which is necessary to make clear to the computer that this is the end
    • 0:30:59of this thought and i sort of impugned it as stupid because honestly
    • 0:31:02one of the biggest frustrations when learning how to code as will now happen
    • 0:31:06today and this week and beyond is initially you're going to forget
    • 0:31:09stupid things like the semicolon or you're going to forget a single quote
    • 0:31:13mark or a parenthesis or things like this and
    • 0:31:16the most important like advice i can give today is this
    • 0:31:18try not to get frustrated by those kinds of stupid things it's a lot more
    • 0:31:23interesting it's a lot more useful in life to understand functions and loops
    • 0:31:27and conditions and not to let yourself get frustrated
    • 0:31:30by the more minor aesthetic things that honestly will just come with
    • 0:31:33practice so if the very first mistake you make
    • 0:31:36is missing a semicolon and it takes you 10 minutes an hour to figure out
    • 0:31:39what is wrong with your code totally normal and that those frustrations go
    • 0:31:43away quite quickly in time so
    • 0:31:47we then have this translation of one function to another
    • 0:31:50where else can we take it from here well let's take a look at one
    • 0:31:54other translation that we used after that ask block last week
    • 0:31:57after we asked the human their name as by providing an input to the function
    • 0:32:01and getting some output we then proceeded to do something
    • 0:32:05with the return value or output of ask again we asked the human their name
    • 0:32:11scratch magically last week put the name into the answer variable but then i
    • 0:32:16did something with it and this is what i mean
    • 0:32:19to distinguish side effects which just kind of happened to you
    • 0:32:22like something printing on the screen like the cat saying something
    • 0:32:26out of its mouth versus a return value which is a piece of information like a
    • 0:32:31name a human has typed in being stored somewhere where you the
    • 0:32:35programmer can make use of it later it's not happening to you it's
    • 0:32:39being handed to you for subsequent use and last week in order to say hello
    • 0:32:44comma world all in one breath so to speak
    • 0:32:46we kind of had to stack these puzzle pieces on top of one another making the
    • 0:32:50output of join the input of say in c this is actually a
    • 0:32:54little easier even though it's some new syntax
    • 0:32:57again printf is going to be the analog for say and we've seen that
    • 0:33:00a moment ago we're still going to have the parentheses we're still going to
    • 0:33:04have the semicolon so what goes inside now the input
    • 0:33:07how do i provide hello comma answer to c well i'm going to go ahead and do
    • 0:33:12hello comma in double quotes but then this strange
    • 0:33:16new syntax here percent s this is what we're
    • 0:33:20going to call a format code a format code and hence the f in
    • 0:33:25printf printf doesn't just print things it can
    • 0:33:28print format codes as well and this is just fancy syntax for saying
    • 0:33:32plug in some actual value here don't print out
    • 0:33:36percent s literally this is a placeholder for what will be s
    • 0:33:40a string well what do i want to put there here's something new too
    • 0:33:44in scratch if you had two inputs to a function you would have two ovals like
    • 0:33:48this and this one here that you could either
    • 0:33:50type words or numbers into or drag variables into in c there's no notion of
    • 0:33:55ovals or graphics at all so instead we're just going to go old
    • 0:33:58school and just use a comma if you use a comma in between the
    • 0:34:02parentheses as the arguments or inputs to a function
    • 0:34:06that's going to separate the one on the left
    • 0:34:08from the one on the right thereby being analogous to having two ovals
    • 0:34:12in the world of scratch now there's something that's potentially a little
    • 0:34:15visually confusing here there's actually two commas here of
    • 0:34:18course and here but notice the important detail that
    • 0:34:22first one that's an english grammatical comma
    • 0:34:25that i've put inside of my quoted string my quoted phrase of text that has
    • 0:34:30nothing to do with programming that just has everything to do with english the
    • 0:34:34fact that this comma is outside of those double quotes though
    • 0:34:37means it's significant in this language called c and it separates first argument
    • 0:34:41from second argument and so in this way do we have the ability now to also
    • 0:34:47say something on the screen using uh printf in this slightly new way so let
    • 0:34:53me go ahead then and do this for real let me go back to
    • 0:34:58cs50 ide and i'm going to go ahead and go back into this program here
    • 0:35:03and consider for just a moment how we can improve upon this very first program
    • 0:35:07which literally just prints hello world not
    • 0:35:10that interesting i can run it all day long it's going to say the same thing
    • 0:35:13how do i now get input from the user well it turns out
    • 0:35:17that i can simply enhance this code a little bit let me go ahead
    • 0:35:21and per the translation of scratch earlier
    • 0:35:24let me do something like string answer equals get underscore string
    • 0:35:31quote unquote what's your name so i'm literally typing out what we saw in c a
    • 0:35:36moment ago i'm going to remember my semicolon over here
    • 0:35:39and then i have to change the second line of code now to not say hello comma
    • 0:35:43world but instead to say hello comma percent s and then outside of the
    • 0:35:47double quotes i'm going to do a comma and then provide literally the word
    • 0:35:52answer which is the name of that variable
    • 0:35:55but i'm not quite done and this is a little subtle and invariably you'll
    • 0:35:58forget this at some point too in order to use get string i have to use
    • 0:36:02this thing called the cs50 library code that the staff wrote that you don't
    • 0:36:06have default access to and in order to do that
    • 0:36:09i need to add one line of code that will explain in more detail in a little bit
    • 0:36:12but for now just take it on faith that by adding this line of code
    • 0:36:16at the very top include cs50.h that will now give me access to the
    • 0:36:22getstring function which i otherwise wouldn't have
    • 0:36:25access to all right now i'm going to go back to the terminal window
    • 0:36:28and notice the dichotomy here if i just run dot slash hello
    • 0:36:31sort of enthusiastically let's see what my new program does i'm about to be
    • 0:36:35unfortunately disappointed because it still says hello world and you might
    • 0:36:39realize intuitively what the problem of course here is i haven't actually
    • 0:36:43recompiled the code and so anytime you make a change to your code it does not
    • 0:36:47suffice to just save the file via file save or control or command
    • 0:36:51s i need to recompile it and to recompile my code that's not such a big
    • 0:36:55deal i just type make hello enter cross my
    • 0:36:58fingers that there's no yellow or red scary text
    • 0:37:01this is all good it seems to have compiled into machine code
    • 0:37:05now i can retype dot slash hello and enter and you'll see now my program is
    • 0:37:10running and waiting for me let me go ahead and type my name david enter
    • 0:37:14hello comma david let me go ahead and run it again after clearing the screen
    • 0:37:17let me run it again and this time let's say my name is brian and i see
    • 0:37:21hello comma brian so quite similar to what we did in
    • 0:37:24scratch but now we're more powerfully doing this all thus far
    • 0:37:27via my keyboard alone all right so that was a lot we wrote
    • 0:37:33hello world super quickly off the top of my memory and then enhanced it to now i
    • 0:37:36take input from the user let me pause here if there are any
    • 0:37:40questions you're welcome to ask via chat for either staff or classmates to answer
    • 0:37:44but if you'd like to raise your virtual hand and zoom please feel free
    • 0:37:48so that i can clarify or expound on anything here
    • 0:37:54any questions brian from the group
    • 0:38:02yeah question from uh ryan
    • 0:38:09sorry is that doing me that asks us in the chat but
    • 0:38:13so the string before the answer that's not the name of the variable
    • 0:38:17so you could hypothetically make it like string que
    • 0:38:20you can make it like string a or string anything else it just matters what comes
    • 0:38:25after string is the name of the variable exactly really good question in the
    • 0:38:29world of scratch you were required to use the variable called
    • 0:38:33answer in c we have the complete flexibility over what we want to do so
    • 0:38:37as ryan proposed i could change my variable's name to
    • 0:38:41just a for short i would have to change it elsewhere too
    • 0:38:44to make clear that the variable being used is the same one by a different name
    • 0:38:48that's perfectly fine but here's where we now get into a matter of
    • 0:38:52of better style having a variable called just a doesn't make really lends
    • 0:38:58itself to the readability of your code i might now glance at my
    • 0:39:01code and be like what is the variable a it's a little better when it comes to
    • 0:39:05writing good code to actually be more verbose and using an actual word
    • 0:39:08like answer in this case indeed even though i keep
    • 0:39:12describing x and y and z as the go-to variables for a mathematician those
    • 0:39:15really say nothing outside the context of like a cartesian plane
    • 0:39:18so in a program that you write in c or scratch or anything else
    • 0:39:22using descriptive variable names is a matter of good
    • 0:39:26style as well uh jonathan over to you yeah just a quick question how come we
    • 0:39:31have to compile the code every single time
    • 0:39:34um and unlike different ides which if you just run the code it automatically
    • 0:39:37compiles uh why do we have to manually do it yeah
    • 0:39:40really good question why do you have to keep recompiling the code
    • 0:39:43short answer is just because this is the way c is
    • 0:39:47um it's an older language decade's now old and so back then
    • 0:39:51everything was very deliberate user interface was not the top priority
    • 0:39:55performance instead was for instance and so nowadays there are fancier
    • 0:39:59integrated development environments and some of you
    • 0:40:01might have used things like codecademyonline or code.org where
    • 0:40:05there's very often like literally a play button
    • 0:40:07that you can just click in the user interface and it just plays your program
    • 0:40:11or runs your program what we're doing in
    • 0:40:13this class is showing you what those buttons are doing so if you do use a uni
    • 0:40:17an environment like that that seems to automate this it's still happening but
    • 0:40:20for our purposes certainly at the beginning of the semester
    • 0:40:23we're going to do it manually ourselves later in the term when we introduce a
    • 0:40:26different language all together for instance python
    • 0:40:29then kinda sorta you don't need to compile
    • 0:40:32any more but more on that in a few weeks good question uh sophia
    • 0:40:36over to you i had a question about i noticed in the
    • 0:40:41source code that there's a backslash like n
    • 0:40:43at the end of the string is that necessary for every time even if it's
    • 0:40:47just one line really good question this backslash n
    • 0:40:50that you're seeing elsewhere a bit of a spoiler but yeah let's go
    • 0:40:54ahead and fix this problem that we've seen
    • 0:40:56even though i'm kind of ignoring it you know this now gets a little particular
    • 0:41:00but this looks kind of stupid honestly it says hello comma brian tilda slash
    • 0:41:03dollar sign like that is not my intended output i
    • 0:41:06literally only wanted to say hello brian or hello david
    • 0:41:10this visual artifact here the dollar sign and the tilde and the slash
    • 0:41:14have to do with my terminal window this command line environment that i'm using
    • 0:41:17and honestly just to be a little nitpicky frankly it should probably be
    • 0:41:21on a new line it should just be on its own line so it's not confused
    • 0:41:25with my own output and as sophia notes we there is a
    • 0:41:28solution to this but again per last week you need to be
    • 0:41:31when writing algorithms and in turn code you have to
    • 0:41:34be super precise nowhere in my code have i told the
    • 0:41:38computer to move the cursor to the next line
    • 0:41:41so i can do that explicitly by doing backslash n immediately after the
    • 0:41:47percent s but still inside of the double quotes this is
    • 0:41:51shorthand notation for what would be telling the computer
    • 0:41:55move the cursor to the next line now you might think that well why don't
    • 0:41:58i just hit enter like this and even though this all might be new to
    • 0:42:02most of us suffice it to say that this just feels like it's going to get
    • 0:42:05messy quickly if you start hitting enter in the middle of your code
    • 0:42:08that's probably not the right solution so instead programmers years ago
    • 0:42:12decided to come up with shorthand notation like backslash n
    • 0:42:16otherwise known as an escape character that sim
    • 0:42:19uh signals to the computer put a new line
    • 0:42:22here so backslash n is newline and let me go ahead and recompile this
    • 0:42:26after saving my file let me go ahead and do make
    • 0:42:30hello it seems to compile okay dot slash hello let me go ahead and type in
    • 0:42:34brian's name again and voila still the same output but it's a little
    • 0:42:38cleaner so we're doing a being a little bit better about housekeeping now
    • 0:42:42really good question uh uh bj is it uh yeah so one question i had is
    • 0:42:49why don't you have to call the function get string
    • 0:42:52in order for you for it to ask for input like it still asked your input when you
    • 0:42:56assigned it to the variable answer ah really good question why don't
    • 0:42:59i have to call getstring for instance by putting it on a line of its own
    • 0:43:04the way that c and a lot of programming languages work
    • 0:43:08is they will evaluate an entire line of code for instance what i have here on
    • 0:43:13line six uh from right
    • 0:43:16to left at least in this context when you have an equal sign
    • 0:43:20on this in the code like i do here that's telling the computer
    • 0:43:24you first have to execute that is do what is said on the right-hand side
    • 0:43:28and then whatever the output of that thing is store it on the left-hand side
    • 0:43:32so it is indeed getting executed we're just now
    • 0:43:34spreading things out on longer lines of code if you will if that makes sense
    • 0:43:40so getstring is getting executed because it appears on that line all
    • 0:43:45right so we've been taking for granted frankly a
    • 0:43:48few details of these programs that it's probably only fair to start teasing
    • 0:43:52apart for instance there is that int main
    • 0:43:54void and a whole bunch of other syntax so let's tease apart
    • 0:43:57some of these other lines of code that i just typed off the top of my head for
    • 0:44:00memory but kind of do need to be there in every
    • 0:44:02c program you write let's at least start to make sense of some of that detail
    • 0:44:06recall that in scratch we always started our programs initially
    • 0:44:09with when green flag clicked we eventually saw some other puzzle pieces
    • 0:44:14like when uh you hear an event or when
    • 0:44:17there's camera motion but this really kick-started most of the
    • 0:44:21programs that we wrote in scratch what is the analog in c of the of the
    • 0:44:25when green flag clicked publicly piece it's essentially this we won't spend
    • 0:44:29time in detail today explaining why it's int why it's void
    • 0:44:33why there's curly braces why there's parentheses
    • 0:44:35for today's purposes only let me just stipulate that this is the analog
    • 0:44:39for this when green flagged clicked puzzle piece you've just got to start
    • 0:44:43your programs initially with this kind of boilerplate code so to
    • 0:44:46speak we will start to explain this in much more detail but for now just take
    • 0:44:50on faith that this is how you start writing a program
    • 0:44:53but there's of course a little more to the programs we've written thus far
    • 0:44:56and particularly we've seen a couple of things called header files
    • 0:45:00this is another term of art that refers to a pro a file
    • 0:45:04written in the language called c whose name ends with not
    • 0:45:07dot c but with dot h so we've seen these before
    • 0:45:12as follows here recall was the simplest program we wrote last week in scratch it
    • 0:45:16just says hello world when you clicked on the green flag
    • 0:45:19this is the analog the more complete analog on the right
    • 0:45:22of that program today but there's something missing
    • 0:45:26and it's probably jumping out at some of you because the program is a little
    • 0:45:29shorter than it was before something's missing
    • 0:45:32which is this line here and i just wrote that from memory earlier
    • 0:45:35but it's referring to a file called stdio.h
    • 0:45:39which stands for standard input output dot h so i o is an acronym in the
    • 0:45:45computer world that just generally refers to input and output
    • 0:45:47so standard io.h is just a very popular file that is used in c programs
    • 0:45:54that gives you the ability to get input and output from the user
    • 0:45:58and it does so by providing you with printf for instance which of course
    • 0:46:01allows you to generate some form of output via those
    • 0:46:03those side effects that we described earlier but when i wrote my other
    • 0:46:07program recall that actually had get string as bj noted earlier where i
    • 0:46:11can get a line of text from the user i needed something else altogether
    • 0:46:15and that's when we added a moment ago a second header file called cs50.h
    • 0:46:20so these header files just give you access to more
    • 0:46:23functions than you might automatically get from the language
    • 0:46:27you're using which here is c it's similar in spirit recall last week when
    • 0:46:31i started poking around scratch's extensions menu
    • 0:46:34and i used the translate block and the the voice block the sort of fancier
    • 0:46:38features that were buried under the extensions menu using an extension in
    • 0:46:43scratch is similar to using a header file in c
    • 0:46:46it's giving me access to a bit more functionality than you otherwise get
    • 0:46:50for free out of the box so to speak all right let me pause here to see if
    • 0:46:56there are any other questions before we forge ahead to actually create
    • 0:47:01some problems uh that you two will likely encounter
    • 0:47:05and we'll give you tools via which to solve those problems so that you don't
    • 0:47:09feel completely on your own in a new language any
    • 0:47:13questions brian before we forge ahead
    • 0:47:22no all right i'm going to slowly tiptoe back
    • 0:47:25until brian gives me the cue all right well let me go ahead and propose that
    • 0:47:31there are so many different ways in which i could have screwed up
    • 0:47:35over the past few minutes of writing these programs
    • 0:47:38uh i might have omitted a semicolon as i implied i might have not closed my
    • 0:47:42quotes i might have gotten my parentheses wrong i might have
    • 0:47:45misspelled words altogether there's many different ways i could have
    • 0:47:48screwed that program up and frankly off the record i was sort of crossing my
    • 0:47:51fingers that i didn't screw up our very first program together but
    • 0:47:54invariably at some point maybe not your first
    • 0:47:57program but early on in learning how to program or learning how to program in c
    • 0:48:02you will screw up and you're going to make some typo there's going to be
    • 0:48:06some disconnect between what your understanding is and what you're trying
    • 0:48:08to get the computer to do and this is to say there are tools
    • 0:48:11thankfully that can help you solve those problems and the first of
    • 0:48:16which is called help 50. any of the tools whose names end with 50
    • 0:48:19are specifically educationally oriented written by cs50 staff
    • 0:48:22that are temporary training wheels that we'll use for the first several weeks of
    • 0:48:26the class but then eventually optionally take away
    • 0:48:29in the sense that you won't need them anymore and so help50 is one command
    • 0:48:33that's going to allow you to troubleshoot
    • 0:48:35problems that you might not otherwise see obviously
    • 0:48:39in your own code and let me go ahead and simulate this
    • 0:48:42as follows let me go back to the very first program that we wrote in c
    • 0:48:47which was quite simply this one whereby it only said hello world
    • 0:48:50and there's a few different places i could have screwed up here for instance
    • 0:48:54suppose i was getting a little ahead of myself
    • 0:48:56and i omitted the standard io.h file at the top of my program the implication
    • 0:49:02is that now my computer is not going to know what
    • 0:49:06printf is because it hasn't been included via
    • 0:49:10standardio.h so let's see what the error message is hopefully it'll be a very
    • 0:49:13self-explanatory message that makes perfect sense
    • 0:49:16let me go ahead and recompile this program knowing it already to be
    • 0:49:19incorrect and oh my god like i this i have more
    • 0:49:23lines of errors than i actually have lines of code and this is kind of a
    • 0:49:27neces well this is kind of a reality of
    • 0:49:30programming a lot of programming languages a lot of
    • 0:49:33tools frankly were not designed with ease of use in mind or user friendliness
    • 0:49:37in mind they were really designed with succinctness and precision in mind and
    • 0:49:41they tend unfortunately to assume that the audience is as
    • 0:49:44technical as the person who wrote the program
    • 0:49:47this of course can backfire when you're just learning how to program in the
    • 0:49:50first place and you have to make sense of crazy
    • 0:49:52cryptic output like this today we don't have to focus on every
    • 0:49:55single word that's been output out on the screen
    • 0:49:58but let's start to recognize patterns walking into a new space and just
    • 0:50:01recognizing familiar objects in the physical world
    • 0:50:03let's now do that with code the most important thing perhaps to take notice
    • 0:50:07of is that when you mess up and you make
    • 0:50:10some mistake in your code such that your program doesn't even compile from source
    • 0:50:14code into machine code odds are you're going to see a clue
    • 0:50:17toward the top of the erroneous output that tells you the name of the file
    • 0:50:21where you messed up and the line number where you messed up
    • 0:50:24three implying line three and then five might imply what
    • 0:50:28column or what character in that line but it depends on the particular problem
    • 0:50:32if that's that useful so on line three i am getting an error
    • 0:50:36implicitly declaring library function printf
    • 0:50:39would type in const char star i mean like who knows what that even means
    • 0:50:43you will eventually but for today it just means something bad went wrong and
    • 0:50:48you might not see it you might not know if i hadn't told you
    • 0:50:51that i intentionally deleted that line so let's see if we can't make sense of
    • 0:50:54this by using this tool help 50 is a tool written by cs50 staff
    • 0:50:58that will help translate arcane cryptic computer messages to more human friendly
    • 0:51:05advice and questions that your teaching fellow or teaching assistant
    • 0:51:09might offer you say in the context of office hours so to use help 50
    • 0:51:13instead of running the same command again and again and seeing the same
    • 0:51:16erroneous output literally just write help 50 first at
    • 0:51:20your terminal window then write the same exact command that
    • 0:51:24you're struggling with for whatever reason
    • 0:51:26and hit enter then and what will happen is the same command will get run
    • 0:51:30we will analyze using the help50 program what that output is and we'll try to
    • 0:51:35highlight in yellow the stuff we recognize and then
    • 0:51:37translate it to more human friendly language for instance
    • 0:51:40after running help50 we're asking for help dot dot dot
    • 0:51:43in yellow here is the thing we recognize oh the staff have seen this problem
    • 0:51:47before and then down here did you forget to
    • 0:51:50include standardio.h in which printf is declared atop your
    • 0:51:54file so hopefully if we recognize the problem we can guide
    • 0:51:58you with this sort of rhetorical question that makes you realize oh yes
    • 0:52:01that's what i did wrong so now i can go back up here
    • 0:52:04move to the top of my file and add include
    • 0:52:09standardio.h and now notice if i rerun make hello voila the problem has gone
    • 0:52:15all together and we could do this all day long
    • 0:52:17there's so many places to mess up for instance i omit the semicolon now
    • 0:52:20let me go ahead and make hello now without the semicolon now we're going to
    • 0:52:23get a different error message and you'll see again the name of the
    • 0:52:26file where i messed up hello.c this time it's on line 5 and that's true
    • 0:52:31because the line numbers move down after i added
    • 0:52:33more stuff up there and you can see expected semicolon after expression
    • 0:52:38so this one's a little more straightforward but you could run help
    • 0:52:4050 on this command too just to get back a little more explicit
    • 0:52:44advice so help50 will be your friend anytime you are having trouble getting
    • 0:52:49your code to actually compile well let me do
    • 0:52:53something else that's bad now i've very deliberately been
    • 0:52:57writing fairly pretty code i've indented the word printf
    • 0:53:01i included some blank line up here just to make it clear i've put these curly
    • 0:53:05braces so to speak on their own lines but frankly my computer or cs50 ide
    • 0:53:11is not so particular i could technically get rid of this blank line
    • 0:53:14i could move this curly brace way up here i could get rid of this indentation
    • 0:53:18altogether and move it on the on its own line and then i could just
    • 0:53:21move this curly brace up here thereby writing a program that's now
    • 0:53:25only two lines long not six but hopefully already even if
    • 0:53:29you've never programmed before this should probably rub you the wrong
    • 0:53:33way this is like people in the real world that like don't use punctuation in
    • 0:53:36their social media posts or their emails or their text messages they just kind of
    • 0:53:40go on and on and on and yes the information is there you can
    • 0:53:43glean what it is they're trying to communicate but my god is it annoying
    • 0:53:47it's hard to read there's probably a higher probability that there's a
    • 0:53:50mistake and it's going to be harder to find it because things aren't nicely
    • 0:53:54balanced on the left and on the right and on the
    • 0:53:56top and the bottom so this is what would be
    • 0:53:58described as bad style my program is still correct i've got the standard io.h
    • 0:54:03i've got the semicolon and everything else but it's really bad style because
    • 0:54:06it's just ugly there's not much white space
    • 0:54:09there's not a lot of blank lines or indentations that just make it easier
    • 0:54:12for you and i to read this thing from top to bottom so
    • 0:54:16notice it does compile so help 50 is not going
    • 0:54:20to help me fix this problem because it compiles okay
    • 0:54:23but i it can run another program that we're going to call style 50. this is
    • 0:54:28another educationally oriented tool that's installed in cs50 ide
    • 0:54:31that allows you to figure out how to improve the style of your code
    • 0:54:36so when i run style 50 we'll see some output that at first glance might be a
    • 0:54:39little cryptic but i think it'll help us make sense of
    • 0:54:42the program ultimately to run style 50 you instead run style 50
    • 0:54:47and then just the name of the file in this case hello.c so you don't run
    • 0:54:50make again you don't run make hello you do style 50
    • 0:54:54space hello.c i'm going to go ahead and hit enter
    • 0:54:58and you'll see some somewhat cryptic output here but in green highlights
    • 0:55:02the tool is trying to coax me toward improving
    • 0:55:05this file so notice here backslash n this was noted earlier
    • 0:55:09that it means move the move the cursor to the next line
    • 0:55:14so the fact that this is green here backslash n that means my gosh
    • 0:55:17hit enter after this parenthesis the fact that there's another backslash n
    • 0:55:21here means hey human hit the enter key again to move the
    • 0:55:24curly brace onto its own line the fact that there's four green spaces
    • 0:55:28here means a human literally hit the spacebar four times or hit tab
    • 0:55:32once to indent your code and then finally one more new line here so style
    • 0:55:3750 doesn't fix the code for you stylistically
    • 0:55:39but it tries to draw your attention to all of the possible places
    • 0:55:43where you can improve this so let me take it literally let me add a blank
    • 0:55:46line there let me add a blank line there let me
    • 0:55:49indent by four spaces there and let me move the curly brace down
    • 0:55:53there and now i can re-run style 50 on this prettier
    • 0:55:57code and hopefully it indeed looks good but
    • 0:56:01it's still giving me advice it's telling me to add something called
    • 0:56:04comments and a bunch of you figured this out in the world of scratch
    • 0:56:06you can add little sticky notes or post-it notes to scratch that are sort
    • 0:56:10of notes to self that remind you what something does or maybe explain to
    • 0:56:14your teaching fellow or teaching assistant what
    • 0:56:16something does c supports these as well so for instance if i just wanted to be
    • 0:56:20really pedantic here and make clear to the human reading my
    • 0:56:24code what i'm trying to do i could say something like greet user
    • 0:56:28and notice the syntax here i've put a new line
    • 0:56:32above my existing line of code and i've similarly indented it so everything
    • 0:56:35lines up visually beautifully i've done slash
    • 0:56:38slash which says hey compiler this is a
    • 0:56:41comment this is for human eyes only this is not
    • 0:56:44actual c code per se then i hit the space bar and then i just typed out an
    • 0:56:48english phrase and this could be any spoken language
    • 0:56:51but i went ahead and typed greet user why well it's just a reminder to myself
    • 0:56:55what the purpose of the following line of code is is to greet the user
    • 0:56:58this is marginally better for instance than saying
    • 0:57:03print hello world and let me just ask here even if you've never
    • 0:57:07programmed before why is the first comment
    • 0:57:10of better comments than the second like why should i say
    • 0:57:14if anything greet user instead of print hello world
    • 0:57:18in the form of these comments brian do you want to call on
    • 0:57:22one person's hand here yeah olivia what do you think
    • 0:57:31oh you're unmuted in zoom but can't hear you technically
    • 0:57:36can you hear me now yes okay it tells you the purpose of the code
    • 0:57:41yeah the purpose of the code as opposed to what what distinction are you making
    • 0:57:45and versus telling you exactly what it's doing
    • 0:57:48yeah exactly if your comment is almost identical to the actual code
    • 0:57:52you're not really conveying much more information to the reader let alone
    • 0:57:55yourself in the future explaining it more generally what the
    • 0:57:59purpose of this line of code is to do is to greet the user
    • 0:58:02that's a little more descriptive now to be super fair honestly this program is
    • 0:58:06so short that even though style50 yes would
    • 0:58:08prefer that you add some comments if you're lying if your program really
    • 0:58:12reduces to one line of code you probably don't need a comment now
    • 0:58:14here however pretty much every other program we're going to write here and
    • 0:58:18after is going to be more than just one main line of code like this printf
    • 0:58:22so it's going to make much more sense soon to come that we're going to want to
    • 0:58:26actually print out add to our code some actual comments
    • 0:58:30well let me introduce one final tool here that will help us solve
    • 0:58:33problems as we proceed now to write more sophisticated
    • 0:58:36programs and this is check 50. this is a tool specifically
    • 0:58:39that you'll use either in labs or in problem sets the course's programming
    • 0:58:43assignments to actually check the correctness of
    • 0:58:45your code so whereas help 50 just helps you compile your code
    • 0:58:49typically when it's not compiling at all style 50 helps you improve the style of
    • 0:58:54your code check 50 will check the correctness of your code
    • 0:58:58against some automated tests that we the staff
    • 0:59:01have written that are consistent with whatever the
    • 0:59:04homework problem actually is so we write some tests to
    • 0:59:07make sure that your code is working correctly
    • 0:59:08as per our own specifications so how might i
    • 0:59:12run check 50 this will totally depend on the problem set or the lab
    • 0:59:17and we will always in the problem setter lab tell you what command to type for
    • 0:59:21check 50. it's not something you could necessarily figure out on your own
    • 0:59:25i happen to remember that we have a check that is a test
    • 0:59:28called cs50 slash problems slash hello and this just happens to be a unique
    • 0:59:33identifier even though they're slashes it's not a url
    • 0:59:36it's not the path of a file it's just a unique identifier that we will always
    • 0:59:40tell you in the problem set or lab if i run this
    • 0:59:43command now what's going to happen is the file called hello.c in my ide
    • 0:59:49is being uploaded to github this popular place for storing code i'm going to log
    • 0:59:53in with my student account and then oh none of you can actually see
    • 0:59:58what i'm doing here so we're going to restart this demonstration here
    • 1:00:01because now you can actually see my code so made it
    • 1:00:04almost without a mistake so check 50 is this tool that's going to allow you to
    • 1:00:09test the correctness of your code let me go ahead now and run it as follows i'm
    • 1:00:13going to type check 50 and then cs50 slash problem slash hello
    • 1:00:17odds are you will never run this identical command
    • 1:00:19again in the problem set or lab we will always tell you
    • 1:00:22what to type you won't know what otherwise to type unless we tell you
    • 1:00:26what test to use this is going to now upload my file
    • 1:00:29hello.c to a service called github which again
    • 1:00:33is a popular tool for sharing code we use it to collect submissions for this
    • 1:00:37i'm going to then type in my password you won't see it you'll instead see
    • 1:00:40asterisks or like bullets in a web page i'm going to go ahead and hit enter then
    • 1:00:44it's going to verify my code it's going to do some thinking
    • 1:00:47it's uploading now dot dot dot and now we're just waiting for the internet to
    • 1:00:52respond because somewhere on cs50 servers
    • 1:00:54we are running your code after compiling your code or in this case mine
    • 1:00:59and making sure yes it actually behaved as it should have and what you'll
    • 1:01:04typically see hopefully are a bunch of green smiley faces saying
    • 1:01:07yes that your code exists yes that your code compiles and yes for
    • 1:01:11instance it prints hello world sometimes you might see red frowny faces
    • 1:01:15which means no your code did not work exactly as it should per the lab or per
    • 1:01:19the problem set at which point it's back to the drawing
    • 1:01:22board on your part to figure out exactly what needs to be fixed up here
    • 1:01:26sometimes you'll see yellow output with just a straight yellow face which just
    • 1:01:29means we weren't even able to run a certain test because some other test
    • 1:01:33already failed so it's meant to be relatively
    • 1:01:36quick feedback on the correctness of your code before you even submit it
    • 1:01:40and call it a day and check 50 instructions will always be accompanied
    • 1:01:45by the problem itself in the lab or problem set so some final commands
    • 1:01:51here now within this terminal window i can do
    • 1:01:54more than just run make and dot slash hello or whatever my
    • 1:01:58program's name is and i can do more than help 50 and style
    • 1:02:0250 and check 50. it turns out that i'm really
    • 1:02:05using in the form of cs50 ide my own server in the cloud so yes i'm
    • 1:02:09using a website but what cs50 ide really is it's like
    • 1:02:13your own server or your own computer in the cloud
    • 1:02:16somewhere out there on the internet you have your own username and password
    • 1:02:20in the form of cs50 ide and only you can access the files that
    • 1:02:25you write the programs that you write that are
    • 1:02:27stored in this ide and there's a few more features i'll now
    • 1:02:30draw our attention to perhaps the most friendly one is this little folder icon
    • 1:02:34at top left if i click this little folder icon
    • 1:02:37you'll now see what's generally called a file browser or a
    • 1:02:40file tree which is just like a graphical representation of the files
    • 1:02:44in my account or in my ide in this case looks similar to mac os looks similar to
    • 1:02:49windows and this is just a graphical user interface built into the ide
    • 1:02:53so that for instance if i close my tab by clicking this little x button up here
    • 1:02:58and i want to reopen the file much like you would imagine on a mac or pc
    • 1:03:02it's as simple as double-clicking the file on the left-hand side
    • 1:03:05but notice i didn't click on hello because notice what happens if i open
    • 1:03:10hello my gosh like what is going on here
    • 1:03:15this is kind of a mess there's redness there's dots
    • 1:03:18any thoughts from someone on why i'm seeing what i'm seeing because odds
    • 1:03:25are you will accidentally at some point click on a file like hello
    • 1:03:30instead of on a file like hello.c sarah what do you think
    • 1:03:38it is the binary code so it's the machine language so it doesn't allow the
    • 1:03:43user to see them besides the code they write and see
    • 1:03:46yeah exactly what you're trying to look at in this tab
    • 1:03:49is binary codes zeros and ones however those zeros and ones are technically
    • 1:03:54being misinterpreted at the moment as ascii characters or unicode
    • 1:03:59characters so recall from last week ascii is this mapping between numbers
    • 1:04:03and letters and numbers of course are just patterns
    • 1:04:06of zeros and ones and this looks super cryptic because we're trying to
    • 1:04:10misinterpret zeros and ones as though they're ascii characters and recall that
    • 1:04:15there's many more characters in ascii and
    • 1:04:17unicode than a through z and the numbers there's some
    • 1:04:21unprintable characters and indeed all the funkiness we're
    • 1:04:23seeing here is just a misinterpretation of zeros and ones
    • 1:04:27that are instructions to the computer machine code
    • 1:04:31for the computer being misinterpreted as text so you can't edit a binary file
    • 1:04:35like this so to speak you should just close hello when you do
    • 1:04:38something like that and make sure you've double clicked on and opened
    • 1:04:41your actual source code file as well so we've seen strings and there's other
    • 1:04:47data types and there's other functions and there's loops and conditions and so
    • 1:04:51much more i think we're at a good point now to
    • 1:04:53perhaps take a break let this sink in why don't we go ahead and take a seven
    • 1:04:57minute break and when we resume we'll introduce a few
    • 1:05:00more features of c and compare them against what we saw last week
    • 1:05:03in scratch so we'll see you in seven minutes
    • 1:12:31all right we are back so recall where we left off was we were looking at this
    • 1:12:37graphical user interface the top left the file browser the file tree that just
    • 1:12:41gives us more graphical access to the files in our account but
    • 1:12:44let's now do this the the old school command
    • 1:12:48line way in my terminal window so it turns out that using our terminal window
    • 1:12:53can we not only compile code and run code and run check 50 style 50 help 50
    • 1:12:57and the like we can also manipulate files and folders
    • 1:13:00even that happen to exist in my ide that is in the computer i have
    • 1:13:05access to here in the cloud and the first command i'll propose is
    • 1:13:09that we type ls ls is shorthand notation for list and
    • 1:13:13quite simply ls lists the files or folders in your
    • 1:13:17current folder so this would be like double clicking on
    • 1:13:19your my documents folder in windows or documents in mac os
    • 1:13:23ls just lists the contents now notice hello is a little weird it's highlighted
    • 1:13:27in green and there's an asterisk and there's an asterisk after it and
    • 1:13:31that's just a visual cue that that file is executable that is
    • 1:13:35that is a program that you can run with dot slash
    • 1:13:38hello the star is not part of the file name and of course we see hello.c
    • 1:13:42now suppose that i wanted to maybe rename
    • 1:13:46my file well i could much like in mac os or windows i could go up to the file
    • 1:13:50browser up here i could control click or right click and
    • 1:13:53notice there's a whole bunch of menu options that pop up just like on your
    • 1:13:56own computer and i could rename the file right up
    • 1:13:58here but generally speaking we're going to do things at the command line only
    • 1:14:02because rudimentary as some of these operations today
    • 1:14:04are it's going to be a much more powerful command line interface for me
    • 1:14:08so suppose i change my mind and you know what i i don't like this
    • 1:14:11version of hello i want to delete that program
    • 1:14:14and start over strictly speaking i don't need to delete hello ever i can just
    • 1:14:17recompile it and it will keep getting changed and changed but if i do want to
    • 1:14:21remove it i can type rm hello and then hit enter and then i'll
    • 1:14:25be asked remove regular file hello that's just a visual confirmation that i
    • 1:14:30do indeed want to delete and i can type y
    • 1:14:32or yes or some such reply and if i hit y and enter nothing seems to happen but
    • 1:14:38notice what happened up here at top left notice that hello is now gone
    • 1:14:43leaving only hello.c and if i type ls again now i see only my
    • 1:14:48code file maybe now i want to change this program
    • 1:14:50and i don't want to write hello.c but goodbye.c
    • 1:14:54well let me close the tab up there and yes i could go and right click or
    • 1:14:57control click on it but again we don't need to use the graphical interface
    • 1:15:01let me go ahead and instead do mv hello.c
    • 1:15:06goodbye.c mv is the move command and even though it would be nice if it's
    • 1:15:10called rename instead of move move just moves one file to another
    • 1:15:15location or to another name so if i do move
    • 1:15:17hello.c goodbye.c notice what happened at top left
    • 1:15:22now my same file is called goodbye.c and if again i type
    • 1:15:25ls i can see that it's indeed uh renamed now let me go ahead and move
    • 1:15:30that back because i want to stay on my hello.c program but suppose i want
    • 1:15:34to start organizing my files in a moment we're going to start writing more
    • 1:15:37programs and so my account is going to get a
    • 1:15:40little bit messy with more and more files over the course of today
    • 1:15:43so suppose you want to create a folder otherwise known as a directory
    • 1:15:47i'm going to go ahead and type mkdir for make
    • 1:15:50directory and then the name of the directory i want to make for instance
    • 1:15:54lecture you could call it anything you want but if i'm in lecture i'm writing
    • 1:15:57code maybe i want to store all of today's
    • 1:15:58files in a lecture directory when i hit enter there notice what
    • 1:16:02happens in my file tree up here i now see a lecture folder if i click
    • 1:16:07the triangle it's empty because i haven't put
    • 1:16:09anything in it so let me go ahead and move
    • 1:16:12hello.c into the lecture folder mv hello.c lecture and now let me hit enter
    • 1:16:19and voila now notice that it's nested inside of this lecture folder
    • 1:16:24and indeed if i now type ls for list i only see the lecture folder
    • 1:16:28unfortunately i'm kind of now don't have access to hello.c
    • 1:16:32within this command line environment unless i change into that directory now
    • 1:16:35in the world of macs and pcs we obviously would just double click on
    • 1:16:39a folder and voila we're inside of it in a command line interface you need to
    • 1:16:42be more deliberate so i'm going to do cd for change directory then lecture and
    • 1:16:48then i'm going to go ahead and hit enter and now notice and now it might make
    • 1:16:51more sense why this whole time we've been seeing in blue
    • 1:16:55this tilde lecture slash the tilde just means my so-called home directory like
    • 1:17:00my own account my own default folder like
    • 1:17:02my documents and windows or documents on mac os
    • 1:17:05that's what tilde represents in shorthand notation lecture is the name
    • 1:17:09of the folder that i am now inside so it's as though i
    • 1:17:13double-clicked on lecture in mac os or windows to open a folder
    • 1:17:17now i'm inside this lecture directory in my terminal window
    • 1:17:21so if i now type ls enter i should see voila
    • 1:17:25the hello.c file that was in front that was uh
    • 1:17:28that i moved into it now let me undo this because i'm going to go ahead and
    • 1:17:32keep things a little simpler for now and suppose that i want to move hello.c
    • 1:17:37into where it previously was last piece of syntax
    • 1:17:40there's this shorthand notation for what we'll call a parent folder
    • 1:17:43so just like in family trees there's the notion of parents and children
    • 1:17:47and grandchildren and so forth that's also true on computer systems that have
    • 1:17:50folders and folders inside of folders and folders inside of folders inside of
    • 1:17:54folders there's a hierarchy there much like a
    • 1:17:57family tree so if i want to move hello.c one level up i can actually do mv
    • 1:18:03hello dot c space dot dot and that's like saying
    • 1:18:07move this file to the folder up above when i do that
    • 1:18:11notice what happened at top left now hello.c
    • 1:18:15is not inside of the lecture folder but below it and indeed if i type ls
    • 1:18:19now in the lecture folder there's nothing there how do i move myself
    • 1:18:23up one level in the family tree that are these folders
    • 1:18:27let me go ahead and type cd space dot dot
    • 1:18:30so change directory to my parent dot dot just means your parent the folder up
    • 1:18:34above enter and now i'm apparently in just
    • 1:18:38tilda slash which is again cryptic shorthand notation for your own
    • 1:18:42home directory your my documents or documents folder and if lastly i type
    • 1:18:47ls here i'm done with this lecture folder i
    • 1:18:49don't want to bother storing things in a folder
    • 1:18:52i can do not rm for remove like uh i did to get rid of hello but rmdir
    • 1:18:58to remove a directory and voila it's gone
    • 1:19:01and i've undone all of the various changes that i made earlier
    • 1:19:05but perhaps now it makes a little more sense why i was doing something earlier
    • 1:19:09let me open up my hello.c file let me make hello again which is way
    • 1:19:13back where we left off and recall that all this time i've been
    • 1:19:17doing dot slash hello well why is that well just as dot dot
    • 1:19:22refers to your parent directory a single dot
    • 1:19:25refers to your current directory so even though this looks a little silly
    • 1:19:29dot slash hello is just a very explicit way
    • 1:19:33of telling the computer run the program called hello
    • 1:19:36that's right here in my current directory dot means current directory
    • 1:19:41dot dot means parent directory and so there we see finally
    • 1:19:45why i've been typing dot slash hello all this time but again it's just the
    • 1:19:49textual analog of doing something like uh double
    • 1:19:53clicking on an icon in mac os or windows so there's other commands too
    • 1:19:57and over time you'll get exposed to these and use them for various problems
    • 1:20:01cp for copy for instance uh is yet another and many others but these are
    • 1:20:06all just standard commands they are not cs50 specific
    • 1:20:09standard commands that allow us to manipulate files and folders
    • 1:20:14in a computer like this and question from max
    • 1:20:18uh hi yeah sorry i was just wondering i don't really understand the difference
    • 1:20:21between the hello program and the hello.c program it seems
    • 1:20:24like the one that doesn't have c on it isn't used for anything or oh it
    • 1:20:28is though so recall that we have two things
    • 1:20:31in the story we have source code which is the c code i've been writing
    • 1:20:34and then machine code which is the zeros and ones that the computer understands
    • 1:20:38i have been writing all of my code in the file called hello.c
    • 1:20:42but after i compile it with make the make
    • 1:20:45program creates a new file called hello that technically contains only zeros and
    • 1:20:50ones and that is the machine code that i'm actually running
    • 1:20:54when i do dot slash hello so again i can use
    • 1:20:57rm and i can get rid of the hello program just like i did before
    • 1:21:01and now we're back at the very beginning of the story where we wrote this code
    • 1:21:03from scratch if i now type make and let me do this
    • 1:21:07now ls notice i've only got one file let me now do make hello i see that
    • 1:21:12cryptic output but if i type ls again now i have two files and that's because
    • 1:21:16only the green one with the asterisk is
    • 1:21:19executable that is the machine code that the compiler
    • 1:21:22has created for me and i should say and disclaim i've been telling a little bit
    • 1:21:26of a white lie today make itself is not actually a compiler
    • 1:21:30we'll see next week exactly what make is doing
    • 1:21:32but it's making it easier for us to actually compile our code
    • 1:21:36but more on that next time all right so we've seen only strings thus far but
    • 1:21:42it turns out in c and in a lot of languages there's other things known as
    • 1:21:46types or data types that is to say you can have variables and values
    • 1:21:50that aren't just strings of text but that are maybe integers like numbers one
    • 1:21:54two three four or maybe floating point values like
    • 1:21:583.14159 or other such values you can have boolean values which are
    • 1:22:02only true or false you can have characters or
    • 1:22:06chars which are single characters this is to
    • 1:22:08say within a language like c there's actually a whole bunch of
    • 1:22:11data types that are available to you string is only one of them and there's
    • 1:22:16even more than are on this list here but this is just a list of some of the
    • 1:22:20most common ones that we'll see today and will use this coming week in the
    • 1:22:23first problem set that allow you to tell the computer not
    • 1:22:26only to store a value in a variable but what type of value to store in a
    • 1:22:30variable moreover we have in the cs50 library a whole
    • 1:22:34bunch more functions we've seen getstring already
    • 1:22:37but similarly have we created functions that you can use for problem sets labs
    • 1:22:42and beyond that allow you to get a single character
    • 1:22:44by a getchar that can allow you to get an integer via
    • 1:22:48getint that can allow you to get a floating point value which is a fancy
    • 1:22:52way of describing a real number with a decimal point
    • 1:22:55using get float but it turns out that each of these data types
    • 1:22:59like int and float only have a finite number of bits
    • 1:23:04and recall from last week that we played around with light bulbs and we played
    • 1:23:07around with bits and zeros and ones more generally it turns out that
    • 1:23:10every one of these data types char double
    • 1:23:14uh float int long string and so forth all use a specific number of bits and it
    • 1:23:20turns out that ins for instance integers in c only
    • 1:23:24use 32 bits and that's great until such time
    • 1:23:28as you want to count higher than roughly 4 billion at which point you can't we'll
    • 1:23:33see later today that if you're only using a specific number
    • 1:23:36of bits you can only count so high and so there exist other data types for
    • 1:23:40instance a long a long is another type of number in c
    • 1:23:43that just uses 64 bits so it gives you way more expressiveness way more
    • 1:23:48patterns of zeros and ones to count even higher
    • 1:23:50similarly a double is like a floating point value a real number with a decimal
    • 1:23:54point and some number of digits after it a double allows you to have even more
    • 1:23:58digits after it then a float would so we'll see
    • 1:24:02and use some of these data types in just a bit printf similarly has the
    • 1:24:06ability to print out not only strings as we saw
    • 1:24:09but also using different format codes other data types as well these are a
    • 1:24:12little more cryptic and it's fine to look these things up as needed but
    • 1:24:15you'll eventually ingrain them for common use
    • 1:24:17cases percent c is going to be the placeholder
    • 1:24:21for printing a single character percent c for a char
    • 1:24:23so to speak percent f is going to be for a floating point value so if you want to
    • 1:24:27print out a real number with a decimal point you're going to use percent f
    • 1:24:31if you want to print an integer using printf you're going to use percent i for
    • 1:24:34integer if you want to print a string we've
    • 1:24:36already seen percent s and if you want to print a long
    • 1:24:39integer aka long you're going to use percent li
    • 1:24:43and there's even others but these are perhaps some of the most common and it
    • 1:24:46just means that again c really needs you the programmer to be
    • 1:24:50precise you can't just say print this you have to tell printf how to print the
    • 1:24:55variable or the value that you're passing into it and then
    • 1:24:58lastly it turns out that in c there's a whole bunch of operators
    • 1:25:03certainly mathematical ones and bunches of others as well just like scratch had
    • 1:25:06a whole toolkit of operators and suffice it to say for
    • 1:25:10now that c also supports addition subtraction
    • 1:25:12multiplication division and even the remainder operator which a
    • 1:25:16little cryptically is represented with a percent sign
    • 1:25:19not to be confused with printf's format codes but this is to say that some of
    • 1:25:23the earliest uses of computers of course were all mathematically oriented in
    • 1:25:26spreadsheet programs programs like visit calc and the like
    • 1:25:29back before there was excel in google spreadsheets and they certainly
    • 1:25:32computers are very good at supporting math and so these are just
    • 1:25:36some of the operators that we'll now see are available to us so let me go ahead
    • 1:25:40and do this let me go back to my ide after cleaning things up
    • 1:25:45and starting fresh with just nothing in my terminal window and no tabs open
    • 1:25:50and let me go ahead and write my next program this time using some more of
    • 1:25:53these functions i'm going to go ahead and create a file
    • 1:25:56up here called edition.c so edition.c but i could call
    • 1:26:01this anything i want but it's important to add the dot c
    • 1:26:04otherwise the computer will not know that it's actual source code as opposed
    • 1:26:07to machine code and let me go ahead and make use of the
    • 1:26:10cs50 library so let me include cs50.h let me include standardio.h so that i
    • 1:26:16can use things like getint and printf and then again for
    • 1:26:20today's purposes i'm just going to do in main intmane void and then the
    • 1:26:25curly braces and again for today just take on faith this is necessary but
    • 1:26:29we'll explain within a week or two exactly why we keep writing int main
    • 1:26:33void but for now it's like the when green flag clicked
    • 1:26:36puzzle piece let me go ahead now and get an integer from the user suppose my goal
    • 1:26:41now is not to write a program that gets a string of text and prints out hello
    • 1:26:44brian or hello david let me go ahead and write a program that
    • 1:26:48maybe asks for two integers two numbers and then just adds them together so let
    • 1:26:52me make the simplest of calculators using code
    • 1:26:55well i'm gonna go ahead and declare a variable
    • 1:26:58called x just like a mathematician would and i'm gonna assign it the value
    • 1:27:03of calling get int and i'll just say something like
    • 1:27:07x colon i could say anything i want what is x question mark but i'm going to keep
    • 1:27:11it simple and just say x colon semicolon to end my thought so again
    • 1:27:15similar in spirit to what i did with string before
    • 1:27:18but now i'm using get int to get a number or an integer from the user
    • 1:27:22the quoted parameter here or argument is the input to print to getint which is
    • 1:27:28going to be the prompt that the human sees
    • 1:27:30the equal sign recall is the assignment operator which says
    • 1:27:34copy the return value on the right the integer that the human hopefully
    • 1:27:39will type in over to the left and the left says give me a variable
    • 1:27:44called x and let me store integers in it
    • 1:27:48so before we used string on the left and we used get string on the right the only
    • 1:27:52difference now is int on the left because i want a number
    • 1:27:55and get int on the right then let me go ahead and do this again
    • 1:27:59and get another number get int and i'll just say y colon but
    • 1:28:02again i could say what is y question mark or anything in english but
    • 1:28:06the last line is going to be the interesting one
    • 1:28:08now i'm going to go ahead and print out for instance the sum of these two
    • 1:28:12numbers but printf again takes an input that
    • 1:28:16tells it what to print out exactly so i can't
    • 1:28:19really type a number here yet because i don't know what the human is going to
    • 1:28:22type in so i'm going to put a placeholder i'm
    • 1:28:25going to put percent i which says put a number here i just
    • 1:28:28don't know yet what it is and then just to keep things clean i'm
    • 1:28:31going to do backslash n which just says give me a new line also that's just an
    • 1:28:35aesthetic detail to move the cursor to the next line just to keep things
    • 1:28:38clean but now printf is going to take a second argument
    • 1:28:42and whether you've programmed or not before brian let's go to someone in the
    • 1:28:46audience if we could what should i type after the comma if
    • 1:28:49the purpose of this program is quite simply
    • 1:28:52to add two numbers together even if you've never programmed before
    • 1:28:55based on the operators that exist and some of the syntax we've seen thus far
    • 1:28:59what would your instincts have you type here even if you've never
    • 1:29:02done this before santiago what do you think
    • 1:29:08um i would say to just write x plus y yeah it is as simple and as
    • 1:29:13straightforward as that x plus y is the right intuition i'm going to add a
    • 1:29:17semicolon at the very end just to finish my thought but indeed computers
    • 1:29:21and c in this case absolutely understand arithmetic and mathematical operations
    • 1:29:24so just type what you mean i'm going to go ahead now and save the
    • 1:29:28file and go down below and i'm not going to type make hello anymore now i want to
    • 1:29:31type make addition because that is the name
    • 1:29:34of my file implicitly edition.c i want to compile into a
    • 1:29:38program called edition hopefully this is where i cross my
    • 1:29:41fingers i didn't make any mistakes and i'm going to go ahead and run make
    • 1:29:45addition all is well no error messages if i had
    • 1:29:49made a mistake and it didn't even compile
    • 1:29:51help 50 might have been my next instinct now i'm going to go ahead and run dot
    • 1:29:54slash edition and notice i'm first prompted for x i'm
    • 1:29:58going to go ahead and do one i'm next prompted for y i'm going to go
    • 1:30:01ahead and do one again and voila santiago proposed i indeed see
    • 1:30:05on the screen x plus y or the value 2. and i didn't
    • 1:30:09hard code 2 i substitute it in using percent i
    • 1:30:12whatever the result of x plus y actually is now notice
    • 1:30:17some of the features of the get in function for you suppose that you're not
    • 1:30:20being very cooperative and you type in cat for x notice that get in just
    • 1:30:24ignores you and prompts you again if i type in dog it ignores me and
    • 1:30:29prompts me again if i type in 1.23 it ignores me
    • 1:30:32and prompts me again because it wants an integer in this case a number like one
    • 1:30:37two three four or negative one two three four or zero
    • 1:30:39or anything above or below so fine i'll cooperate now and give it the number one
    • 1:30:44same for y it's going to ignore any non-integer input so if i give it a
    • 1:30:47number like 2 this time i'll hopefully get the answer of
    • 1:30:503. all right so we have a basic calculator in c
    • 1:30:54we're using some basic building blocks as before we've got these header files
    • 1:30:57which just give me access to getint and printf respectively but
    • 1:31:02suppose now i want to count up even higher you know what let me try
    • 1:31:05something like this let me run this program once more
    • 1:31:07and let me get a little greedy how about four zero zero zero
    • 1:31:11zero zero zero zero zero zero so roughly four well exactly
    • 1:31:16four billion uh that's the number i want to type in
    • 1:31:19notice that x does not like that so get in does not accept four billion well let
    • 1:31:25me try maybe three billion
    • 1:31:29uh-huh didn't like that how about 2 billion
    • 1:31:32okay that one worked let me pause here what's going on perhaps now again we
    • 1:31:39the staff wrote get in so we are the ones that are rejecting
    • 1:31:42cats and rejecting dogs and rejecting 4 billion and even 3 billion
    • 1:31:47but in this case it's a little less clear why did we reject 4 billion in 3
    • 1:31:52billion do you think based on some of the definitions thus
    • 1:31:55far today why might this be nathaniel what do you
    • 1:31:59think
    • 1:32:08hi um i just got unmuted uh there's like a cap on like the size of the number
    • 1:32:12since um it would take too many bits and bytes uh after the size of two billion
    • 1:32:17perfect so integers again are implemented in c as these things called
    • 1:32:21ants ants only use it turns out 32 bits total
    • 1:32:25and you you would only know that by having been taught it or looked it up
    • 1:32:27for a particular computer system but they on cs50 ide and most modern
    • 1:32:31systems an integer is only 32 bits and that then invites
    • 1:32:35the question well if you've got 32 bits or light bulbs how high can you count
    • 1:32:38well it turns out with 32 light bulbs or bits you can count
    • 1:32:42roughly as high as 4 billion you can absolutely count as high as 3 billion
    • 1:32:47and yet get into still rejecting it but that's because the get in function
    • 1:32:51supports integers broadly speaking which includes not only
    • 1:32:55positive numbers but also negative numbers
    • 1:32:58and zero and the catch is that if you want to support both positive numbers
    • 1:33:02and negative numbers you can represent four billion or so
    • 1:33:06total possible values but if you want to go as far to the left
    • 1:33:10and as far to the right on the number line that i'm describing
    • 1:33:13you can only really count as high as 2 billion in the positive direction
    • 1:33:16and negative 2 billion in the negative direction because that still gives you a
    • 1:33:20total of 4 billion but not nearly as high as 3 billion or 4
    • 1:33:24billion so what might the solution here be
    • 1:33:26well i recall earlier noting that there's other data types not just ins
    • 1:33:30and strings but also longs which literally are longer
    • 1:33:33integers namely 64 bits so let me go ahead and try this
    • 1:33:37let me go ahead and change get in to get long this get in
    • 1:33:40to get long let me change this in to a long
    • 1:33:43and this into a long so same program same calculator but i'm now using a
    • 1:33:47different data type that's just going to use more bits to
    • 1:33:50store values let me run make addition again to recompile my
    • 1:33:53program and oh damn it i screwed up so let's see if we
    • 1:33:57can't glean what's wrong here let me scroll up and i can't emphasize
    • 1:34:01this enough sometimes i got lucky here and i only have one mistake
    • 1:34:04apparently in the error messages it is not going to be
    • 1:34:08uncommon for you to have two errors 10 errors
    • 1:34:12in like two lines of code this is because sometimes when you have errors
    • 1:34:16in your code the compiler sometimes just gets
    • 1:34:18confused and if it gets sufficiently confused it starts thinking
    • 1:34:21everything is an error in your actual code so the most important takeaway is
    • 1:34:25that no matter how many errors you seem to have
    • 1:34:28always scroll up to the top of the output and address the first error first
    • 1:34:33so that's why i scrolled up in my window to look immediately below what i typed
    • 1:34:36make addition and here's the first error edition.c on line 10. all right i can't
    • 1:34:40see line 10 so let me scroll my code up and it saying something about format
    • 1:34:44specifies type int but the argument has type long we
    • 1:34:48haven't seen this error before but i think i can infer from this it's not
    • 1:34:52super cryptic even though it's unfamiliar i think what this means is
    • 1:34:56that percent i recall was for integers i
    • 1:34:59think what i need is a different format code for long
    • 1:35:03integers which is going to be l i instead and that was from my little
    • 1:35:07cheat sheet earlier so let me go ahead and try this again
    • 1:35:10make addition after changing the i to an li
    • 1:35:12that indeed works now let me do uh whoops typo
    • 1:35:15now let me do dot slash addition and now i'll type in four
    • 1:35:19zero zero zero zero zero zero zero zero zero four billion
    • 1:35:23now get long is happy and it will accept such a long
    • 1:35:28integer because it has enough bits all right questions on types
    • 1:35:35like ins and longs or functions like get int
    • 1:35:39or get long yeah uh peter
    • 1:35:45yeah uh when i typed uh two billion and both were integers uh
    • 1:35:51well eventually it just gives you the wrong answer
    • 1:35:54some negative number is that because of the bits and bytes indeed it's the same
    • 1:35:58answer so i didn't demonstrate that but if you inputted both 2 billion for x
    • 1:36:02and 2 billion for y and then you tried to add those together
    • 1:36:06that would require that would give you mathematically 4 billion but again
    • 1:36:10an int is not big enough to store 4 billion
    • 1:36:14if we want to also be able to represent negative numbers so peter what you're
    • 1:36:18seeing is that you can't fit the result
    • 1:36:21in the data type allowed and we'll see in a moment uh in a little bit today
    • 1:36:25actually what the ramifications of that are but the symptom you're describing is
    • 1:36:29exactly that you tried to cram too big of a number
    • 1:36:32into finitely many bits 32 you can avoid that problem though of
    • 1:36:36course by switching over to long let me try one other thing
    • 1:36:39that's a bit curious let me go ahead and write a slightly different program now
    • 1:36:43and i'm going to describe this as truncation.c fancy term but we'll see
    • 1:36:48what this means in just a moment i'm going to give myself at the top
    • 1:36:51cs50.h and i'm going to give myself standardio.h
    • 1:36:54and it's certainly fine once you get started with the first lab or problem
    • 1:36:57set if it takes you much longer to type some of these things out
    • 1:37:01i'm just doing it for muscle memory int main void
    • 1:37:04and now we're good to go with a new program in a file called truncation.c
    • 1:37:08i'm going to go ahead and prompt the user for an int again so
    • 1:37:11just like before i'm going to prompt the user for another int
    • 1:37:15just like before and then i'm going to go ahead and
    • 1:37:19do this i want to do division this time so not just addition that was a little
    • 1:37:22too easy let me do division so let me give myself another variable z
    • 1:37:26equals x divided by y and let me pause here for
    • 1:37:31a moment and just ask the question what data type should i perhaps use for
    • 1:37:35z this line of code is not yet correct
    • 1:37:37because recall that anytime you create a new variable
    • 1:37:40on the left here i'm going to need to put something
    • 1:37:44to the left of that variable's name so that c knows what type of variable i
    • 1:37:48want and thus far we've seen string and and long so would you propose we use one
    • 1:37:53of those or something else for z
    • 1:37:58what should the data type be for z would you say
    • 1:38:04data type for z how about jack what do you think
    • 1:38:12uh would it be a float yeah so float so float which is short for floating point
    • 1:38:17value which is the programmer's way of
    • 1:38:19describing a real number let me go ahead and do that a float and
    • 1:38:22uh i'm guessing uh your instincts for float were that well if you
    • 1:38:26add in type in one number for x and another for y and the result is a
    • 1:38:29fraction of some sort so something with a decimal point we
    • 1:38:32need to store it in a float so that we can actually
    • 1:38:35uh store whatever the numbers are after the decimal point so let's go ahead and
    • 1:38:40do this let me now go ahead and print this out
    • 1:38:43percent f backslash n because i'm printing a float
    • 1:38:46this time and then let me go ahead and print out
    • 1:38:48the value of z and you know what just for good measure let me start practicing
    • 1:38:52good style here too so get a number from user let me give
    • 1:38:57myself another comment get another number from user or you know
    • 1:39:00what this seems a little silly i can combine these lines and why don't
    • 1:39:03i just say get for instance numbers from user that's a reasonable way to comment
    • 1:39:08your code and then let's just go ahead and divide
    • 1:39:11x by y but even this is getting a little
    • 1:39:14pedantic because you can kind of read that from the code so at some point we
    • 1:39:17might not even need a comment for that so let's just simplify as such let's go
    • 1:39:22ahead now and compile this make come on make truncation
    • 1:39:29all right it compiles okay and i like how we used a float here that does feel
    • 1:39:33correct so let me run truncation and let me go ahead and type in for
    • 1:39:37instance 4 for x and 2 for y okay i like that it's 2.000
    • 1:39:42so that's the correct math calculation how about 1 divided by 2. so
    • 1:39:46x is 1 y is 2 and it's 0.0000
    • 1:39:53all right well maybe that was just a fluke let me try running it again how
    • 1:39:56about uh how about two thirds two divided by
    • 1:40:00three huh that's not right either all right how about four thirds
    • 1:40:06let's put a bigger number for the four for the x so
    • 1:40:08four thirds okay it's closer to right but this is a example this week
    • 1:40:15of a bug so my code compiles so syntactically
    • 1:40:19it's fine but this is a logical bug like i've
    • 1:40:21somehow used c code improperly so what might be going on
    • 1:40:26here y is one divided by two and two divided by three both apparently
    • 1:40:31zero followed by zero six zeroes after the decimal point and even four
    • 1:40:36thirds gives me 1.00000 instead of 1.33333
    • 1:40:43nina what do you think
    • 1:40:49because with and they don't like recognize like decimals so
    • 1:40:53like so fourth four thirds question three only goes into four one
    • 1:40:57time so it returns a one and you need to use like
    • 1:41:00other types of character like a float or a double
    • 1:41:04if you want like the actual decimal yeah exactly this one's more subtle than the
    • 1:41:08mistakes i've made before but c like most programming languages is
    • 1:41:12going to take you literally so if on the right hand side of this
    • 1:41:15expression on line 11 i am literally doing x divided by y
    • 1:41:20you first have to ask yourself well what is the type of x
    • 1:41:23what is the type of y if they are both ants
    • 1:41:27by definition of how c works you are going to get back
    • 1:41:30an integer as your answer so if you do 1 divided by 2
    • 1:41:34yes mathematically that's 0.5 0 0 0 0 however if you convert that to
    • 1:41:41an int insofar as x and y are ins the way c
    • 1:41:45works is it truncates everything after the
    • 1:41:47decimal point so it does the math correctly
    • 1:41:50but because you cannot fit floating point values you cannot fit decimal
    • 1:41:54points and numbers thereafter in an integer you
    • 1:41:57get you lose all of those digits after the decimal
    • 1:42:00point because you can only fit the integer part of the answer into an
    • 1:42:05integer it's not relevant that i'm saving the
    • 1:42:09results ultimately in a float because that's too late
    • 1:42:11the math has already been done on the right hand side and so yes i'm storing
    • 1:42:16an integer in a float so i can print it as a float but it's too late everything
    • 1:42:20after the decimal point has already been thrown
    • 1:42:22away so what are the implications of this or how could i fix well
    • 1:42:25i could go through and just change all of this right like well if the problem
    • 1:42:29is that x and y are ints well let me just change them to floats and change
    • 1:42:32this up here change x to a float change y to a float
    • 1:42:36and so forth that would fix the problem but that's
    • 1:42:39kind of a heavy-handed solution to this problem go through and change all of
    • 1:42:42your code you can instead be a little more clever
    • 1:42:45and you can convince the computer to convert an integer to a
    • 1:42:49float by something known as casting so i can
    • 1:42:52actually go in here and using a new syntax i can say float y
    • 1:42:56and i can even for good measure but it's not strictly necessary also do it to x
    • 1:43:00you can and c cast or type cast one data type to another by
    • 1:43:06literally in parentheses just putting the new data type that you want
    • 1:43:09and if it makes mathematical sense for one to be converted
    • 1:43:13into the other the computer will do it for you so in this way
    • 1:43:17i'm telling the computer convert x to a float convert
    • 1:43:20y to a float then do the math and so before when i typed in 1
    • 1:43:24and 2 respectively for x and y now it's kind of like i typed in 1.0
    • 1:43:30and 2.0 and 1.0 divided by 2.0 is going to be mathematically 0.5
    • 1:43:37but because x and y now were converted in advanced to floats
    • 1:43:41the answer is going to remain a float 0.5
    • 1:43:45and that's what's going to get stored in z and ultimately printed
    • 1:43:48so if i rerun truncation having now fixed this problem
    • 1:43:52let me do dot slash truncation type in 1 type in 2. i don't have to type the
    • 1:43:57point 0 myself the computer is doing that for me but via these
    • 1:44:00casts in parentheses now i see that the answer is indeed
    • 1:44:040.5 all right so we seem to have now some very basic
    • 1:44:09low-level control over what you can do with the program
    • 1:44:12let's now add back all of the fancy features that we had from scratch last
    • 1:44:16week so we can start making more interesting programs so variables
    • 1:44:20in another term of art called syntactic sugar
    • 1:44:22is also amongst these features here so recall from last week
    • 1:44:26when we wanted to have a variable called counter set equal to zero
    • 1:44:29we can go ahead and define it like this in c starting today
    • 1:44:33we're going to instead say something like counter equals zero
    • 1:44:36but we additionally need to specify the data type of that variable
    • 1:44:40and we need to end our thought with a semicolon so whereas we said set counter
    • 1:44:44to zero like this last week now it's going to translate quite simply
    • 1:44:48to this on the right hand side well what comes after that well if we
    • 1:44:51wanted to increment a counter variable last week by one
    • 1:44:54adding one to it we used quite simply this puzzle piece this week we need to
    • 1:44:58be a little more explicit and say something like this
    • 1:45:01counter equals counter plus one and semicolon to finish the thought now
    • 1:45:06this might seem very mathematically paradoxical like how can
    • 1:45:10counter equal counter plus one like that just
    • 1:45:13doesn't work logically but that's not the equal
    • 1:45:17sign in this case in c as with other languages we'll
    • 1:45:20encounter the equal sign is the assignment operator from right
    • 1:45:24to left so this is saying take counter plus 1
    • 1:45:27and store that mathematical result on the left
    • 1:45:30so whatever counter is add one store the result
    • 1:45:34in counter thereafter effectively increasing its total by one
    • 1:45:38now this is a very common operation in programs we'll see over the term where
    • 1:45:41you just want to add something up because you want to keep track
    • 1:45:44of the count of something so it turns out there's some syntactic
    • 1:45:47sugar which means there's a different way of doing this syntactically
    • 1:45:50that doesn't give you any new capabilities that you didn't already
    • 1:45:53have in c it just makes it marginally more
    • 1:45:56pleasant or quicker to type so this line of code in c is identical
    • 1:46:00to saying this line of code in c counter plus equals
    • 1:46:05one semicolon means take the variable on the
    • 1:46:08left and just add one to it and it's slightly more succinct and it
    • 1:46:11just makes your code a little more readable because it's just
    • 1:46:14fewer things for us humans to have to read but you can even do one step
    • 1:46:18further additional syntactic sugar exists
    • 1:46:21whereby you don't even need to type this you can instead just do counter plus
    • 1:46:24plus counter plus plus is the shortest hand notation in c
    • 1:46:28for just adding one to a variable all right besides variables what else do
    • 1:46:34we have in our toolkit as of last week what we also had in our toolkit last
    • 1:46:38week the notion of course of conditions a
    • 1:46:41condition was like a fork in the road that could allow you to do this thing
    • 1:46:44this other thing or or something else altogether
    • 1:46:46in scratch for instance if we wanted last week to compare two variables
    • 1:46:50x and y for inequality is x less than y and if so say x is less than y how can
    • 1:46:56we translate this to c well the syntax is going to be quite
    • 1:47:00simply this a little new some new stuff some more parentheses
    • 1:47:03some more curly braces but it kind of visually looks the same
    • 1:47:07albeit in text form i literally say if a space then in
    • 1:47:10parentheses i include my boolean expression
    • 1:47:13recall those from last week x less than y is my boolean expression
    • 1:47:17then notice i use an open curly brace and a closed curly brace and then i'm
    • 1:47:21just leaving a blank line for one or more lines of code just
    • 1:47:24like i might have last week and in fact let's put the
    • 1:47:26equivalent line of code here print out using printf x is less than y
    • 1:47:32backslash n so we've already done that translation before
    • 1:47:35say is just like printf just like if now is like if
    • 1:47:40strictly speaking especially if you've programmed before you do not need
    • 1:47:44these two curly braces if you only have one line of code
    • 1:47:48inside of the condition however stylistically for cs50
    • 1:47:51and for style 50 sake always include these curly braces nonetheless
    • 1:47:55and on their own lines all right what else can we do in scratch recall that we
    • 1:48:00can do if else and we can go either one way in the
    • 1:48:03fork or the other way in the fork in c the corresponding code
    • 1:48:07is going to look like this so it's almost the same as before
    • 1:48:10i've just added else and then another curly brace and a closed curly brace
    • 1:48:14and let me just add in the printfs and you can see that in c this is really
    • 1:48:17like the black and white the text-based version of
    • 1:48:20what was very graphical last week but the idea is the same you just got to
    • 1:48:23start to recognize where the parentheses go where the curly braces go the
    • 1:48:26semicolons and all that sort of visual stuff all right let's
    • 1:48:30make one more scratch and uh comparison here was one
    • 1:48:34where i said if x is less than y say x is less than y else if x is
    • 1:48:38greater than y say x is greater than y else if x equals
    • 1:48:42y then say x equal to y now here's where
    • 1:48:45scratch and c diverge because scratch is meant to be
    • 1:48:48very user friendly and not require long explanations of assignment operators
    • 1:48:52mit for scratch just use the equal sign for
    • 1:48:56assignment c uses the equals on uh no sorry rewind mit
    • 1:49:04used the equal sign for equality whereas c
    • 1:49:07uses the equal sign for assignment from left to right but this means equality
    • 1:49:11as before all right now notice the difference here
    • 1:49:16everything is a line-by-line translation although we can put elsif on the same
    • 1:49:20line and else if on the same line except here's kind of a stupid work
    • 1:49:23around right in some sense humans decades ago realized oh shoot at one
    • 1:49:27point we've already used the equal sign for
    • 1:49:30assignment what do we use now for equality
    • 1:49:33well mit ignored that problem and just used a single equal sign for equality
    • 1:49:37computer scientists inventing c and subsequent languages
    • 1:49:41when comparing two values on the left and right for equality
    • 1:49:44use two equal signs just because one equal sign is assignment from right to
    • 1:49:49left two equal signs is equality comparisons
    • 1:49:52are these two values equal but you know what this is not
    • 1:49:56necessarily well designed it is correct logically both my scratch
    • 1:50:01code and my c code is correct but can anyone make an observation as to
    • 1:50:05why the code is not necessarily well
    • 1:50:08designed i'm doing a little more work than i need to i
    • 1:50:12could tighten this code up a little bit i could type slightly fewer characters
    • 1:50:17and accomplish the same correct decision making
    • 1:50:22any thoughts on in what sense this code is not
    • 1:50:25perfectly designed uh great over to you
    • 1:50:32yes so you used elsif two times you could have used else in
    • 1:50:37the end without the condition really good
    • 1:50:40observation i'm using elsif twice which logically is fine this code is correct
    • 1:50:44it's asking and answering the right questions
    • 1:50:47but consider this if x is less than y is one fork in the possibility one fork in
    • 1:50:52the road else if x greater than y is the second
    • 1:50:55what's the only other possibility logically in the world of math
    • 1:50:59either it's less than or greater than or equal to
    • 1:51:02there's no reason to belabor the point and ask that third question
    • 1:51:06explicitly let's simplify the code and marginally better design it as
    • 1:51:10this just get rid of the else if as you propose
    • 1:51:13which isn't that much cleaner isn't that much shorter but it does avoid asking an
    • 1:51:18additional question so instead of maybe three questions being asked now
    • 1:51:22there's only two and frankly if you're writing a lot of code or doing this
    • 1:51:25again and again and again that kind of difference might very well
    • 1:51:29add up and indeed give us now some better code so now that i have
    • 1:51:33the ability to use these conditions let's actually try
    • 1:51:36converting this into a program let me go ahead and open up a program that i wrote
    • 1:51:40in advance let me go ahead and copy a file that i
    • 1:51:43came with today called conditions.c and this is one of
    • 1:51:47the files available on the course's website let me just open this if only
    • 1:51:51because it's kind of an uh literal translation translation of
    • 1:51:55the scratch code to see so i've added my include files at the
    • 1:51:59top here i've and now let me show you what i've
    • 1:52:04actually done over here so i've opened up a file that i wrote in
    • 1:52:06advance called conditions.c and i have at the top of the file my two
    • 1:52:11includes as usual and then down here i have pretty much
    • 1:52:14what we just saw on the slide plus two calls or uses of get ins
    • 1:52:19and then i'm just asking this question down here if x less than y
    • 1:52:23else if x greater than y else go ahead and do the following
    • 1:52:26so it's just a copy paste pretty much of the scratch
    • 1:52:30translation let me go ahead and make conditions which again conditions.c is
    • 1:52:33the name of the file no apparent mistakes so let me go ahead
    • 1:52:37and run dot slash conditions enter and x will be 1 y is 2 and indeed
    • 1:52:42x is less than y if i go ahead and run this this time for instance with
    • 1:52:46how about 10 and 5 x is greater than y and then lastly if i go ahead and run
    • 1:52:51this with 4 and 4 x is equal to y so i now have a c
    • 1:52:55program that is actually adding conditions for me
    • 1:52:59which is uh adding conditions for me which is actually then allowing me to
    • 1:53:04make decisions and print out one thing
    • 1:53:06potentially or the other but let me do something
    • 1:53:10slightly fancier let me go ahead and open up another pro
    • 1:53:14actually let me write this program from scratch
    • 1:53:16suppose i want to write a program called agree.c
    • 1:53:19that simulates the idea of like these stupid forms that you have to agree to
    • 1:53:22when signing into when using a piece of software for the
    • 1:53:24first time or the like or even when i deleted a file before i had to type in
    • 1:53:28yes or why in order to proceed let me go ahead and
    • 1:53:31include cs50.h at the top let me go ahead and include standardio.h
    • 1:53:37at the top and then my int main void which is copy paste from before
    • 1:53:41and now let me do this let me go ahead and get not an int from the user and not
    • 1:53:44even a word let's keep it simple and just ask the user for
    • 1:53:47y or n for yes or no let me go ahead and give myself a char
    • 1:53:52and i'll call it c but i could call it anything like answer
    • 1:53:55but c seems reasonable if i only have one char let me go ahead and call the
    • 1:53:59function getchar and let me just ask do you agree
    • 1:54:03question mark and then let me go ahead and compare this so if c
    • 1:54:08equals y then let me go ahead and print out agreed
    • 1:54:14backslash n else if c equals n
    • 1:54:19let me go ahead and print out for instance
    • 1:54:22not agreed now unfortunately i've made a couple of mistakes here that one at
    • 1:54:29least one of which might be a little more obvious than the other
    • 1:54:32any thoughts on what mistakes or bugs i might have introduced
    • 1:54:36already into this program
    • 1:54:39any thoughts on bugs here
    • 1:54:43what do i seem to have done wrong
    • 1:54:47anyone at all yeah how about uh olivia what do you think
    • 1:54:59thank you oh here we go um for one thing the
    • 1:55:02for the boolean you did you used a single equal sign instead of the double
    • 1:55:07for both good so i use the single equal sign instead of double so i need to fix
    • 1:55:11that and there's another even more subtle bug
    • 1:55:14and this is because c is very uh specific when it comes to its
    • 1:55:18data types all this time i've been using double
    • 1:55:20quotes for strings but it turns out in c you have to use single quotes when
    • 1:55:25you're comparing individual characters so i'm going to go in here
    • 1:55:28and change only the quotes around why and n to be single quotes why
    • 1:55:33because i'm now dealing with the world of chars chars are
    • 1:55:36individual characters like y or n and when you are
    • 1:55:40talking about characters you need to quote them literally like this
    • 1:55:43the variable name c doesn't need to be quoted but y and n do need to be quoted
    • 1:55:47but i don't need to change any of my other quotes in the file
    • 1:55:50because those are still strings of text that is actual phrases or
    • 1:55:54sentences so let me go ahead and try running make agree
    • 1:55:58it compiles okay let me go ahead and run dot slash agree
    • 1:56:01do i agree let me go ahead and type in y agreed i like that so let me try
    • 1:56:05n no not agreed oh and i left off a backslash n so let me fix that real
    • 1:56:11quick just for consistency let me recompile my program and pretend
    • 1:56:15that never happened but let me very reasonably now do this
    • 1:56:17dot slash agree i want to agree and yes capital y
    • 1:56:22huh nothing happened what about n capital n
    • 1:56:26nothing happened but the program still works if i do lower case it works
    • 1:56:30and if i do lower case there it works so what's going on well again the computer
    • 1:56:34is only going to take you literally and even though we humans might be oh it's
    • 1:56:37fine it's uppercase or a lowercase you have to be more explicit so we can
    • 1:56:41ask two questions as follows we could do
    • 1:56:44something like else if c equals equals capital y in single quotes
    • 1:56:49you could imagine again saying agreed like this
    • 1:56:52but just like last week when i started copying and pasting scratch blocks like
    • 1:56:56that's probably not very good design similarly this block of code lines 11
    • 1:57:00through 14 is almost identical to 7 through 10. let's just get rid of
    • 1:57:06one of them and let's see if we can't combine these
    • 1:57:08thoughts let me express if c equals equals y or c equals equals
    • 1:57:13capital y and indeed you can use this vertical bar
    • 1:57:17operator which is the logical or operator and
    • 1:57:20actually say two questions at once it turns out you
    • 1:57:24can do this with the notion of and a logical and by using ampersand
    • 1:57:28ampersand more on those another time but two vertical bars is the equivalent
    • 1:57:32of just saying if this on the left or this on the right and now
    • 1:57:36if i save and recompile the program with make agree and do dot
    • 1:57:40slash agree you'll see that i can type in y in
    • 1:57:43lowercase or y in uppercase and now it works
    • 1:57:48so again it would have been correct to just add another elsif and another else
    • 1:57:52if but again not necessary because i can combine
    • 1:57:55these thoughts and make my program better designed and notice too all this
    • 1:57:59time i've been very religiously indenting every time i'm
    • 1:58:04inside of curly braces indenting every time i have an if
    • 1:58:08condition or an elf if so i'm manifesting hopefully good
    • 1:58:12style aesthetics as well all right well now
    • 1:58:16let's consider that we have the ability not only to
    • 1:58:18express conditions but how about also these things called loops well turns out
    • 1:58:23in scratch we had very straightforward loops do the following forever
    • 1:58:26c is a little clunkier there's no forever keyword in c
    • 1:58:30but we can mimic this idea as follows the closest way to translate forever in
    • 1:58:35c is actually to say while which kind of
    • 1:58:37has the right semantics in english like while something is the case do this
    • 1:58:42but you have to be even more explicit you can't just say while
    • 1:58:45and then say printf hello world it turns out in c that
    • 1:58:48while similar to a condition is constantly asking a question to decide
    • 1:58:54whether or not to continue very similar again to a condition with its own
    • 1:58:58boolean expression so with while in c you have to have
    • 1:59:02parentheses after the word while and you have to ask a question in those
    • 1:59:06parentheses you have to say something like
    • 1:59:08x greater than y x less than y or the like
    • 1:59:11but this is a bit of a corner case in the sense that if you want to do
    • 1:59:14something forever who cares what the question is you just want the answer
    • 1:59:18always to be yes or in computer terms always to be true and the most blunt way
    • 1:59:24to express true always is literally to write the
    • 1:59:27word true so even though this looks a little weird
    • 1:59:30this in c is how you deliberately induce what we'll call an infinite loop that
    • 1:59:34never stops because the true is always by definition
    • 1:59:38true you don't even have to ask a more
    • 1:59:40complicated question you could put a less than sign or greater than sign or
    • 1:59:43the like but if you just want something to happen
    • 1:59:45forever this is the most canonical way to express something forever well what
    • 1:59:50if you want to do something finitely many times well we can do that
    • 1:59:53in c as well using what's going to be called a for
    • 1:59:56loop uh or a while loop well let's consider both of
    • 2:00:00these uh in turn so if i want to do something 50 times
    • 2:00:03the most like mechanical manual way i can think of is
    • 2:00:07like just count on my fingers right like one two three all the way up to 50
    • 2:00:11somehow or 10 minimally on my or maximally on my hands
    • 2:00:14so how can i do something finitely many times in c
    • 2:00:17well i have at my disposal variable so let me give myself a variable called
    • 2:00:21counter initialize it to zero semicolon and the
    • 2:00:25data type will be int because i just need to count much like on my fingers
    • 2:00:28but you know what counter is a little verbose programmers whenever they're
    • 2:00:32counting frequently just counting up from zero on up they'll
    • 2:00:35often just use i for integer or c for character or s for
    • 2:00:40string you don't want to do that always in your
    • 2:00:42code it's sometimes better for your variables to be more descriptively named
    • 2:00:46but for a stupid variable that's just going to count from 0 on up let's just
    • 2:00:49keep it simple and call it i i can now do a while loop again but now
    • 2:00:53i have to ask a question because i don't want this
    • 2:00:55running forever i want it running 50 times what question could i ask
    • 2:00:58well why don't i just check well i is less than 50.
    • 2:01:01so it's like counting up on 50 fingers let me start at 0 and count up 2 but not
    • 2:01:06through i equals 50. so so long as i is less
    • 2:01:09than 50 do the following what do i want to do i
    • 2:01:12want to keep printing out hello world hello world hello world
    • 2:01:15but i'm not done because on every iteration
    • 2:01:18of this loop on every cycle of this loop i need to do one more thing
    • 2:01:22mathematically i need to add another finger add another finger
    • 2:01:26or in other words i need to add one to i so let me go ahead and set i
    • 2:01:30equal to whatever it is now plus one but again we have some syntactic sugar just
    • 2:01:34to make this a little cleaner a little tighter
    • 2:01:37i could do i plus equals one or even more succinctly
    • 2:01:40i plus plus so even though this is way more annoying to implement than in
    • 2:01:45scratch where mit just gives you what you want
    • 2:01:48in c we have all of the building blocks now with variables
    • 2:01:52and with uh loops to implement the notion
    • 2:01:56of repeating some finite number of times but there's another way to do this
    • 2:02:01and as you might have discovered with problems at zero there's different ways
    • 2:02:03to achieve the same goals in scratch similarly in c i could
    • 2:02:07do this i could just start counting at one and
    • 2:02:10count up through 50. so there's no greater than or equal
    • 2:02:13sign key on your keyboard most likely or less than or equal to so in c
    • 2:02:18as with other languages you just use two characters you do less than sign
    • 2:02:22followed by the equal sign and that expresses less than or equal to
    • 2:02:26so this is also correct if i start counting at one i need to count
    • 2:02:29through 50. you can do this don't do this this is unconventional
    • 2:02:34and like programmers will conventionally per last week when we started always
    • 2:02:38counting from zero with all the light bulbs off
    • 2:02:40they'll instead start at zero and count up 250
    • 2:02:44which gives you 0 through 49 implicitly so do this
    • 2:02:48not that but this does speak to the fact that you can solve problems in so many
    • 2:02:52different ways there's another way fundamentally too we could start
    • 2:02:55counting from 50 down to zero the only difference
    • 2:02:59being we have to do i minus minus instead of i
    • 2:03:02plus plus so again that's three different ways to solve the same problem
    • 2:03:06and again you'll start to have the right instincts and muscle memory and you'll
    • 2:03:08also start to see common patterns in lecture code your tf
    • 2:03:12or teaching assistance code books and references online there just
    • 2:03:15tend to be the ways to do things even though all of these are still right
    • 2:03:20all right one more approach to loops here it turns
    • 2:03:23out there's another loop construct that's a little more cryptic and it's
    • 2:03:25called a for loop and it allows you to automate or rather
    • 2:03:29allows you to express all of those steps a little more
    • 2:03:32concisely so for printf hello world is going to get us one step
    • 2:03:37closer to printing hello world 50 times but the for statement just like the
    • 2:03:42while statement comes with necessary parentheses after it
    • 2:03:46but this time you can put more stuff in the parentheses it's not just a boolean
    • 2:03:49expression first the first thing in the parentheses
    • 2:03:53is you can initialize any variable you want to some value i
    • 2:03:56might say in counter equals zero or more succinctly int
    • 2:03:59i equals zero semicolon but the way the for loop looks it's a little funky you
    • 2:04:04can do multiple things on one line the second thing inside of the
    • 2:04:08parentheses to a for loop is a condition that you want to check
    • 2:04:11again and again and again and the last thing
    • 2:04:14in the parentheses of a for loop is an update or an incrementation or
    • 2:04:18decrementation whereby you can do i equals i plus one
    • 2:04:22or rather let's just do i plus equals one or even
    • 2:04:24more succinctly i plus plus this is perhaps the most
    • 2:04:29conventional way in c and in other programming languages
    • 2:04:33to do something 50 times or a finite number of times
    • 2:04:36it's different looking from the things we've seen thus far there's semicolons
    • 2:04:40in weirder places there's more stuff in parentheses so
    • 2:04:42again you'll develop the muscle memory overall
    • 2:04:44but for now just realize this says initialize i to zero
    • 2:04:49check the condition and if i is less than 50 print hello world
    • 2:04:53then update i then check the condition if it's less than 50 print hello world
    • 2:04:59then increment i then check the condition
    • 2:05:02then if it's less than print hello world so
    • 2:05:05the initialization of the variable happens once
    • 2:05:08everything else happens again and again and again until you've done this
    • 2:05:12some 50 times all right so with those building blocks that's kind of it for
    • 2:05:17our translation of scratch into c let's now start to build up some more
    • 2:05:23interesting programs and practice for instance abstraction
    • 2:05:26so abstraction recall was this problem-solving principle
    • 2:05:29whereby you can simplify otherwise more complicated
    • 2:05:33details an abstraction is a simplification on top of
    • 2:05:36more complicated details or implementation details as a programmer
    • 2:05:40might say so for instance let me go ahead and
    • 2:05:42write a program here called meow similar to last week but this time in c
    • 2:05:47and in order to make a cat meow textually let me give
    • 2:05:50myself standard io.h at the top in int main void down here
    • 2:05:56again i'm in a file called meow.c and i've included standardio.h
    • 2:06:01and int main void and now i'm going to go ahead and just do something like this
    • 2:06:04printf quote unquote meow backslash n and i want this cat to meow textually
    • 2:06:10three times let me save that file make meow
    • 2:06:14all right now dot slash meow and meow meow meow all in text so not nearly as
    • 2:06:19cute or pretty as the one with the cat last week
    • 2:06:21but it's it's correct but it's not very well designed
    • 2:06:25right because i'm repeating myself i literally copied and pasted and those
    • 2:06:29are bad instincts but now we have the ability to
    • 2:06:32do things with loops so let me actually delete this part of
    • 2:06:35the function and let me try to remember from the example before if i want to do
    • 2:06:39something three times i could use a while loop but that felt like a bunch of
    • 2:06:43lines of code let me do this int i equals zero i
    • 2:06:47less than three i plus plus so cryptic but this again is sort of the
    • 2:06:52de facto way of doing something a finite number of
    • 2:06:56times initialize some variable like i to zero check a condition and
    • 2:07:01keep incrementing your variable again and
    • 2:07:03again so that it executes a total of that many times
    • 2:07:07now let me go ahead and print f meow on melo
    • 2:07:10meow on the inside let me go ahead and recompile
    • 2:07:14meow by make meow let me do dot slash meow and voila
    • 2:07:18now the program is arguably better designed but let me take this one step
    • 2:07:22further recall that the trajectory last week was to not only implement meow
    • 2:07:27more uh with better design without repeating yourself thereby using a loop
    • 2:07:32but remember we introduced the abstraction that was a custom puzzle
    • 2:07:35piece called meow so in c turns out we have the ability to
    • 2:07:38make our own functions as well and the syntax is
    • 2:07:40going to take a little getting used to but let me go ahead and do this let me
    • 2:07:44get rid of my printf here and at the bottom of my file actually at
    • 2:07:47the top of my file i'm going to go ahead and type void main
    • 2:07:52void meow void which is very cryptic for
    • 2:07:55today but again this is fine to be boilerplate copy pasted for now
    • 2:07:59let me go ahead and just print f meow here even though we haven't explained
    • 2:08:04and won't explain today what this keyword void means what i've
    • 2:08:08done in lines three through six is create my own custom function
    • 2:08:12c does not come with a function called meow cs50 is library does not come with
    • 2:08:16a function called meow but now thanks to me there exists a
    • 2:08:19function called meow whose sole purpose in life
    • 2:08:22is just to print out meow but what's cool about this now
    • 2:08:25is that down here just like with scratch last week i can now
    • 2:08:29call a function called meow and my code's a little more readable because it
    • 2:08:34rather says what it does by just by way of the
    • 2:08:37function's name and let me go ahead now and compile this
    • 2:08:41make meow so far so good dot slash meow and it
    • 2:08:46seems to work okay but i don't love the fact that i've
    • 2:08:50implemented meow at the top of the file it's not a big deal
    • 2:08:54by convention we'll typically put custom functions
    • 2:08:57at the bottom of the file why only because when a programmer or in our case
    • 2:09:02a teaching fellow wants to understand your code from top
    • 2:09:05to bottom it's just human convention to put the main program
    • 2:09:08the main function rather at the top of your file
    • 2:09:12the problem is when i do this i'm going to have created a problem for myself
    • 2:09:16when i run make meow now darn it two errors generated
    • 2:09:21and so there's a couple of bugs to be solved but first brian
    • 2:09:24a question from the group there was a question that came in from the chat
    • 2:09:29about why it is that on like line five for
    • 2:09:34example right here you don't have a semicolon at the end of the for loop and
    • 2:09:37on line 11 you don't have a semicolon at the end of the function name
    • 2:09:40so why do some lines need semicolons at the end of them but other lines don't
    • 2:09:43really good question why do some of these lines not have semicolons but
    • 2:09:46others do the short answer not to be glib is
    • 2:09:49honestly just because the way the language was designed was
    • 2:09:54that you should generally finish your thoughts when expressing
    • 2:09:57verbs or actions or functions with semicolons and we've seen that
    • 2:10:01after printf for instance we've just seen that after meow
    • 2:10:04however when you're using other programming constructs like
    • 2:10:08loops or like custom functions you don't have semicolons there
    • 2:10:13why some humans years ago just decided that we don't need semicolons in those
    • 2:10:17places and this is one of those things that it will take a while to develop the
    • 2:10:20muscle memory and the sort of mental model for recognizing
    • 2:10:22where those things go and don't but thus far the only places we've seen
    • 2:10:26semicolons are at the ends of functions like meow and printf here and now
    • 2:10:31admittedly weirdly inside of the parentheses
    • 2:10:35for the for loop but again when tackling problem said
    • 2:10:38one and the first lab and so forth you'll often want to refer back to
    • 2:10:41examples like these and the slides and the references in your section
    • 2:10:45so that you can wrap your mind around these patterns
    • 2:10:48so let me go ahead now and solve the two problems i seem to have created here
    • 2:10:51it's a little non-obvious but it's reminiscent of what we've seen before
    • 2:10:55implicit declaration of function meow is invalid in c99
    • 2:10:59c99 is referring to the 1999 version of c
    • 2:11:02the language we're using but it's just getting confused
    • 2:11:06c is right now well why is that well let me scroll up here
    • 2:11:09and let me make the point that frankly c and in turn my compiler
    • 2:11:13they're not that bright like they're only going to do what i tell them to do
    • 2:11:16explicitly and the problem at the moment is that
    • 2:11:19when the compiler reads my code from top to bottom left to right it is not
    • 2:11:23until line 11 that the meow function even exists
    • 2:11:28however i am trying on line seven at the moment
    • 2:11:31to use that meow function so my compiler frankly
    • 2:11:34just doesn't know what meow is because it hasn't gotten to meow later and the
    • 2:11:38compiler is not smart enough or not user friendly enough to read
    • 2:11:42everything first and then decide if there's a problem it's only going to
    • 2:11:46read it once through top to bottom and it's going to yell at you the moment
    • 2:11:49it encounters the problem so the solution to this is quite simply
    • 2:11:53move the function to the top of your file but again that just gets annoying
    • 2:11:56eventually because then you have to go fishing for your main function which
    • 2:11:59might be dozens of lines down in the file
    • 2:12:01or there's another way and we'll explain this in due time too but you can also
    • 2:12:06copy the very first line only of your custom function
    • 2:12:10put it at the top of your file above main
    • 2:12:14and then to brian's question and that with a semicolon so this is weird
    • 2:12:19this is what's generally known as a prototype which is a hint
    • 2:12:23only it's sort of a clever way of telling the compiler
    • 2:12:26there will exist a function called meow but just not yet
    • 2:12:30but know that it will and it's just kind of a work around a common work around
    • 2:12:34for that particular problem all right so let me go ahead and make one more change
    • 2:12:39one more change here suppose that i want to really finish off
    • 2:12:43this meowing example just like we did in scratch whereby we
    • 2:12:45also allow meow to take some number of meows as input so i don't want to
    • 2:12:51have this for loop anymore in my main function
    • 2:12:54suppose i just want to be able to say meow
    • 2:12:573 inside of my main function 3 there by being the input to the meow function
    • 2:13:02i now need to change my custom function just like i did last week
    • 2:13:06as follows it turns out and more on this in the weeks to come
    • 2:13:10that this mention of void here on line 11
    • 2:13:13refers to the return value or output of this function
    • 2:13:16long story short my custom meow function today has no
    • 2:13:20return value it doesn't output anything per se it instead only has a side effect
    • 2:13:25of printing visually on the screen but it does have an input
    • 2:13:28and if you want a function in c to take input or
    • 2:13:32arguments you can literally do something like the name of the type you want
    • 2:13:36and the name of the variable that you want so suppose i want meow to take as
    • 2:13:40input some number we'll call it n and i want to use that number in a loop
    • 2:13:44i could then do something like this for int i gets 0 i is less than
    • 2:13:49n i plus plus i can then surround my printf
    • 2:13:53with curly braces and now notice just like last week with my final
    • 2:13:58implementation of now my custom function can take input as
    • 2:14:01denoted by the parentheses it doesn't have output per se that's why
    • 2:14:05i'm leaving void here but again we'll explain void more
    • 2:14:08in detail down the road but now i'm using that input
    • 2:14:12inside of the for loop so even though this is a new
    • 2:14:15implementation in c i'm using the same building blocks i'm using a for loop
    • 2:14:19i've like before but instead of hard coding three or 50 like i did earlier
    • 2:14:23now i'm actually going to go ahead and just plug in that variable
    • 2:14:28just like scratch allowed me to do as well
    • 2:14:33what if i want to do something even fancier you know what let me go ahead
    • 2:14:36and do this suppose that we want to get input from the user
    • 2:14:40but we really want them to provide a specific type of input
    • 2:14:43let me go ahead and introduce one other type of loop and this one i'm going to
    • 2:14:46go ahead and grab for my archives the code that i brought
    • 2:14:49with me today and i'm going to go ahead and copy over a file
    • 2:14:53called positive.c which is going to insist that the user give me a positive
    • 2:14:58value so this too is on the course's website
    • 2:15:01let me just walk us through code that i already wrote
    • 2:15:04here at the top of my file i'm including some now familiar header files
    • 2:15:08and down here i'm including a prototype that is a hint
    • 2:15:12for a function that's going to be called quite simply get positive int
    • 2:15:16so this is a function that's only going to get a positive integer then in my
    • 2:15:19main function notice i'm going to use this i'm going
    • 2:15:22to get a variable called i on line 10 and i'm going to get a
    • 2:15:25positive in from the user and then i'm just going to print it out
    • 2:15:29but what's interesting now is i have this additional abstraction the
    • 2:15:33cs50 library does not come with a function called getpositiveint
    • 2:15:36but it does come with a function called getint and notice what i've done down
    • 2:15:40here between lines 15 and 24. down here i've declared a function
    • 2:15:46called getpositiveint and notice that's my own custom function
    • 2:15:50name it doesn't take any inputs it just gets a positive integer from the human
    • 2:15:54but now notice it does have a return value previously i
    • 2:15:58used the word void to say the absence of input or the absence of output here i'm
    • 2:16:02using it still to say no inputs it just always gets a positive
    • 2:16:06end but i'm saying int on the left hand side of this custom
    • 2:16:09function's name because this function does have
    • 2:16:11output what is the output going to be well notice here on line
    • 2:16:1517 i give myself a variable and i call it n then i have one final new feature
    • 2:16:20of c today this loop which is called not a while loop but a
    • 2:16:24do while loop a do while loop is almost the same as a
    • 2:16:28while loop except that it blindly does one thing
    • 2:16:31first before checking a condition so notice
    • 2:16:34here i'm going to do the following call getint with this prompt positive
    • 2:16:39integer and then store the return value into the variable called n then down
    • 2:16:43here notice i'm saying while n less than one so this is kind of
    • 2:16:48a weird syntax but if mathematically i want the user to
    • 2:16:51give me a positive integer that's technically the same thing as
    • 2:16:54wanting the user to give me an integer and just make sure that it is not
    • 2:17:00less than one because if it's less than one it's zero or negative one or
    • 2:17:03negative two that's obviously not a positive integer so how can i express
    • 2:17:06this in code the only new thing at the moment now is
    • 2:17:09the fact that there exists this thing called do while and again the value of
    • 2:17:12do while is that you will do things at least once
    • 2:17:15and then check a condition a while loop checks
    • 2:17:18the condition first and then does something instead this is
    • 2:17:21what i want in this case though i want to do this
    • 2:17:23get a positive get an integer from the user prompting them for a positive
    • 2:17:27integer then while n is less than one
    • 2:17:32so if the human typed in zero or negative one or negative two what do i
    • 2:17:35wanna do the same thing again and again and again
    • 2:17:39so it reads rather grammatically do the following while n is less than
    • 2:17:44one and then lastly and the only other new
    • 2:17:47line here is return n this is the way that a program can
    • 2:17:51actually return some value to you it can hand you back a
    • 2:17:54value not by printing it on the screen not by saying it audibly or visually
    • 2:17:57from a cat's mouth it returns it in the sense that what's
    • 2:18:01being returned here is n which is an integer that matches the
    • 2:18:04output of this function why is this useful well let's scroll
    • 2:18:08back up let's now take for granted that the get positive in function exists
    • 2:18:12and now notice how we can use it in main i call get
    • 2:18:16positive int on the right it returns a value i claim that is of
    • 2:18:20type integer i'm storing that return value on the
    • 2:18:23left in this variable called i and then i'm printing out i and just
    • 2:18:28like last week with the meow example in scratch
    • 2:18:30now that i have implemented get positive in it's sort of out of sight out of mind
    • 2:18:35i know that it can be done and i can abstract away
    • 2:18:38the underlying implementation details by just
    • 2:18:41calling it by its name but there's one weird thing
    • 2:18:45i do want to point out about these implementation details
    • 2:18:48why did i declare n out here every other time i've created a variable
    • 2:18:55i've done this does anyone want to conjecture why
    • 2:18:59this approach where i declare n on line 27 to be an int
    • 2:19:04would actually not work correctly any thoughts
    • 2:19:11why might this not be correct this is subtle
    • 2:19:15and if you have programmed before you might have the instincts for this or
    • 2:19:18if you haven't perhaps just a bit of intuition
    • 2:19:23why might this not work as is uh yeah over to uh abigail
    • 2:19:33abigail why does this not work are we catching you off guard
    • 2:19:46um i'm not certain i think we've already
    • 2:19:51assigned that it is an integer a good good thinking
    • 2:19:56not the correct answer in this case it's not going to work for a more subtle
    • 2:20:01reason here and that actually in some sense has to
    • 2:20:03do with these curly braces it turns out we've been getting lucky this whole time
    • 2:20:07and any time i've declared variables they've technically been in between
    • 2:20:11curly braces the curly braces belonging to the main function
    • 2:20:14or my other functions that i've written thus far
    • 2:20:18but in this case the problem is when you declare a variable
    • 2:20:21inside of curly braces you run into what we'll call an issue of scope
    • 2:20:25the scope of a variable is the lines of code in which it exists
    • 2:20:30the scope of a variable are the lines of code where you can use
    • 2:20:33that variable and the rule of thumb for today
    • 2:20:36is that if you declare a variable inside of curly braces like those here on line
    • 2:20:4126 and 28 which you must do for a do while loop that variable n only
    • 2:20:47exists inside of those curly braces which means you cannot compare it
    • 2:20:51against one in line 29 which means you cannot
    • 2:20:54return it in line 30 it just no longer exists so
    • 2:20:58you're doing all of this work getting the variable n and then
    • 2:21:01boom it's gone once you exit top to bottom these curly braces so the
    • 2:21:07work around for that stupid though it is frankly is that you
    • 2:21:10can declare an initially on its own line 25
    • 2:21:14you don't need to assign it a value even because you're going to assign it a
    • 2:21:17value eventually but again to create a variable as i keep
    • 2:21:20saying is to declare a variable you don't need to define it as having a
    • 2:21:24value necessarily right away so this is a way to work
    • 2:21:28around what's otherwise known as an issue of scope all right with all of
    • 2:21:34these puzzle pieces now in place so to speak
    • 2:21:36let me go ahead and propose that we solve something a little more graphical
    • 2:21:40so you'll recall of course super mario brothers is we uh one of the
    • 2:21:44first problem sets that we alluded to last week and within this game there's a
    • 2:21:47whole bunch of visuals for instance there's this visual early
    • 2:21:49on where there's four question marks in the sky
    • 2:21:52and these question marks if you jump up and underneath them give you coins for
    • 2:21:56instance so let me draw our attention to that and let me ask
    • 2:21:58well how could i write a program in c that just prints out for question marks
    • 2:22:02well let me go ahead and do this let me go ahead and write a program called
    • 2:22:04mario.c let me go ahead and include uh
    • 2:22:08standardio.h in a file called mario.c give myself a
    • 2:22:13main function in main void i'm going to keep this simple
    • 2:22:16printf1234 backslash n semicolon this is not nearly as cool or pretty as the
    • 2:22:22old school game but if i run make mario and then do dot slash mario voila i get
    • 2:22:27a very uh poor approximation of these
    • 2:22:32four blocks in the sky using just ascii or
    • 2:22:35really what is called ascii art but i can do a little better than that
    • 2:22:38recall that now we have the ability to use loop so i could say for
    • 2:22:41int i gets 0 i is less than 4 i plus plus and then i could just print
    • 2:22:47out one question mark at a time and then at
    • 2:22:51the very end of my program i could print out a new line
    • 2:22:53just to move the cursor at the very last moment i don't want to do that
    • 2:22:57every question mark because then it would be vertical i want to do it only
    • 2:23:00at the end so now if i make mario and i run mario
    • 2:23:03now same exact result but a little better in
    • 2:23:06the sense that it is um now uh more dynam
    • 2:23:11now it's uh using a loop instead of a hard-coded value
    • 2:23:15but let me be a little more clever now and let me do this instead
    • 2:23:19let me borrow the logic of that positive integer example and do something like
    • 2:23:23this let me give myself a variable called
    • 2:23:24n for a number and let me do the following just like before
    • 2:23:28let me get an integer from the user and ask the user for the width
    • 2:23:32of the bricks that i want to print so it's not always four maybe it's a
    • 2:23:36variable number and then let me go ahead and do this
    • 2:23:40while n is less than one so identical to my logic before and then you know
    • 2:23:45what once i have a value of n so let me go ahead up here and give
    • 2:23:49myself a comment get positive integer from user that
    • 2:23:53rather says what all of these lines of code do i don't need to comment every
    • 2:23:56single line you can comment every few if it makes
    • 2:23:59logical sense to do so let me go ahead now and print out that
    • 2:24:03many question marks so i can do a loop for
    • 2:24:07int i gets 0 i is less than n this time i
    • 2:24:11plus plus and now i can print out a single question mark without a new line
    • 2:24:15and then at the very end of my program i can print out a single new line
    • 2:24:20semicolon let me go ahead now and increase the size of my terminal window
    • 2:24:24let me do make mario and now oh darn it implicit declaration of function
    • 2:24:30get int here's where help50 might be my friend
    • 2:24:33so let me go ahead and run help50 make mario
    • 2:24:36it's going to ask for help ah you seem to have an error in mario.c
    • 2:24:40on line 9 by implicit declaration of function getint clang means which is the
    • 2:24:44name of the compiler which we'll see next week
    • 2:24:46means that it doesn't recognize getint did you forget to include cs50.h
    • 2:24:50in which inkgetint is declared i type your file and indeed i did so let me fix
    • 2:24:54this so include cs50.h
    • 2:24:59save the file recompile with make mario and now let me go ahead and may and do
    • 2:25:05dot slash mario and i'll give myself a width of four
    • 2:25:08it's the same let me give myself a width of 40. now i get that dynamism
    • 2:25:12let me give myself a width of 50 and so forth so now we have a program
    • 2:25:16that's much more dynamic but you know what let's go ahead and
    • 2:25:19enhance this a little further later on in super mario brothers there's
    • 2:25:22like a lot of this underworld here where you see these grids of bricks and let me
    • 2:25:26draw our attention to this this looks like multiple bricks both
    • 2:25:29horizontally and vertically so there's like a width and a
    • 2:25:32height so how can i go about printing out maybe maybe that's three by three
    • 2:25:36three bricks across by three bricks down let me actually go into my program here
    • 2:25:42and get rid of all the question mark stuff from before
    • 2:25:45and consider how i could print out a three by three grid well
    • 2:25:49the bad approach here if i go back into my code
    • 2:25:53would be to print out three of these and then maybe
    • 2:25:56three more whoops maybe three more and then three more
    • 2:26:00but of course this copy paste is not going to fly long term but that's fine
    • 2:26:04let me do make mario dot slash mario all right i kinda sort of have a grid
    • 2:26:09that looks like this thing here it's not exactly but at least it's the right idea
    • 2:26:13but this is not necessarily the best way to do this i really want to go
    • 2:26:17three across and three down well it turns out
    • 2:26:21using c we can actually express that as well let me go ahead and do this let
    • 2:26:26me go ahead and print out for instance the following let me go ahead and print
    • 2:26:32out not just hash again and again and again let me go
    • 2:26:35ahead and do this let me print out one row at a time so
    • 2:26:39for int i gets 0 i less than 3 i plus plus
    • 2:26:43i don't know what i'm about to do yet but i know that i'm going to do it three
    • 2:26:46times what do i want to do three times well i
    • 2:26:48want three rows and on every row i want three hashes so you know what you can do
    • 2:26:53you can nest loops let me do four int j gets zero
    • 2:26:57j is less than three and j plus plus i don't know what i'm going to do yet but
    • 2:27:01i do know i'm going to do this three times and you can perhaps see
    • 2:27:04where this is going three things three times that's going to give me all nine
    • 2:27:08bricks so long as inside of this inner loop
    • 2:27:12so to speak this nested loop i print one of those hashes
    • 2:27:16so long as after that loop i print out a new line
    • 2:27:19over here so to be clear even if it's not obvious at first glance what's going
    • 2:27:24on we already know that this is the type of
    • 2:27:26syntax you use for doing something finitely many times
    • 2:27:30three this is the same syntax but i'm using a different variable name so i can
    • 2:27:33keep track of two different values essentially rows and columns
    • 2:27:38and then i'm just printing a single brick each time but after i'm done
    • 2:27:41printing a whole row i do want to move the new cursor to a
    • 2:27:45new line so let me try this let me go ahead and
    • 2:27:47do make mario on my code dot slash mario and voila now i'm using
    • 2:27:53a nested loop to print out bricks like this and i can change this if i want to
    • 2:27:56do a 10 by 10 all i have to do is change that in one
    • 2:27:59place or if i really wanted to be fancy i could go use get int again
    • 2:28:03i could get the width and the height from the user and do it completely
    • 2:28:06dynamically but now if i do 10 by 10 for instance i can at least see an even
    • 2:28:11bigger grid so if you wonder how things like super mario bros or frankly any
    • 2:28:14game nowadays on a pc or console or phone or made it's with
    • 2:28:19this kind of generation of maps maybe back in the day they were hard-coded
    • 2:28:22maybe they were generated using code you can absolutely imagine
    • 2:28:26generating brick after brick after brick like that
    • 2:28:29so that ultimately your game even your world is partly dynamically generated
    • 2:28:34and we already have the building blocks via which to do that unfortunately we
    • 2:28:40haven't really spoken to the limitations of what
    • 2:28:44computers can do and in our final minutes we thought we'd
    • 2:28:47set the stage for things that computers aren't very good at and in fact problems
    • 2:28:51that are exist that are latent in pretty much
    • 2:28:54everything we've done today but i've been very carefully avoiding tripping
    • 2:28:57over this picture here is a picture of a
    • 2:28:59typical computer's memory or ram random access memory it's just one of
    • 2:29:03the pieces of hardware that you'd have in your phone
    • 2:29:05your desktop your laptop these days and it's where programs are stored
    • 2:29:09when they're running so on a mac or pc if you double click a program
    • 2:29:12it's ultimately stored in a piece of hardware that looks like this when you
    • 2:29:16do dot slash mario and hit enter in a
    • 2:29:18program like this you're using cs50 ide's ram
    • 2:29:21but same idea albeit somewhere else in the cloud
    • 2:29:25so it turns out though that if you only have a finite amount of memory like this
    • 2:29:30you can only do so much with it you can't solve all of the world's problems
    • 2:29:34if you only have a finite amount of memory
    • 2:29:37and what do i mean by that well let me go ahead and create
    • 2:29:40another program here called imprecision.c
    • 2:29:44and we'll see why i've named it that in just a moment let me go ahead and
    • 2:29:47include standardio.h again in in main void just
    • 2:29:50to give myself some setup here and then let me go ahead and very
    • 2:29:53reasonably very simply ask the user for a variable called x as a type float let
    • 2:29:58me ask for the value just like before let me ask for
    • 2:30:01another one in the form of y quote-unquote y and then let me go ahead
    • 2:30:06and let me go ahead and print out with
    • 2:30:08percent f the value of x divided by y so i'm
    • 2:30:12pretty sure we did this earlier we did division
    • 2:30:15with values this time i'm using floats but let's go ahead and just run this
    • 2:30:18make imprecision oh and i goofed here implicit
    • 2:30:22keep doing it implicit declaration of function get flow
    • 2:30:25i didn't practice what i've been preaching i also need to include
    • 2:30:29cs50.h which is where getfloat is defined
    • 2:30:32now let me recompile now it works now let me go ahead and run imprecision
    • 2:30:37and let me go ahead and type in 1 uh 10th
    • 2:30:40okay so 1 10th it turns out according to my very simple calculator here
    • 2:30:44is 0.10
    • 2:30:48but i'm getting a little curious now it turns out that printf
    • 2:30:51is even more powerful than we've seen and you can actually print out more than
    • 2:30:54just single digits suppose i want to print out not six
    • 2:30:59significant digits but maybe 10. it's a little funky the syntax but instead of
    • 2:31:03saying percent f you can instead say literally percent
    • 2:31:07dot the number of digits you want to see and
    • 2:31:09then the f so let me go ahead and recompile this
    • 2:31:13make imprecision now let me do dot slash imprecision and
    • 2:31:17one tenth ha ha well that's a little curious
    • 2:31:22i don't recall knowing that there's a one five at the end of one tenth
    • 2:31:26well let's get a little more curious let's print out 50 decimal points to
    • 2:31:29really dig into what's going on here let me recompile my code and let me
    • 2:31:33rerun dot slash imprecision and do 1 divided by 10 and oh my god like i am
    • 2:31:39quite sure in grade school when we all learned 1 divided by
    • 2:31:4210 the teacher did tell us 1 10 or 0.1 and they never mentioned the fact that
    • 2:31:46it's 0.1000001490116 and so forth so what is going on well it
    • 2:31:54turns out as powerful as computers are and is
    • 2:31:57as sophisticated as all of the syntax we've been looking at today is
    • 2:32:01my god a computer you can't even calculate one-tenth
    • 2:32:05correctly and so we're bumping up against a fundamental limitation here
    • 2:32:08which is that if computers are finite in their capacity they only have
    • 2:32:12so much ram so much hardware so many bits well
    • 2:32:16it stands to reason that if you only are using a finite number of bits
    • 2:32:2032 for instance or 64 yes you can count pretty high or pretty precisely you
    • 2:32:26cannot count infinitely high or infinitely precisely
    • 2:32:31at some point you have to start to approximate values
    • 2:32:34and indeed that's what the computer is doing
    • 2:32:36if it only has a finite number of bits 32
    • 2:32:39via which to represent a float there's a infinite number of floating point
    • 2:32:43real numbers in the world unfortunately if you have a finite number of bits you
    • 2:32:47got to start cutting some corners and that's what the computer is doing
    • 2:32:50it's representing one tenth as closely as it can
    • 2:32:54and this is what you then see when you look enough significant digits out now
    • 2:32:57for most problems that's probably not a big deal
    • 2:33:00but it could very well be a big deal if you're doing math you're dealing with
    • 2:33:04finance or monetary values or military
    • 2:33:07operations where a lot of small numbers uh scientifically really start to add up
    • 2:33:11and indeed there have been many examples in the real world where bad things
    • 2:33:14happen because of this so-called imprecision
    • 2:33:18and there's another issue that computers run into not only
    • 2:33:21this floating point in precision even integers have their limitations
    • 2:33:25recall that integers of course can be represented in decimal or in
    • 2:33:29binary and if we have three light bulbs or three bits let's consider how we
    • 2:33:32might count in binary zero zero zero is where we began last
    • 2:33:36week zero zero one zero one zero zero one one one zero zero
    • 2:33:41one zero one one one zero one one one and now
    • 2:33:44this recall from last week is the number we know is seven in decimal
    • 2:33:48how do i count one digit higher well i just
    • 2:33:51carry the one so to speak but if i only have three light bulbs or in turn three
    • 2:33:55bits or heck 32 bits that additional bit the
    • 2:33:59carry disappears and so there's this problem
    • 2:34:02with integers too and someone noted it earlier when we
    • 2:34:05tried to do two billion or when you tried to do two billion plus two billion
    • 2:34:09it couldn't fit in the result that's because integers two
    • 2:34:12if they're only 32 bits or a long in c if it's only 64 bits those are big
    • 2:34:17numbers but they're not infinitely large and we humans have
    • 2:34:21tripped over this again and again you might recall hearing about if not
    • 2:34:24living through the y2k problem where a lot of stuff in the world broke
    • 2:34:29or was worried to be broken on january 1st 2000 because humans had
    • 2:34:33made the reasonable but not very long-sighted
    • 2:34:36decision to store years using just two digits so 1995
    • 2:34:40would be represented as 9.5 96 97 98 99 then
    • 2:34:46around the change of the year from 1999 to 2000
    • 2:34:49any computer program or system that was still using two digits
    • 2:34:53would of course add one at the stroke of midnight unfortunately if there's no
    • 2:34:57third digit available it disappears and the entire
    • 2:35:01world confuses the year 2000 for the year 1900 because 1900 was
    • 2:35:07assumed as being the prefix and if you can
    • 2:35:10believe it we humans are about to do this again
    • 2:35:14in the year 2038 which is not that far off from now
    • 2:35:18we are going to run out of bits by which to keep track of time because
    • 2:35:24years ago humans decided reasonably at the time
    • 2:35:27they are going to use 32 bits to represent numbers
    • 2:35:30and we are going to use 32 bits to count up
    • 2:35:33the number of seconds from the year a date
    • 2:35:37january 1st 1970. so that's when time began computing wise in some sense and
    • 2:35:42we've been counting the seconds ever since
    • 2:35:44unfortunately with 32 bits you can only count as high as 4 billion give or take
    • 2:35:49and unfortunately we are going to hit the 4 billionth second on
    • 2:35:54january 19th in the year 2038 so unless all of us upgrade our macs and pcs and
    • 2:35:59worse embedded systems and satellites and any hardware
    • 2:36:03baked into various devices that we now use we're about to run into this problem
    • 2:36:07again where all of a sudden it's going to be like january 1st
    • 2:36:111970 again unless we stay ahead of this problem
    • 2:36:15so with all the power we've seen and see and all of the capabilities we've seen
    • 2:36:19in c and in scratch there are still these
    • 2:36:20fundamental limitations so when it comes to solving your own problems in
    • 2:36:23c and in turn cs50 it's going to be ever so important to be mindful of these
    • 2:36:28constraints and to ultimately find solutions even to these problems but for
    • 2:36:32now we'll adjourn here and leave you for
    • 2:36:34your first problem set in c on mario and more we'll see you next
    • 2:36:48time
    • 2:36:58you
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video