Lecture notes · CS50: Programming in C · Lecture 2

Arrays

Harvard CS50 Introduction to Computer Science, lecture 2, 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 explains that the make command runs a compiler called Clang behind the scenes, and shows what happens when you run that compiler directly.

Key ideas

Words to know

compiler
A program that converts source code into machine code that a computer can run.
source code
The text you write in a programming language, such as the hello.c file.
machine code
The 0s and 1s that a computer reads and runs directly.
Clang
The specific compiler program used in this course to turn C code into machine code.

Example

Think of a compiler like a translator who turns a letter written in English into one written in another language that a different reader understands. make is like a helper who tells the translator when to start the job, so you do not have to do it yourself. This comparison is only an analogy to help the idea click; the lesson does not use it.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains how the clang compiler turns your C code into a program, including the extra typed options it needs and the first of its four steps, called preprocessing.

Key ideas

Words to know

command line arguments
Extra words or short codes you type after a command at the command prompt to change how a program is built or runs.
library
Code that someone else wrote, which you can use in your own project. CS50 provides one for get_string.
prototype
A short line that tells the compiler a function's name, what it returns, and what inputs it takes, so the compiler knows the function exists before it sees the full code.
preprocessor directive
A line that starts with a hash symbol, such as an include line. It gives the preprocessing step an instruction before the main compiling happens.

Example

Think of writing a letter that says 'see the address card on page 1.' Before you mail it, someone copies the address card into the letter so the reader has everything in one place. Your original notes stay the same, and only the copy that gets sent is changed.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part explains the four steps that turn C source code into a finished program, and why finding and fixing bugs is a normal part of programming.

Key ideas

Words to know

assembly language
A low-level language that is written in short commands close to what the computer hardware actually does. It is much easier to read than 0's and 1's, but still hard for most people to write.
assembling
The step that turns assembly language into 0's and 1's, which the computer can run directly.
linking
The final step that joins several compiled pieces of a program into one finished file.
compiler
A program that translates source code written by a person into a form the computer can run. Clang is the compiler used in this course.

Example

Think of a recipe written in a cookbook as your source code. First you turn it into a shorter set of steps for the kitchen, then into a list of exact actions a cook can follow, then you combine it with other recipes into one menu. Each stage makes the instructions closer to what the cook can actually do. (This comparison is a teaching aid and is not from the lesson.)

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part shows how to find a bug, a mistake that compiles but makes the program behave wrongly, using printf, a helper function, and a debugger.

Key ideas

Words to know

bug
A mistake in code that lets the program run but makes it do the wrong thing.
printf
A C command that prints text and values to the screen, so you can see what the program is doing while it runs.
breakpoint
A marked line where the debugger pauses the program so you can look around before it continues.
debugger
A tool that runs your program step by step and lets you pause it and inspect its values to find mistakes.

Example

Think of a recipe that you check by tasting it as you cook, not only at the end. If the dish tastes wrong, you can see at which step things went off track. A debugger works the same way by letting you stop and check the program at each step.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

This part covers how to find bugs with a debugger, why rubber duck debugging helps, and how C stores different kinds of data in fixed amounts of memory.

Key ideas

Words to know

garbage value
Leftover, meaningless data that was in memory before your program ran. It is not a real value you chose.
step over
A debugger button that runs the current line without stopping to look inside any function it calls.
bit
The smallest piece of computer storage. It holds either a 0 or a 1.
rubber duck debugging
Finding bugs by explaining your code out loud, line by line, to an object such as a rubber duck. Saying it aloud often shows you where your thinking went wrong.

Example

Imagine a shared whiteboard in an office that still has someone else's old notes on it. You cannot trust those notes to be correct, so you erase them and write your own before you use the board. A variable with a garbage value is like that whiteboard before you write on it.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–1:00:00

This part explains how a computer stores information in memory as numbered bytes, and why an array is a better way to keep a list of related values like test scores.

Key ideas

Words to know

byte
A small unit of computer storage made of 8 bits. Each byte holds one small piece of information.
address
A number that says where a byte sits in memory, like a house number on a street of storage spots.
array
A row of values stored one after another in memory, all under one name and counted by position starting at 0.
cast
Telling the computer to treat a value as a different type, such as turning an int into a float.

Example

Think of an array like a row of mailboxes in an apartment building, all numbered in order and placed side by side. One name, like the building's name, covers the whole row, and you find each item by its box number. Three separate variables would be like three loose boxes with different names that you have to track one by one.

3 checked practice questions for this part are in the app.

Part 7 · 1:00:00–1:10:00

This part shows how an array stores several values under one name, how to reach each value by its number, and how a loop can fill the array without repeated code.

Key ideas

Words to know

array
A group of values of the same type, stored one after another in memory and shared under one name.
index
The number that tells you which spot in an array you want, such as 0 for the first spot. To index into an array means to go to a specific spot.
constant
A value that is set once and should not be changed later. Programmers often write its name in capital letters so it stands out.
global variable
A variable declared at the top of a file, outside of any function, so that every function in the file can use it.

Example

Imagine an apartment building with mailboxes numbered 0, 1, and 2, but no sign on the door telling you how many boxes there are. If you try to open box 3, you may find nothing there or hit a wall, and nothing warns you ahead of time. In C, you have to remember how many boxes there are yourself.

3 checked practice questions for this part are in the app.

Part 8 · 1:10:00–1:20:00

This part shows how arrays are passed into functions, how a loop adds up an array to compute an average, and how characters and strings are really just numbers stored back to back.

Key ideas

Words to know

accumulator
A variable that builds up a total as a loop runs, like a running total on a receipt.
cast
Telling the computer to treat a value as a different type, such as turning an int into a float before dividing.
char
A type that holds a single character, such as a letter or a symbol, written in single quotes like 'H'.
string
A row of characters stored one after another, such as a word or a sentence.

Example

Think of an array as a row of numbered mailboxes. To add up everything in the boxes, you need to know the row itself and how many boxes there are, then you visit each box in order and keep a running total. A string works the same way, with each mailbox holding one letter.

3 checked practice questions for this part are in the app.

Part 9 · 1:20:00–1:30:00

This part explains how C knows where a string ends: every string is a row of characters followed by a special zero byte that marks the end.

Key ideas

Words to know

sentinel value
A special marker value that means something other than normal data. Here, it is the all-zero byte that tells the computer a string stops at that point.
NUL
The name for the all-zero byte that ends a string. It is written as \0 in C, and it is not the number zero shown on the screen.
byte
A small unit of computer memory. A char takes one byte, and the lesson says a char is 1 byte no matter what.
array
A row of values of the same kind stored one after another in memory, each reached by an index number in square brackets.

Example

Think of a row of mailboxes, one letter per box, with an empty box at the end that holds a special closing mark. A mail carrier reads boxes one by one and stops when they reach the closing mark, so they never read the empty boxes past the end. A C string works the same way: the zero byte is the closing mark that tells the computer to stop reading.

3 checked practice questions for this part are in the app.

Part 10 · 1:30:00–1:40:00

This part shows how C arrays and strings work in memory, how to count the characters in a string, and how to use a helper function and the string library.

Key ideas

Words to know

array
A list of values of the same type, stored one after another in memory and reached by position number.
string
A row of characters, such as the letters of a word, stored one after another.
null character
A special invisible character, written as backslash 0, that marks the end of a string.
scope
The part of a program where a variable can be used. A variable made inside main can only be used inside main.

Example

Think of a row of numbered mailboxes, where each box holds one letter. An empty box at the end says the message is finished, just like the null character ends a string. To count the letters, you open boxes one by one from the start until you reach that empty box.

3 checked practice questions for this part are in the app.

Part 11 · 1:40:00–1:50:00

This part shows how to walk through every character of a string with a loop, how to make that loop more efficient, and how to change lowercase letters to uppercase in two ways.

Key ideas

Words to know

ASCII
A standard chart that gives each letter, digit, and symbol a number. For example, lowercase a is 97 and uppercase A is 65.
strlen
A function that counts how many characters are in a string, not counting the end marker.
%c
A format code that tells printf to print one single character.
toupper
A function from the ctype library that returns the uppercase version of a letter. It leaves other characters alone.

Example

Think of a row of mailboxes, numbered from 0 to 4. To check each one, you walk down the row by number, and you know the row has 5 boxes, so you do not need to count them again on every step. Storing the count once before you start is like writing the number on a sticky note instead of recounting the boxes each time.

3 checked practice questions for this part are in the app.

Part 12 · 1:50:00–2:00:00

This part shows how a C program can read words typed on the command line when it starts, so it does not have to stop and ask the user for input.

Key ideas

Words to know

command line argument
An extra word you type after a program's name at the prompt, which the program can read and use as input.
argc
A whole number that tells the program how many words were typed at the prompt, including the program's name.
argv
A list (array) of the words typed at the prompt. Each item is a string, and you pick one by its position number.
flag
A command line argument that starts with a dash and changes how a program works, like cowsay -f for a different picture.

Example

Think of ordering at a counter by saying your whole order in one sentence, such as 'one coffee, no sugar.' You do not wait for the clerk to ask you each question. A program that reads command line arguments works the same way: the user types everything at once, and the program uses the words right away.

3 checked practice questions for this part are in the app.

Part 13 · 2:00:00–2:11:33

This part explains how programs report success or failure with a hidden exit number, and introduces cryptography, the art of scrambling a message so only the intended reader can read it.

Key ideas

Words to know

exit status
A hidden number a program returns when it finishes. 0 means success, and any other number means something went wrong.
encryption
Scrambling information so that only the intended person can read it.
cipher
A set of steps, or algorithm, for scrambling a message in a way that can be undone later.
key
A secret value, usually a number, that the sender and receiver both know and that controls how the message is scrambled and unscrambled.

Example

The lesson compares a key to a key that opens a lock. In the simple shift cipher with a key of 1, each letter moves forward by one, so HI! becomes IJ!. The receiver knows the key is 1, so they move each letter back by one to get HI! again.

3 checked practice questions for this part are in the app.

← Week 1: C · Week 3: Algorithms →

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