Lecture notes · CS50: Programming in C · Lecture 1

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 free

Watch 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

Week 2: Arrays →

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