Before class · do this at home
Watch, read, then come
CS50x 2026 · Week 1: C by David J. Malan. Watch the lecture on YouTube (you are also enrolled on edX), read the notes, skim the slides. Our lecture builds on it instead of repeating it.
- Make sure Lab 1 is pushed and you saw your score under Releases. If not, fix that first; the loop is the same every week.
- Open cs50.dev once, sign in with GitHub, and run the lecture's
hello.cyourself:code hello.c,make hello,./hello. - Accept the invitation to the FiratUniversity-IJDP-SoftEng GitHub organization (arrives by email this week) and sign in once at classroom50.org. From Lab 2 on, labs are accepted and graded there.
Learning objectives
By the end of this week you can:
- 1 Explain what a compiler does and run the edit → compile → run cycle in the terminal.
- 2 Use int, float, double, char, bool and string, and choose printf format codes (%i, %f, %c, %s).
- 3 Get input with the CS50 library (get_int, get_string) and validate it with a do-while loop.
- 4 Write conditions with if / else if / else and the comparison and logical operators.
- 5 Write while and for loops, and know when to use each.
- 6 Define and call your own functions with parameters and return values, and declare prototypes.
- 7 Explain integer overflow and floating-point imprecision, and why they caused real disasters.
★ the wow moment
The Boeing 787 had to be rebooted every 248 days because an integer ran out of room.
In 2015 the FAA ordered airlines to power-cycle every 787 at least every 248 days: a 32-bit counter in the generator control units, counting in hundredths of a second, would overflow after 2³¹ ticks and shut down all four generators mid-flight. 2³¹ hundredths of a second is 248.55 days. Today you learn what an int is; by the end of the lab you will make one overflow on purpose.
From blocks to text
Last week every idea had a block: say, repeat, if, a variable, a custom block. This week the same ideas have a spelling. The cost of text is real: you can now make a typo, and the compiler will tell you about every single one. The gain is bigger: you can write anything, the result runs on every computer on Earth, and the language you learn, C, is the one under Linux, Windows, macOS, Python itself and most of the code in your car.
All the programs on this page are the ones from CS50’s Week 1 lecture. Each orange box is a program we open and run together in VS Code; each has a download link, and the whole set is in src1.zip. The lab afterwards, Lab 2: C Kickstart, asks you to write your own versions.
hello, world
// A program that says hello to the world
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
} | Piece | Meaning |
|---|---|
#include <stdio.h> | Use the standard input/output library, which is where printf lives. The C equivalent of adding a Scratch extension. |
int main(void) | The program starts here. The green flag. |
{ … } | A block: the statements that belong to main. |
printf("hello, world\n"); | Call the function printf with one argument. \n is a newline, like pressing Enter. The semicolon ends the statement. |
Almost every C program you read this term has this skeleton. The interesting part is always what goes between the braces.
Source code becomes machine code
The processor understands only bits, so the text you write has to be translated. That is the job of the compiler. In the terminal, three commands you will type a thousand times this term:
$ code hello.c$ make hello$ ./hellohello, world code hello.c opens (or creates) the file in the editor. make hello compiles it: it reads hello.c and produces a program called hello. ./hello runs that program; the ./ means “the one in this folder”. Edit, compile, run. When the compiler complains, read the first error only, fix it, compile again. Later errors are very often echoes of the first one.
make is itself a convenience. Behind it, a real compiler is being called: on cs50.dev it is clang, in our lab repositories it is gcc, and the command that actually runs is something like gcc -o hello hello.c. Same idea, two brands.
Input: get_string and printf’s placeholders
A program that only prints is a poster. Programs become useful when they take input. CS50 gives us a small library, cs50.h, with functions that ask the user for a value and hand it back: get_string, get_int, get_float, get_long, get_char. In the lab you get the same library as two files, cs50.h and cs50.c, so everything here compiles wherever you are.
// get_string and printf with incorrect placeholder
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string answer = get_string("What's your name? ");
printf("hello, answer\n");
} // get_string and printf with %s
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string answer = get_string("What's your name? ");
printf("hello, %s\n", answer);
} The line string answer = get_string(...) does three things at once: it declares a variable called answer, it says its type is string, and it assigns the return value of get_string to it. The single = is assignment, not equality: “put this value in that box”. Equality is ==, and confusing the two is the most classic bug in C.
Types
Scratch did not care whether a variable held a number or a word. C does, because different kinds of values take different amounts of memory, and the compiler wants to know in advance how many bytes to set aside.
| Type | Holds | Bytes | Example | printf |
|---|---|---|---|---|
int | integers | 4 | int age = 19; | %i |
long | bigger integers | 8 | long population = 8000000000; | %li |
float | real numbers, about 7 significant digits | 4 | float pi = 3.14159; | %f |
double | real numbers, about 15 significant digits | 8 | double e = 2.718281828459045; | %f |
char | one character | 1 | char grade = 'A'; | %c |
bool | true or false | 1 | bool done = false; | — |
string | text (CS50 library) | depends | string name = "Alice"; | %s |
Notice the quotes: a char uses single quotes ('A'), a string uses double quotes ("Alice"). In week 4 we discover that a string is really a row of chars, which is why the distinction matters.
Arithmetic and a first calculator
C has the operators you expect: +, -, *, /, and %, the remainder (7 % 3 is 1). Let’s add two numbers.
// Addition with int
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
int x = get_int("x: ");
// Prompt user for y
int y = get_int("y: ");
// Perform addition
printf("%i\n", x + y);
} Try 2 000 000 000 plus 2 000 000 000. The answer is negative. We come back to that at the end of the page, because it has grounded airplanes.
// Addition with long
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
long x = get_long("x: ");
// Prompt user for y
long y = get_long("y: ");
// Perform addition
printf("%li\n", x + y);
} Conditions
A condition in C looks like Scratch’s if … then … else, with parentheses around the question and braces around the answer.
// Conditional, Boolean expression, relational operator
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for integers
int x = get_int("What's x? ");
int y = get_int("What's y? ");
// Compare integers
if (x < y)
{
printf("x is less than y\n");
}
} The question inside the parentheses is a boolean expression: something that is either true or false. The comparison operators are <, <=, >, >=, == and !=. The progression from compare2 to compare4 is what design means: all three are correct, one of them is clearly better.
// Comparing against lowercase char
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user to agree
char c = get_char("Do you agree? ");
// Check whether agreed
if (c == 'y')
{
printf("Agreed.\n");
}
else if (c == 'n')
{
printf("Not agreed.\n");
}
} || is or, && is and, ! is not. The same three words Scratch had as green blocks.
Loops
// Opportunity for better design
#include <stdio.h>
int main(void)
{
printf("meow\n");
printf("meow\n");
printf("meow\n");
} Programmers count from 0, so i < 3 gives exactly three iterations: 0, 1, 2. i++ is shorthand for i = i + 1. Use for when you know how many times; use while when you repeat until something happens; while (true) is Scratch’s forever.
// Prints a row of 4 question marks
#include <stdio.h>
int main(void)
{
printf("????\n");
} This is the shape of nearly every input in this course: ask in a do … while, reject what is wrong, and only then do the work. Never trust input; in week 13 this sentence becomes the whole of security.
Functions: abstraction, in C
// Abstraction
#include <stdio.h>
void meow(void);
int main(void)
{
for (int i = 0; i < 3; i++)
{
meow();
}
}
// Meow once
void meow(void)
{
printf("meow\n");
} // Abstraction with parameterization
#include <stdio.h>
void meow(int n);
int main(void)
{
meow(3);
}
// Meow some number of times
void meow(int n)
{
for (int i = 0; i < n; i++)
{
printf("meow\n");
}
} // Scope error
#include <cs50.h>
#include <stdio.h>
int add(void);
int main(void)
{
// Prompt user for x
int x = get_int("x: ");
// Prompt user for y
int y = get_int("y: ");
// Perform addition
int z = add();
printf("%i\n", z);
}
int add(void)
{
return x + y;
} Read int add(int a, int b) as a contract: give me two ints, I give you back one int. Every function in every library you will ever use, from printf to the ones that render your phone’s screen, is a contract like this.
Where numbers break
Integer division and truncation
// Division with ints, demonstrating truncation
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
int x = get_int("x: ");
// Prompt user for y
int y = get_int("y: ");
// Divide x by y
printf("%i\n", x / y);
} Floating-point imprecision
// Floating-point imprecision
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for x
int x = get_int("x: ");
// Prompt user for y
int y = get_int("y: ");
// Divide x by y
float z = (float) x / (float) y;
printf("%.20f\n", z);
} Never compare floats with ==, and never store money in a float. Banks count in integer cents. In 1991 a Patriot missile battery missed a Scud because a clock that counted in tenths of a second accumulated this exact rounding error for 100 hours.
Integer overflow
An int is 32 bits: 4 294 967 296 values, split into −2 147 483 648 to 2 147 483 647. Add one to the maximum and the bits wrap around to the minimum, like a car’s odometer rolling from 999999 to 000000. That is why calculator0.c gave a negative answer for two billion plus two billion.
int big = 2147483647;
big = big + 1;
printf("%i\n", big); // -2147483648
It is also the wow moment of the week: the Boeing 787’s generator controllers counted hundredths of a second in a 32-bit integer, and 2³¹ hundredths of a second is 248.55 days, after which they switched off. The same bug took Pac-Man’s original arcade machine to a broken level 256, and the fear of two-digit years was the whole of Y2K. Use long when values can be large, and think about the maximum whenever you count anything.
Summary
- Edit, compile (
make), run (./program). Read the first compiler error. - Types:
int,long,float,double,char,bool,string, withprintfplaceholders%i %li %f %c %s. get_intand friends ask for input;do … whilevalidates it.- Conditions with
if / else if / elseon boolean expressions, joined by||,&&,!. - Loops with
whileandfor, counting from 0; nested loops for grids. - Functions with prototypes, parameters and return values; a variable lives only in its own braces.
- Integer division truncates, floats are approximate, ints overflow, and all three have broken real systems.
- Lab 2 starts at 15:00: C Kickstart. You write your own hello, mario, calculator and cash from this page’s building blocks.
In industry
-
C is the substrate
Linux, Windows, macOS, the Python interpreter, SQLite, Git, most of your car's firmware and the code on the Mars rovers are C. Every language you learn later calls into C somewhere.
-
Compilers are a career
gcc and clang, the two compilers behind nearly all C code (we use gcc in the labs; cs50.dev uses clang), are decades-old open-source projects that Apple, Google and Linux depend on. Compiler engineers are among the best-paid developers because everyone's code runs through their code.
-
Input validation is security
The do-while loop that rejects a negative height is the same discipline that prevents most web vulnerabilities: never trust input.
-
Floating-point cost real money
In 1991 a Patriot missile battery missed a Scud because 0.1 seconds cannot be stored exactly in binary and the rounding error grew for 100 hours. 28 soldiers died. Floats are approximations; know it.
Lab 2: C Kickstart
Your first real C programs: three one-line warm-ups (hello, greet, bits), then mario pyramids, a calculator with validated input and cash change-making with a greedy loop. Accepted and autograded on Classroom 50. C · due Fri, Oct 9 · 23:59 · 10 points · open now Open lab instructions →Lab 2 · Explore: C in a notebook
Read it here, or open it in Colab: it writes, compiles and runs C right in the browser.
Go deeper
- CS50x 2026 · Week 1 notes · Every program from the lecture, explained.
- CS50 Manual Pages · Beginner-friendly docs for printf, get_int, and friends.
- Beej's Guide to C, chapters 1–4 · A second, friendlier explanation of the same material.
- Compiler Explorer · Paste hello.c, see the machine code your compiler produces.
- FAA airworthiness directive on the 787 (2015) · The primary source for the wow moment.
Tags:Ccompilergccmakeprintfscanfget_intintfloatcharstringifwhileforfunctionoverflowfloating point