C Programming: Memory, Pointers and the Stack

Learn C by watching real programs run: variables on the stack, pointers and addresses, arrays and strings, structs, and memory you manage yourself with malloc and free. Every lesson is recorded from a real compiled run.

C Basics

  1. printf: Printing Values: Print numbers, decimals and characters with printf and format codes like %d, %.2f and %c.
  2. Data Types and sizeof: char, int, long long, float, double and unsigned: what each holds and how many bytes sizeof says it uses.
  3. Operators: Division, Remainder, ++: Integer division, the remainder, casting to get decimals, and the difference between x++ and ++x.

Control Flow

  1. if, else if and switch: Choose a branch with if / else if / else, and match exact values with switch, where break matters.
  2. for, while and do-while: Three loops: for when you know the count, while while a condition holds, and do-while that always runs once.
  3. break and continue: Skip a bad value with continue and stop at a sentinel with break.

Functions and Scope

  1. Functions: Define functions with parameters and return values, call them from main, and watch each call get its own stack frame.
  2. Pass by Value vs Pass a Pointer: C copies arguments. A swap function only works when it gets pointers to the caller's variables.
  3. Recursion and the Call Stack: sum_to(3) calls sum_to(2), then (1), then (0). Watch the frames pile up and unwind.
  4. Scope: Local, Global and static: Locals are new on every call; a global is shared by everything; a static local keeps its value between calls.

Arrays and Strings

  1. Arrays: A fixed-size block of values on the stack. Partly fill one, count its length with sizeof, and find the largest.
  2. Two-Dimensional Arrays: A grid of rows and columns. Add up each row with nested loops.
  3. Strings: char Arrays and \0: C strings are char arrays ending in a zero byte. Measure, copy, join and compare them with string.h.

Pointers

  1. Pointers: Addresses and *: A pointer holds the address of another variable. Read and change a variable through it, and point it somewhere else.
  2. Pointer Arithmetic and Arrays: An array name acts as a pointer to its first element, and p++ moves to the next element, not the next byte.
  3. Pointers to Pointers: A pointer can point to another pointer. Follow the chain with ** to reach the value at the end.
  4. Function Pointers: A pointer can hold a function. Pass one to another function to choose behaviour at run time.

Structs and Types

  1. Structs: Group related fields into one type. Set fields, initialise in one go, and see that assignment copies the whole struct.
  2. Pointers to Structs and ->: Pass a struct's address so a function can change it, and use p->field as a shortcut for (*p).field.
  3. typedef and enum: Name a set of constants with enum, give types short names with typedef, and step a traffic light.
  4. const and #define Macros: Constants with const, and text-replacing macros with #define, including why macro arguments need brackets.
  5. Bitwise Operators and Flags: Store several on/off flags in one int: turn bits on with |, test with &, turn off with & ~, and shift.

Dynamic Memory

  1. Dynamic Memory: malloc and free: Ask for memory on the heap at run time, use it like an array, and give it back with free.
  2. Growing Arrays with realloc: Start small, and when the array is full, double its capacity with realloc. This is how dynamic arrays grow.
  3. A Linked List: Nodes on the heap, each pointing to the next. Build a list, walk it, and free it node by node.