CS50 Lecture 1 — C
Move from visual blocks to typed code. Learn variables, types, conditionals, and loops in C — the language that powers most of the software world.
From Scratch to C
Last week you saw programming concepts as colorful blocks in Scratch. This week, those same concepts are written as text in C — one of the most important programming languages ever created.
You won't use C in your daily work. So why learn it? Because C shows you exactly what happens inside the computer. Python and JavaScript hide the complexity; C exposes it. Understanding C makes you better at every other language.
Your First C Program
#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
return 0;
}
Let's break this down:
#include <stdio.h>— tells C to load the "standard input/output" library (so you can useprintf)int main(void)— every C program starts running from a function calledmainprintf("Hello, world\n")— prints text. The\nmeans "new line"return 0— tells the system "I finished successfully" (0 = no error)- Every statement ends with a semicolon
; - Code blocks use curly braces
{}(not indentation like Python)
Variables and Types
In Python, you just write x = 5. In C, you must declare the type first:
int age = 29; // integer (whole number)
float price = 19.99; // decimal number
char grade = 'A'; // single character
char name[] = "Lia"; // string (array of characters)
Why does C make you specify types? Because it needs to know exactly how much memory to reserve. An int takes 4 bytes, a float takes 4 bytes, a char takes 1 byte. This is the reality that Python hides from you.
Conditionals
Same concept as Scratch and Python, different syntax:
if (deal_value >= 100000)
{
printf("Major deal\n");
}
else if (deal_value >= 50000)
{
printf("Significant deal\n");
}
else
{
printf("Standard deal\n");
}
Notice: conditions must be in parentheses (), and blocks use curly braces {}.
Loops
// while loop
int i = 0;
while (i < 5)
{
printf("Count: %i\n", i);
i++; // i++ means i = i + 1
}
// for loop (most common in C)
for (int j = 0; j < 5; j++)
{
printf("Step: %i\n", j);
}
The for loop packs three things into one line:
int j = 0— initializej < 5— condition (keep going while true)j++— update after each iteration
Compiling vs. Interpreting
Here's a crucial difference from Python:
Python is interpreted: You write code, run it directly. Python reads your code line by line and executes it. Quick to test, but slower to run.
C is compiled: Before running, a special program (compiler) translates your entire source code into machine code (binary) that the processor can execute directly. Slower to develop, but much faster to run.
Source code (.c) → [Compiler] → Machine code (binary) → [Processor] → Output
When you build a Next.js project and run npm run build, a similar compilation step happens — your TypeScript/JSX gets transformed into optimized JavaScript. Now you know why that step exists.
printf and Format Codes
C's printf uses special codes to insert values into strings:
int age = 29;
float gpa = 3.8;
char name[] = "Lia";
printf("Name: %s, Age: %i, GPA: %.1f\n", name, age, gpa);
// Output: Name: Lia, Age: 29, GPA: 3.8
%s= string%ior%d= integer%f= float (%.1f= 1 decimal place)%c= character
Python's f-strings (f"Hello {name}") are much nicer. But understanding format codes helps you read logs, error messages, and documentation in many languages.
Key Takeaway
C is harder than Python. That's the point. It forces you to think about things Python handles automatically: memory, types, compilation. This deeper understanding makes you a better programmer in every language.
What to Do This Week
- Watch CS50 Lecture 1 (link above)
- You don't need to install C or write C code locally (CS50 provides an online environment). But watch the lecture carefully.
- Focus on understanding the concepts: types, compilation, how C differs from Python.
- Take the quiz below.
Quiz
What does `#include <stdio.h>` do in C?