C

How computers actually work

C

Memory, pointers, the stack, malloc. The language under your operating system, compiled for real in your browser.

0 / 12 lessons done
  1. 01 Hello, compilerWrite your first C program and find out what a compiler really does: it turns your text into machine code the CPU can run. 2 exercises
  2. 02 Variables are bytesIn C every variable is a fixed number of bytes in memory. Learn int, char, double and long long, and measure them with sizeof. 2 exercises
  3. 03 Numbers are just bitsHow integers are stored in binary, why an unsigned char wraps from 255 to 0, and how to print numbers in hex with printf. 2 exercises
  4. 04 Decisions and loopsMake C programs choose with if and else, and repeat work with for and while loops. Includes FizzBuzz and reading input. 2 exercises
  5. 05 Functions and the stackHow C functions get their own stack frame, why local variables vanish after return, and how recursion stacks frames on top of each other. 2 exercises
  6. 06 Arrays in memoryC arrays are blocks of same-sized elements packed side by side in memory. Learn indexing, sizeof tricks and why C never checks bounds. 2 exercises
  7. 07 Strings are char arraysC has no string type. A string is an array of chars ending in a zero byte. Write your own strlen and change letters by hand. 2 exercises
  8. 08 Pointers and addressesA pointer is a variable that holds a memory address. Learn what & and * do in C, with no magic and no fear. 2 exercises
  9. 09 Pass by value, pass a pointerC always copies arguments into the function. Learn why a naive swap fails and how passing pointers lets a function change your variables. 2 exercises
  10. 10 Pointer arithmeticAdding 1 to a pointer moves it by one whole element. See why arr[i] is really *(arr + i) and how arrays decay into pointers in C. 2 exercises
  11. 11 Structs and paddingGroup related values into one C struct, use the dot and arrow operators, and see why sizeof a struct can be bigger than its parts. 2 exercises
  12. 12 The heap and mallocAsk for memory at runtime with malloc, give it back with free, build a dynamic array in C and learn why memory leaks happen. 2 exercises

Projects for this track

FreeC · 40 min

Bit printer

Print any number in binary using shifts and masks. Once you can see the bits, overflow stops being magic.

  1. Write void print_bits(unsigned char x).
  2. Loop from bit 7 down to 0 and print (x >> i) & 1.
  3. Print 5, 255 and what 255 + 1 becomes in an unsigned char.
Start in the playground
FreeC · 40 min

Reverse a string in place

No library help: find the end of a string yourself, then swap characters from both ends until they meet. The best pointer practice there is.

  1. Write int my_strlen(const char *s) that counts characters up to the zero byte.
  2. Write void reverse(char *s) with one pointer at the start and one at the end, swapping as they move inwards.
  3. Test it on "hello", on a one-letter string and on an empty string.
Start in the playground
Pro, coming soonC · 4 hours

Write your own malloc

Carve a big byte array into blocks, track what is free, and hand out memory yourself. The best way to understand the heap is to be the heap.

Get told when it opens