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
- printf: Printing Values: Print numbers, decimals and characters with printf and format codes like %d, %.2f and %c.
- Data Types and sizeof: char, int, long long, float, double and unsigned: what each holds and how many bytes sizeof says it uses.
- Operators: Division, Remainder, ++: Integer division, the remainder, casting to get decimals, and the difference between x++ and ++x.
Control Flow
- if, else if and switch: Choose a branch with if / else if / else, and match exact values with switch, where break matters.
- 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.
- break and continue: Skip a bad value with continue and stop at a sentinel with break.
Functions and Scope
- Functions: Define functions with parameters and return values, call them from main, and watch each call get its own stack frame.
- Pass by Value vs Pass a Pointer: C copies arguments. A swap function only works when it gets pointers to the caller's variables.
- Recursion and the Call Stack: sum_to(3) calls sum_to(2), then (1), then (0). Watch the frames pile up and unwind.
- 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
- Arrays: A fixed-size block of values on the stack. Partly fill one, count its length with sizeof, and find the largest.
- Two-Dimensional Arrays: A grid of rows and columns. Add up each row with nested loops.
- 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
- Pointers: Addresses and *: A pointer holds the address of another variable. Read and change a variable through it, and point it somewhere else.
- 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.
- Pointers to Pointers: A pointer can point to another pointer. Follow the chain with ** to reach the value at the end.
- Function Pointers: A pointer can hold a function. Pass one to another function to choose behaviour at run time.
Structs and Types
- Structs: Group related fields into one type. Set fields, initialise in one go, and see that assignment copies the whole struct.
- 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.
- typedef and enum: Name a set of constants with enum, give types short names with typedef, and step a traffic light.
- const and #define Macros: Constants with const, and text-replacing macros with #define, including why macro arguments need brackets.
- 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
- 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.
- Growing Arrays with realloc: Start small, and when the array is full, double its capacity with realloc. This is how dynamic arrays grow.
- A Linked List: Nodes on the heap, each pointing to the next. Build a list, walk it, and free it node by node.