Language basics
35 steps in 7 sets of C.
C from the beginning. Types, operators, control flow, functions, arrays and strings, then storage classes and printf and scanf. Thirty-five steps, and the whole language is smaller than this tour makes it look.
What takes the time in C is not the syntax. It is knowing what the machine does with it, which starts here and does not really stop.
Variables & types
- Variables and printfDeclaring typed variables and printing them.
- Types, casts, and sizeofConverting between C's numeric types explicitly.
- Constants, macros, enumsThe several ways C names a fixed value.
- Sized integer typesstdint.h gives you exact widths, which int and long do not promise.
- Floating pointfloat, double, and why you never compare them with equals.
- Limits and overflowEvery integer type has a range, and crossing it is either wrapping or undefined.
- Integer promotionSmall types promote to int, and mixing signed with unsigned converts the signed one.
Operators & flow
- OperatorsCompound assignment, increment, and the ternary that replaces a short if.
- ConditionalsBranching with if/else and the ternary operator.
- for and while loopsC's counting and condition-driven loops.
- do whileThe loop that always runs once, which suits menus and retry loops.
- switch statementsMulti-way branching with fall-through cases.
- Enums as stateAn enum names the states, and a switch over it makes the gaps visible.
- FizzBuzzThe classic screening exercise for loops and conditionals.
Functions
- FunctionsDefining and composing functions, including static ones.
- Storage classesstatic, extern and automatic: where a variable lives and how long it lasts.
- GCDEuclid's algorithm, still the shortest correct answer after two thousand years.
- Primality testTrial division up to the square root, which is enough.
- Iterative FibonacciFibonacci without recursion, two variables and a loop.
- RecursionA base case and a smaller problem, on the stack rather than in a loop.
- Towers of HanoiRecursion that moves a whole stack by trusting the smaller case.
Arrays & strings
- ArraysFixed-size arrays: initializing and iterating.
- Max of an arrayFinds the largest element, and shows why the length has to travel with the array.
- Character stringsWalking a null-terminated string one char at a time.
- Manual strlenWalks to the terminating zero byte, which is all a C string is.
- Classifying charactersctype.h answers what a character is, without hard-coding ASCII ranges.
Values & storage
- sizeof and alignmentsizeof measures storage, and a struct is usually bigger than the sum of its fields.
- const correctnessconst on a parameter is a promise to the caller, and the compiler enforces it.
- Designated initializersC99 lets you name the members and elements you are initializing.
- Compound literalsAn unnamed value of struct or array type, built where it is needed.
Input & output
- printf conversionsThe format string is a small language: width, precision, and the right specifier per type.
- Reading input safelyscanf is sharp: check its return value, and never let %s run unbounded.
Encore
- average.cA complete program: read numbers from a file, average them, handle the failures.
- histogram.cPrint a text histogram from counts given as arguments.
- number_facts.cA complete program: read numbers from argv, report facts about each.