typestar

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.

Start this tour

Variables & types

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

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.

The other C tours