C
Harvard CS50 Introduction to Computer Science, lecture 1, explained in plain English
Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.
Learn this lecture freeWatch or read the original lecture: the course website.
Part 1 · 0:00–10:00
This part introduces the C language, explains how human-written code becomes machine code, and shows the tools you will use to write and run it.
Key ideas
- C is an older, more traditional programming language. Last week's ideas, like functions, loops, and conditionals, still apply, but the code looks more cryptic. 1:01
- The code you write is called source code. Computers only understand machine code, which is made of zeros and ones, so source code must be converted before a computer can run it. 3:55
- A compiler is a program that translates one language into another. Here it turns source code into machine code, so you never have to write zeros and ones yourself. 4:40
- You will write code in a web browser at cs50.dev, which uses VS Code. This avoids setup problems because everyone uses the same cloud setup. 5:45
- The screen has two main parts: a place to write code at the top, and a terminal window at the bottom where you type commands. The terminal is a command-line interface, so you use the keyboard only. 7:54
Words to know
- source code
- The code that people write, in a higher-level language that humans can read more easily than zeros and ones.
- machine code
- The zeros and ones that a computer actually understands and runs.
- compiler
- A program that translates code from one language to another, such as from source code to machine code.
- command-line interface
- A way of using a computer by typing commands on the keyboard, instead of clicking icons and menus.
Example
Think of a compiler like a translator who turns a letter written in English into another language the reader understands. You write the letter in the way that is easy for you, and the translator does the hard work of converting it. The lesson uses this kind of idea to explain how source code becomes machine code.
3 checked practice questions for this part are in the app.
Part 2 · 10:00–20:00
This part shows how to write, compile, and run a first C program that prints hello, world, and why the output needed a fix.
Key ideas
- You write C code in a file ending in .c, then use make to compile it. Compiling turns the readable code into machine code, which is the zeros and ones a computer can run. Then you run the result with ./hello. 10:00
- The dollar sign is a prompt. It is where you type commands in the terminal, which is the text window where you talk to the computer. The dollar sign is not money. 10:40
- When make finishes with no messages, that is a good sign. If something is wrong, the computer shows error messages. 14:12
- C code is line-based, so each thought should start and finish on one line. Pressing Enter in the middle of a sentence of code causes an error. 15:35
- Text goes inside double quotes, and each line of C ends with a semicolon. To move the output to a new line, you add a backslash n inside the quotes. 16:39
Words to know
- compile
- To change code written by a person into machine code that a computer can run.
- source code
- The code a person writes, before it is changed into machine code.
- prompt
- The symbol in the terminal that shows where you can type a command, like a dollar sign.
- argument
- An input you give to a function, like the words inside the parentheses of printf.
Example
Think of writing a recipe in your own language and then giving it to a translator who turns it into a form a robot chef can follow. If the translator finds a mistake, they hand back a note saying what is wrong. If there is no note, the translation worked.
3 checked practice questions for this part are in the app.
Part 3 · 20:00–30:00
This part explains how C uses header files and libraries to get ready-made tools, and how a function can hand back an answer that you store in a variable.
Key ideas
- C keeps many of its ready-made tools in separate files. Writing #include stdio.h tells the compiler to load the file that knows how printf works, so it can print to the screen. 20:20
- A library is code someone else wrote that you can use. A header file is a file ending in .h that gives your program access to that library's code. 21:24
- Libraries let you rely on other people's work, so you can spend your time on the problems you care about. CS50 also gives you a website with plain-language help on functions. 21:46
- Some functions give back a return value, which is an answer you can use later. In C, you store it in a variable by writing the variable name on the left of an equals sign. 27:19
Words to know
- header file
- A file ending in .h that gives your program access to code someone else wrote, such as the printf instructions in stdio.h.
- library
- Code that someone else wrote that you can use in your own program, often for free.
- return value
- The answer a function gives back to you so you can use it later, instead of only showing it on the screen.
- string
- A word the lesson uses for text, such as a name typed by a user.
Example
Think of a function as a helper who writes an answer on a slip of paper and hands it to you. A return value is that slip of paper, and you can keep it, change it, or print it, as you like. Storing it in a variable is like putting the slip in a labeled box so you can find it again later.
3 checked practice questions for this part are in the app.
Part 4 · 30:00–40:00
This part shows how to ask the user for their name in C, store the answer in a variable, and print it back using printf, with a placeholder for the name.
Key ideas
- In C, you must name the type of each variable, such as string for text or int for a whole number. This tells the compiler how to store the value in memory. 30:00
- printf is C's version of Scratch's Say block. The %s inside its quoted text is a placeholder, and the real value is plugged in where the placeholder sits. 31:06
- A value that goes into the placeholder is passed as a second input, called an argument, after a comma. The first input to printf is always the quoted text itself, so anything to print must go inside the quotes. 32:35
- C only knows the functions it has been told about. The speaker had to include the CS50 header file so the compiler learned what get_string and the string type mean, and then the error went away. 39:22
Words to know
- compiler
- A program that turns the code you write into machine code the computer can run.
- argument
- A value you pass into a function inside its parentheses, so the function can use it.
- placeholder
- A marker, here %s, in a piece of text that gets replaced by a real value when the program runs.
- syntax highlighting
- Automatic coloring of code in an editor so that different parts, like functions and text, are easier to spot. You do not type the colors yourself.
Example
Think of a printed form letter that says 'Dear ____, thanks for visiting.' The blank is like the %s placeholder, and the name you write in is like the argument you pass in. The letter is only filled in correctly when the blank and the name you give it match up. (This comparison is my own illustration, not from the lesson.)
3 checked practice questions for this part are in the app.
Part 5 · 40:00–50:00
This part shows how C makes a program choose between paths using if, else if, and else, and how it stores whole numbers in variables.
Key ideas
- A conditional is a fork in the road. It lets a program pick one action out of several, depending on a question it asks. 41:22
- A Boolean expression is a question with only two possible answers: true or false, yes or no. Every if statement checks one. 42:07
- In C, a single equal sign means assignment, which stores a value in a variable. To check whether two things are equal, C uses two equal signs. 45:23
- Once a program has checked that x is not less than y and not greater than y, it already knows they are equal. So the extra third question is wasted work, and a plain else is enough. 47:13
- To change a variable's value, you can write counter = counter + 1. Read it as: take the current value, add 1, and store the result back in counter. C also has shorter ways to write this. 49:27
Words to know
- int
- Short for integer, a whole number with no decimal point, such as 0, 5, or -3.
- Boolean
- A question whose answer is only true or false, yes or no, or 1 or 0.
- format code
- A short code like %s or %i that tells printf what kind of value to print, such as text or an integer.
- curly braces
- The { and } symbols in C. They group several lines of code together so they belong to the same if, else, or other block.
Example
Think of a signpost at a crossroads that says: if the road is flooded, go left, otherwise go straight. The sign asks one yes-or-no question and sends you down one path. An if statement in C works the same way: it checks a Boolean question and runs one set of lines.
3 checked practice questions for this part are in the app.
Part 6 · 50:00–1:00:00
This part shows how a C program makes decisions by asking yes-or-no questions, and why ordering those questions with else if makes the program work better.
Key ideas
- In C, counter++ adds 1 to a number and counter-- subtracts 1. These are shorter ways to write what you could also write in longer forms, and they do the same thing. 50:00
- A program can ask a yes-or-no question, called a Boolean expression, and then run different code depending on whether the answer is true or false. The compare.c program asks whether x is less than y. 53:35
- Separate if statements ask every question every time, even when an earlier answer already settles the matter. An else if chain stops as soon as one question is answered yes. 57:08
- The else if version is correct and also better designed, because most of the time the program asks only one or two questions instead of three. 58:12
- When x is not less than y, the else branch should say x is not less than y, not x is greater than y, because the two numbers might be equal. 54:43
Words to know
- Boolean expression
- A question in code that has only two possible answers, true or false.
- control-flow diagram
- A drawing, like a flowchart, that shows the order in which a program asks its questions and does its steps.
- integer
- A whole number, such as 1, 2, or -5, with no fractions or decimals.
- compile
- To turn the code you wrote into a program the computer can run. The lesson does this with the make command.
Example
Imagine sorting mail. You first check whether a letter is for you. If it is, you stop there. If not, you check whether it is for your neighbor. You only ask the second question when the first answer was no, so you never waste time on questions you already know the answer to. That is the idea behind else if.
3 checked practice questions for this part are in the app.
Part 7 · 1:00:00–1:10:00
This part shows how C checks a single character typed by the user, how to write a yes-or-no check that accepts both capital and lowercase letters, and how to avoid repeating code.
Key ideas
- The CS50 library's get_int keeps asking until the user types a whole number, but plain C does not do this by default. C trusts the person at the keyboard to do what the program wants, which is one reason C code can be vulnerable to hacking. 1:00:21
- A string is zero or more characters of text and uses double quotes. A char is exactly one character and uses single quotes. The get_char function gets one character from the user. 1:01:26
- A program that checks for lowercase 'y' or 'n' will ignore capital letters, so you have to check for both. Doing this by copying the same lines is not good design, because you might change one copy and forget the other. 1:04:41
- The two vertical bars, written ||, mean 'or' in C. Using || lets you check two possible letters in one branch, so the message is written only once. 1:07:53
- The double ampersand, written &&, means 'and' in C. It does not fit this yes-or-no check, because a single character cannot be both uppercase and lowercase. 1:08:17
Words to know
- char
- A C data type that holds exactly one character, such as the letter y or the symbol ?.
- string
- A piece of text made of zero or more characters, written in double quotes in C.
- get_char
- A helper function from the CS50 library that asks the user to type one character and gives it back to the program.
- ||
- The C symbol for 'or.' It is true when at least one of the two checks is true.
Example
Think of a bouncer at a door with a list that says 'y' or 'Y' means yes. Instead of writing two separate rules that each let someone in, the bouncer writes one rule that says 'if the answer is y or Y, let them in.' This way the rule is written once and is easy to change later.
3 checked practice questions for this part are in the app.
Part 8 · 1:10:00–1:20:00
This part shows how to use a variable and a while loop in C to repeat an action, such as printing "meow" three times, instead of copying the same code over and over.
Key ideas
- A variable is a named storage spot for information. Here, a variable called counter is set to 3, and it keeps track of how many meows are left. 1:10:39
- A while loop checks a yes-or-no question before each pass through its code. As long as the answer is true, the code inside the curly braces keeps running. 1:11:22
- Each pass prints a meow and then subtracts 1 from counter. When counter reaches 0, the question becomes false and the loop stops. 1:12:27
- Copying and pasting the same code is a warning sign. A loop lets you write the code once, so a change only needs to be made in one place. 1:16:24
- Programmers have several correct ways to count, such as starting at 0 or 1, or using a for loop. Some are more conventional, but there is rarely only one right way. 1:19:43
Words to know
- variable
- A named box in the computer's memory that holds a piece of information, like a number.
- Boolean expression
- A question that can only be answered true or false, such as 'is counter greater than 0?'
- loop
- A piece of code that repeats again and again, checking a question each time to decide whether to keep going.
- decrement
- To make a number smaller by 1, like taking one finger down when counting backward.
Example
Think of holding up three fingers and saying 'meow' each time you put one finger down. You keep going only while you still have a finger up, and you stop when your hand is empty. That is how the while loop works: it checks the question, does the work, and checks again.
3 checked practice questions for this part are in the app.
Part 9 · 1:20:00–1:30:00
This part shows how a C for loop repeats code three parts at a time, how to make a loop run forever, and how to write your own function (meow) that must be defined before it is used.
Key ideas
- A for loop puts three things in its parentheses: a place to set up a counting variable, the condition that is checked each time, and the update that runs at the end of each pass. It does the same job as the earlier loop, but in a shorter form. 1:20:00
- The for loop runs in a fixed order. The counter i starts at 0 once, then the condition is checked, the loop body runs, i goes up by one, and the condition is checked again. When the condition becomes false, the loop stops. 1:20:00
- Curly braces can be left out when there is only one line inside the loop, but the lecturer keeps them in because someone may add a second line later and forget them. 1:20:00
- To make a loop run forever, put true in the condition spot, because true is always true. The lecturer notes that endless loops are usually not what you want in code. 1:20:00
- In C, you can make your own function, such as void meow(void). C reads the file from top to bottom, so the function must be defined above where it is used. If it is placed lower down, a copy of its first line with a semicolon, called a prototype, can go near the top to tell the compiler it exists. 1:20:00
Words to know
- initialize
- To give a variable its first starting value, such as setting i to 0 at the start of a loop.
- Boolean expression
- A question in code that can only be true or false, such as is i less than 3?
- prototype
- A short line that tells the compiler a function exists, including its name and return type, so it can be used before its full code appears.
- void
- A word meaning nothing. In void meow(void), the first void means the function gives nothing back, and the one in parentheses means it takes no inputs.
Example
Think of a shopkeeper who needs to serve exactly three customers. The shopkeeper starts a tally at zero, asks whether the tally is still below three, serves one customer, adds one to the tally, and then asks again. This is the same order a for loop follows.
3 checked practice questions for this part are in the app.
Part 10 · 1:30:00–1:40:00
This part shows how to give a C function inputs (arguments) and a returned answer, and why a variable can only be used inside the curly braces where it was made (scope).
Key ideas
- A function can take inputs called arguments. You name each one and tell C its type, such as int n, so the function can use that number in place of a fixed value like 3. 1:30:21
- Using an argument lets the function work for any number, so main can just ask for the meowing and tell it how many times, instead of repeating the loop itself. 1:31:31
- A function can hand back a value instead of only making something appear on the screen. For that, you change void to the type of value you want back, such as int, and use return. 1:36:37
- Scope means the place where a variable exists. A variable only lives inside the nearest set of curly braces, so x and y made in main cannot be used inside add. 1:37:42
- The fix is to pass the values into add as arguments. Then add works on its own inputs, and it does not need to see variables from main. 1:38:50
Words to know
- argument
- An input you hand to a function inside its parentheses, so the function can work with it.
- return value
- The value a function sends back to the place that called it, so that place can store it or use it.
- scope
- The part of a program where a variable can be used. A variable made inside curly braces usually cannot be used outside them.
- prototype
- A line near the top of a file that tells the compiler a function exists, what it returns, and what inputs it takes, before the full function appears later.
Example
Think of a kitchen with a recipe card that says 'make n cups of tea.' The number n is the argument you pass in, and the cook can make any amount you ask for. A cook in one room cannot grab the sugar bowl kept in another room unless someone hands it over, which is like scope: each variable only works where it was made.
3 checked practice questions for this part are in the app.
Part 11 · 1:40:00–1:50:00
This part explains how functions can hand back an answer, why main returns a number, and how to use typed commands in a Linux command-line window to work with files.
Key ideas
- A function can have a return value, which is an answer it hands back to the code that called it. The word int before the function name says that answer is a whole number. Inside the parentheses, int a and int b say the function takes two whole-number inputs. 1:40:00
- The names you give inputs are just labels. You can call them a and b, first and second, or anything else. Using the same names as your outer variables is allowed but confusing, so pick different names. 1:41:03
- You can skip storing a result in a variable and put one function call inside another. This works but can make code harder to read. Storing the result in a variable can make code easier to follow. 1:41:45
- The main function always returns a number. By default it returns 0, which means the program ran without problems. Other numbers can signal that something went wrong. 1:43:31
- Linux is an operating system that is often used with a text-only command-line interface. You type short commands instead of clicking. For example, mv renames or moves a file, and ls lists the files in the current folder. The same change can be made with a mouse or with typed commands. 1:48:35
Words to know
- return value
- The answer a function gives back when it finishes. Think of it as a slip of paper with the result written on it.
- argument
- A value you pass into a function when you call it. The function gives each one a name inside its own code, like a and b.
- command-line interface (CLI)
- A way of using a computer by typing text commands into a window, instead of clicking on menus and buttons.
- Linux
- A popular operating system, the main software that starts up when a computer turns on and runs everything else. It is often used on servers.
Example
Think of ordering a coffee. You give the barista your order, which is like the inputs to a function, and they hand you back a receipt with the total on it, which is like the return value. The receipt tells you the answer, and you can then use it to pay.
3 checked practice questions for this part are in the app.
Part 12 · 1:50:00–2:00:00
David Malan shows how to draw Mario-style blocks in C by printing one character at a time, using loops and a newline in the right place, and he also covers a few basic terminal commands.
Key ideas
- The rm command deletes a file. It asks you to confirm with y or n, and once a file is deleted there is no easy way to get it back. 1:50:42
- The cd command changes folder, and the prompt shows the name of the folder you are in, so you always know where you are. 1:51:45
- A loop lets the program repeat the same print step many times, so you do not have to write each step by hand. Four question marks can come from one printf inside a loop that runs four times. 1:54:00
- The backslash n moves the output to a new line. If you put it inside the loop, every block ends up on its own line. Putting it after the loop makes the blocks sit side by side, with one line break at the end. 1:55:08
- For a 3-by-3 grid, one loop handles the rows going down and a second loop, inside the first, prints the blocks across each row. Typing 3 over and over in the code makes it hard to change the size later, which is a sign of magic numbers. 1:59:44
Words to know
- loop
- A part of a program that repeats the same steps again and again, until a counting condition says to stop.
- executable
- A file the computer can run as a program. In the terminal, David marks these with an asterisk.
- printf
- A C command that prints text to the screen. It can also print a newline when you add backslash n.
- magic numbers
- Plain numbers typed directly into code, like 3, with no name to explain them. They make code hard to change later.
Example
Think of seats in a classroom with rows and columns. You fill one row from left to right, then move to the next row, and keep going until every seat is filled. The outer loop is like moving down the rows, and the inner loop is like filling each seat in a row.
3 checked practice questions for this part are in the app.
Part 13 · 2:00:00–2:10:00
This part shows how to replace repeated numbers in C code with one named variable, how to lock a value so it can't change by accident, how to keep asking for input until it is valid, how comments work, and why computer memory is limited.
Key ideas
- Typing the same number in several places is risky, because you might change one and forget the other. Storing the number in one variable, n, means you only change it in one place. 2:00:00
- A constant is a value marked so it cannot be changed by accident. This protects the value from you or from a teammate who edits the code later. 2:00:40
- A do while loop runs its block of code first and then checks a condition. If the number is less than 1, it asks again, so the program only moves on once it gets a valid positive number. 2:06:30
- A comment starts with // and is a note for people reading the code. The computer skips comments completely. 2:07:13
- Computer memory is limited, so a counter can only go up so high. With only three bits, counting past 7 wraps back around to 0. 2:09:47
Words to know
- magic number
- A number that appears in the code with no clear label, such as a 3 written in the middle of the code. It is hard to know what it means and easy to change in one place but forget in another.
- constant
- A named value that is marked so it cannot be changed by accident later in the code.
- do while loop
- A loop that runs its block of code once first, then keeps repeating it as long as a condition stays true.
- scope
- The part of the code where a variable can be used. A variable declared inside a set of curly braces usually cannot be used outside those braces.
Example
Think of a classroom where the number of seats is written on one sign at the door instead of on every chair. If the room changes size, you only update the sign, and everything else follows. This is the same idea as storing the brick wall size in one variable n.
3 checked practice questions for this part are in the app.
Part 14 · 2:10:00–2:20:00
This part explains why computers can get numbers wrong, through integer overflow, the limits of bits, and rounding when dividing.
Key ideas
- A computer stores numbers in a fixed number of bits. If a number counts higher than those bits can hold, the count is called integer overflow and it wraps around to 0 or to a negative number. 2:10:00
- Using 32 bits, you can store about 4 billion different values if you only use positive numbers. If you also need negative numbers, you can only count about half as high in each direction. 2:10:00
- When you divide one integer by another integer, C throws away the fraction, so 1 divided by 3 gives 0. This is called truncation. 2:10:00
- Type casting lets you treat an integer as a float before dividing, so the fraction is kept. Even then, computers have limited memory, so some numbers like 1/3 can only be stored as the closest value they can hold. 2:10:00
Words to know
- integer overflow
- When a whole number gets too big for the space set aside for it, so it wraps around to 0 or to a negative number.
- bit
- The smallest unit of computer storage. It holds either a 0 or a 1.
- truncation
- Cutting off the part of a number after the decimal point and throwing it away, instead of keeping it.
- type casting
- Telling C to treat a value as a different type, such as treating an integer as a float, so the math keeps the decimal part.
Example
Think of a car odometer with only six digits. Once it reaches 999999, the next mile makes it roll back to 000000. That is like integer overflow: the number runs out of room and wraps around to the start.
3 checked practice questions for this part are in the app.
Part 15 · 2:20:00–2:27:06
This part explains why computers can make mistakes with numbers, using real examples like the Y2K scare, the 2038 time problem, Pac-Man, Donkey Kong, and a Boeing 787 software bug.
Key ideas
- Computers have limited memory, so they cannot store every real number exactly. This causes floating point imprecision, where answers come out slightly rounded. 2:20:00
- Early software stored years with only two digits to save expensive memory. That caused fears that 1999 might be read as 1900 when the year 2000 arrived. 2:20:00
- Many clocks count seconds since January 1, 1970 using a 32-bit integer. That counter runs out of room around 2038 and wraps around to zero or a negative number, which can break things. 2:20:00
- Old games like Pac-Man and Donkey Kong hit the same kind of problem. A number grew past the largest value its memory could hold, so it wrapped around and caused strange behavior. 2:20:00
- A Boeing 787 software counter could overflow after 248 days of continuous power, which is a real safety risk. Turning the plane off and on reset the memory and fixed it for a while. 2:20:00
Words to know
- integer overflow
- When a number gets bigger than the space set aside for it, so it wraps back around to a small or negative value, like an odometer rolling over.
- floating point imprecision
- The fact that computers can only store a limited set of decimal numbers, so many numbers are stored with small rounding errors.
- epoch
- A fixed starting date that a computer counts time from. Many systems count seconds from January 1, 1970.
- bit
- The smallest unit of computer memory, holding a single 0 or 1. A 32-bit number uses 32 of these to store a value.
Example
Think of a car odometer that only has five digits. Once it reaches 99999, the next mile shows 00000, so the number has wrapped around even though the car kept driving. Computers have the same limit: once a counter hits its maximum, it wraps back to zero or to a negative number.
3 checked practice questions for this part are in the app.
Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. Harvard CS50 · CC BY-NC-SA 4.0 · not affiliated with or endorsed by Harvard. These notes are shared under the same license.
LessonBound · Free lecture notes · Privacy · Terms · New York