References

C reference

Learn memory, pointers, and efficient systems programming.

C Variables

Declare typed values.

C Arrays

Store a fixed sequence.

C Pointers

Work with memory addresses.

C conditions, loops, and totals

Combine a counted loop with an even-number condition.

C strings and null terminators

Traverse characters without reading beyond the string.

C functions and pointer parameters

Modify caller-owned values through valid addresses.

C structures and records

Group related fields into a typed record.

C dynamic memory and ownership

Allocate, check, use, and free a heap buffer.

C reading and writing text files

Write lines to a file with fprintf, read them back with fgets, and parse each line with sscanf.

C bit manipulation and flags

Set, clear, test, and toggle bits with &, |, ^, ~, and shifts, and count set bits.

C two-dimensional arrays

Store a matrix, transpose it, and multiply it with another one.

C macros versus inline functions

See how a macro evaluates its arguments more than once and why a function is safer.

C array search with explicit length

Search only within the supplied array bounds.

C linked nodes and traversal

Connect records with pointers and follow a chain.

C array-based stacks and circular queues

Build a fixed-capacity stack and a queue that wraps around the end of its array.

C binary search and the standard bsearch

Find a value in a sorted array in logarithmic time, by hand and with the library function.

C sorting: bubble sort versus quicksort

Implement two sorts, count their comparisons, and see why O(n log n) beats O(n²).

C binary search trees with malloc and free

Allocate tree nodes dynamically, traverse them in order, and release every node.

C string algorithms

Write strlen and strcmp by hand, check palindromes, and count occurrences with strstr.

C dynamic programming: longest common subsequence

Fill a table of subproblem answers to find the longest sequence that two strings share.

C: best, worst, and average case of linear search

Count comparisons to see how the cost of one algorithm depends on the input it receives.

C: amortized cost of a growing array

Double the capacity of a malloc'd array when it fills, and count the copies made.

C: a hash index with chaining

Store keys in buckets of linked lists and see how collisions lengthen a lookup.

C: a sorted index and range queries with lower_bound

Use binary search on a sorted array to find the first key at or above a value and read a range from there.

C: simulating round-robin scheduling

Give each job a fixed time slice in turn and compute the turnaround time of each.

C: protecting a shared counter with a POSIX mutex

Run two threads that increment one counter and use pthread_mutex_lock so no update is lost.

C: the CRC-32 frame check sequence

Compute the cyclic redundancy check used by Ethernet and detect a single flipped bit.

C: network, broadcast, and host count of a CIDR block

Use shifts and masks on a 32-bit address to describe a subnet.

C: unit testing with a small macro

Report which checks pass and fail by printing the text of each expression.

C: the strategy pattern with function pointers

Choose behaviour at run time by passing a function to qsort, without changing the sorting code.

C: preventing buffer overflows with snprintf

Copy text into a fixed buffer without writing past its end, and detect truncation.

C: validating numeric input with strtol

Accept only text that is entirely a number in the allowed range, and reject everything else.

C: why a one-time pad key must never be reused

XOR two messages with the same key and show that the key cancels out and leaks information.

C: splitting a sum across threads without sharing

Give each thread its own range and result slot, then combine the results after joining.

C: the speed-up limit of Amdahl's law

Compute the best possible speed-up for a program in which only part of the work can run in parallel.

C: breadth-first search on a grid with an array queue

Find the length of the shortest path through a maze using a queue stored in a plain array.

C: game search with negamax and a memo table

Solve a take-away game and count how many positions memoization saves.

C: forward chaining with bit sets

Store facts as bits of an integer and apply if-then rules with bitwise operations.

C: resolution refutation with clauses as bit masks

Represent each clause by two masks, one for positive and one for negative literals, and resolve until a contradiction appears.

C: linear regression with gradient descent

Fit y = w·x + b by repeatedly stepping the parameters against the gradient of the mean squared error.

C: k-nearest-neighbors classification

Sort training points by distance to a query and let the k closest vote.

C: training a perceptron until it makes no mistakes

Use integer weights and the perceptron rule to learn the AND function.

C: softmax and why the largest score is subtracted

Convert scores to probabilities and show that the naive formula overflows for large scores.

C: smoothing noise with a box-blur convolution

Average each 3×3 neighbourhood of a small image to reduce an isolated bright pixel.

C: grayscale histogram and a global threshold

Count how many pixels have each brightness level and separate dark from bright pixels at the mean.