CS50 Week 1 Explained: Your First Steps in C
Week 1 is where the blocks come off. Everything you did in Scratch (functions, conditionals, loops, variables) comes back in C, a language from the 1970s that still runs much of the world's infrastructure. C is strict and unforgiving about details, which is exactly why CS50 starts with it: you see what the computer is really doing.

Jump to the key moments
- 0:43Source code
- 14:33From Scratch to C
- 31:01Hello, you
- 44:03Terminal commands
- 55:49Conditionals
- 1:02:55Types
- 1:22:24Loops
- 1:44:46Functions
- 1:56:21Correctness, design, style
- 2:02:48Mario
- 2:18:32Integer overflow
- 2:26:47Floating-point imprecision
Source code, machine code, and the compiler
You write source code that people can read. The computer runs machine code, which is just bits. A compiler translates one into the other. In CS50's environment you type make hello to compile hello.c into a program called hello, then ./hello to run it. Change the code and you have to compile again. Forgetting to recompile is the most common Week 1 mistake.
The Scratch-to-C translation
The lecture maps each Scratch block to its C equivalent, and it's worth pausing on:
- "say" becomes
printf, which prints text.\nmeans "new line", and placeholders like%s(a string) and%i(an integer) get filled in with values. - "ask and wait" becomes CS50's own helpers, such as
get_stringandget_int, which come from a library you include at the top of your file. - "if" becomes
if (...) { }, withelse ifandelse. - "repeat" and "forever" become
forandwhileloops, plusdo ... while, which runs at least once. That makes it ideal for asking again until the user types something valid.
Types matter in C
In C you must declare what kind of value each variable holds: int for whole numbers, float and double for decimals, char for a single character, bool for true or false, long for bigger whole numbers, and CS50's string for text. Types come with limits, and the end of the lecture shows what happens when you hit them:
- Integer overflow: an
inthas a fixed number of bits, so counting past its maximum wraps around to a negative number. The lecture's real-world examples include Pac-Man's broken level 256 and a Boeing 787 that needed rebooting before a counter overflowed. - Truncation: dividing one integer by another throws away the decimal part, so 1 divided by 2 is 0.
- Floating-point imprecision: some decimals can't be stored exactly in binary, so 0.1 is really "very close to 0.1". This matters for money, and it's a clue for the Cash problem.
Correctness, design and style
CS50 grades on three things, and this lecture explains them. Correctness: does it work? Design: is it well written, without repetition or wasted effort? Style: is it readable, with consistent indentation and clear names? A program can be correct and still badly designed. If you've copied and pasted the same five lines three times, that's a sign you need a loop or a function.
Problem Set 1: what it asks
- Hello, World gets your environment working. You complete it, but don't submit it.
- Hello, It's Me: ask for a name and greet the user.
- One of Mario (less comfortable), which prints a right-aligned half-pyramid of
#, or Mario (more comfortable), which prints two pyramids with a gap between them. - One of Cash, which works out the fewest coins to give as change, or Credit, which checks whether a card number is valid and identifies the card type.
Where people get stuck
- Mario: don't write any code at first. Draw a table with one row per pyramid line and three columns: the row number, how many spaces, and how many hashes. Once you can see the pattern in the table, the loops almost write themselves. A loop inside a loop is normal here.
- Cash: the problem works in cents, not dollars, so you never touch decimals. Taking the biggest coin first, as often as you can, is the greedy approach the problem is built around.
- Credit: card numbers don't fit in an
int, so think about which type does. Getting digits one at a time comes down to two operations: the remainder when you divide by 10, and dividing by 10. Work the checksum through by hand on paper for one test number before writing any code. - "It compiles but prints nothing": check that you're running the file you just compiled, in the folder you think you're in.
lsandcdare covered in the "Terminal commands" chapter.
Check yourself
What's the difference between a while loop and a do-while loop?
A while loop checks its condition first, so it might never run. A do-while runs once, then checks. That makes it the natural choice for "ask until the answer is valid".
Why might 7 / 2 give you 3 in C?
Both numbers are integers, so C does integer division and throws away the remainder. If one of them is a float or double, you get 3.5.
You changed your code but the program still behaves the old way. What did you forget?
To recompile with make. The program you're running is the old compiled version.
Your program is correct but you pasted the same block of code three times. Which grading axis suffers?
Design. Repeated code belongs in a loop or a function.
Want to see how AI writes code compared with a human? Watch Build Lab put a website together live.
Try Build Lab →