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 freeWatch 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
- A compiler is a program that turns your source code into machine code, the 0s and 1s a computer can run. 6:18
- make is not a compiler itself. It is a program that runs a compiler for you. 7:45
- The compiler the course has been using is called Clang, which stands for C language. 8:49
- If you run Clang directly on hello.c, it does not name the program hello. It creates a file called a.out by default. 9:12
- a.out is a poor name for a program, so the lesson asks how to get a better result. 9:56
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
- Command line arguments are extra words you type after a command to change how a program behaves. Earlier, you used them without knowing that's what they were called. 10:00
- The -o option gives the program you build a name, such as hello, instead of the default name a.out. If your code uses a third-party library, you also have to tell the compiler about it with -l cs50. 13:15
- Make is a tool that types these long commands for you. Compiling is actually four steps, and the first one is preprocessing. 13:40
- Preprocessing handles lines that start with a hash symbol, such as include lines. It copies the contents of each included file, like the prototype for get_string, into your code before the rest of the compiling happens. 18:05
- Preprocessing happens in the computer's memory. Your hello.c file does not change. 19:29
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
- Turning C code into a program that runs takes four steps: preprocessing, compiling, assembling, and linking. Everyday people often just call the whole process compiling. 23:43
- Compiling converts your C code into assembly language, a low-level language that is still not machine code. Assembly language is very close to the machine's own instructions. 24:29
- Assembling takes that assembly code and converts it into 0's and 1's, which is the machine code the computer can run. 21:06
- Linking combines the 0's and 1's from several separately compiled files, such as your own code and the staff's helper files, into one final program file. 23:22
- Bugs are a normal part of programming, even for experienced programmers. Admiral Grace Hopper is known for popularizing the word 'bug' for a mistake in a computer program. 28:52
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
- A program can compile with no syntax errors and still give the wrong answer. Here, the program printed four bricks when it should have printed three. 30:22
- printf is a quick way to see what is happening inside the program. Printing the value of i on each loop showed that the loop ran one time too many. 32:06
- The usual fix was to change the less-than-or-equal sign to a less-than sign, so the loop stops before the extra step. 33:14
- Adding printf line after line turns into guessing, and each change means recompiling and rerunning. A debugger is a better tool for that. 33:57
- A debugger, started with debug50, pauses the program at a breakpoint so you can look at its variables before it runs further. Variables that have not been given a value can hold leftover numbers, like the odd value shown for h. 38:20
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
- A variable you never gave a value holds a garbage value, which is leftover junk from memory. You cannot trust it until you set it yourself. 40:00
- In a debugger, step over runs a line without looking inside it, and step into lets you walk through the code inside a function. 40:39
- Stepping through a loop one line at a time lets you watch the counter change. This showed that the condition less than or equal to was wrong and should be less than. 43:35
- A debugger is better than printf for finding bugs, even though printf is quick. Learning the debugger early saves a lot of time later. 44:40
- Each data type has a fixed amount of space, such as 4 bytes for an integer. A bool uses a whole byte even though only one bit is really needed. 48:29
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
- Computer memory works like a grid of bytes, and each byte has a number, called an address, that identifies it. A byte is a small chunk of storage made of 8 bits (tiny on/off switches). 50:43
- A value takes up as many bytes as its type needs. On this system, an int (a whole number) takes 4 bytes. 51:03
- Dividing whole numbers with whole-number math throws away any part after the decimal point. Using one float (a number with a decimal point) in the math keeps the decimal part. 53:59
- Separate variables like score1, score2, and score3 get messy as the number of scores grows. An array stores a fixed number of values back to back in memory under one name. 57:52
- To make an array, you write the type, a name, and a size in square brackets, such as scores[3]. You then fill its spots with scores[0], scores[1], and scores[2]. 58:14
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
- In C, array positions are numbered starting at 0, so the first, second, and third elements are scores[0], scores[1], and scores[2]. Counting from 0 is just how arrays work. 1:00:00
- Going past the last position, such as scores[3] in an array of 3, goes beyond the memory the array was given. This can crash the program or cause strange behavior. 1:00:21
- A loop can ask for each score and store it at index i, where i goes up by one each time. This removes the repeated lines and is better design. 1:02:33
- C does not tell you how long an array is, so you have to keep track of its size yourself. Storing the size in a constant, and capitalizing its name by convention, helps avoid mistakes. 1:05:22
- Because C does not check positions for you, the programmer has to make sure every index stays inside the array. 1:08:15
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
- To write an average function that works on an array, you must pass in two things: the array itself and how many numbers it holds. The function cannot work out the count on its own. 1:10:40
- The average function uses an accumulator, a variable called sum that starts at 0. The loop adds each element to sum, and then sum is divided by the length, cast to a float, so the division keeps the fraction. 1:12:06
- When a function takes an array, you write square brackets after the parameter name and do not need to give its size. You still need to know the length somehow, which is why it is passed in separately. 1:13:14
- A char and an int are both just numbers in memory. Printing with %c shows the letter, and printing with %i shows the number, such as 72 for H. The computer does not change the stored bits, only how they are shown. 1:15:53
- A string is really an array of chars stored one after another. The string s is one variable, and s[0], s[1], and s[2] reach the H, the I, and the exclamation point, even though you never listed the length. 1:19:13
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
- In C, a string is an array of characters, and each character takes one byte of memory. You can read or change a single character using square brackets, the same way you would with any array. 1:20:00
- The computer knows where a string ends because of a special sentinel value, a byte with all 0 bits, stored at the end. When a loop reaches that byte, it stops. 1:21:06
- Because of that extra end byte, a string with n visible characters actually takes n + 1 bytes. For example, "HI!" uses 4 bytes: three for the characters and one for the zero at the end. 1:21:28
- A list of strings works like a two-dimensional array. The first index picks which word you want, and the second index picks a character inside that word. 1:28:19
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
- You can read and change individual characters in an array by using square brackets and a position number. Nothing is hidden from you, so you control what happens in memory. 1:30:41
- In C, an array should hold values of only one type. Mixing types is possible but much harder than in friendlier languages like Python. 1:31:01
- A string is a row of characters that ends with a special null character. Your code can use that null character to find where the string stops. 1:33:56
- A variable can only be used inside the function where it was created. A helper function must use its own input, which is why the name variable caused an error inside string_length. 1:37:06
- The string library, included with string.h, has a ready-made function called strlen that finds a string's length. You can use it instead of writing the counting loop yourself. 1:37:51
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
- A loop can visit each character in a string one at a time, from position 0 up to the string's length. Inside the loop, %c prints one character. 1:40:19
- Calling strlen in the loop's middle check runs it again on every pass, even though the length never changes. Storing the length once before the loop, or in the start part of the loop, avoids that repeated work. 1:41:58
- In the ASCII chart, each lowercase letter is 32 more than its matching uppercase letter. You can use that gap to turn a lowercase letter into uppercase by subtracting 32. 1:46:33
- The toupper function from the ctype library does the uppercase change for you. If the character is already uppercase, toupper returns it unchanged, so you do not need an extra if check. 1:49:09
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
- A command line argument is a word typed after the program's name at the prompt. Programs can use these words as input, so the user does not need to be asked again. 1:50:38
- Instead of void inside main's parentheses, you can write int argc and string argv[]. argc is the number of words typed, and argv is an array holding those words. 1:51:23
- argv[0] is always the name of the program, and argv[1] is the first word the user typed after it. 1:54:56
- Before using argv[1], check that argc equals 2. If the user typed a different number of words, the program can print a default message instead of showing bogus output. 1:56:07
- A flag is a command line argument that starts with a dash and changes how a program behaves. You usually learn about flags from the documentation or a demonstration. 1:59:01
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
- Every program has an exit status, a hidden number it hands back when it finishes. By default it is 0, which means everything went OK. 2:00:00
- Any other number, such as 1, signals that something went wrong. The programmer chooses which number to return, and the user usually never sees it. 2:03:21
- You can see the hidden number by typing echo $? at the prompt right after a program runs. 2:04:24
- Cryptography scrambles a message so that only the intended recipient can read it. The original message is plaintext, and the scrambled version is ciphertext. 2:06:12
- A cipher is the method used to scramble and unscramble a message. Most ciphers also use a key, a secret number that sender and receiver agree on in advance. 2:07:14
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