The snippet library.
Small, practical examples. Search by task or narrow things down below.
Replace one collection element
C: Update an existing element at a known valid index.
C✓ Tested
Represent a small set of named states
C: An enum gives symbolic names to integer state values.
C✓ Tested
Require two conditions to be true
C: Combine a lower and an upper bound to check whether a value is in range.
C✓ Tested
Return a status separately from a calculated result
C: An output pointer lets a function return success or failure independently of its numeric result.
C✓ Tested
Reverse an array in place
Swap C array elements from opposite ends.
C✓ Tested
Rotate a nonempty array left by one element
C: Save the first item before shifting the remaining elements.
C✓ Tested
Round a halfway floating-point value
C: The C round function rounds halfway cases away from zero.
C✓ Tested
Round downward and upward independently
C: floor and ceil round toward negative and positive infinity respectively.
C✓ Tested
Run your first program
C: Start with a complete program that prints one line. Follow the run instructions below.
C✓ Tested
Search a sorted array with bsearch
C: The search uses the same ordering comparator as the sorted input.
C✓ Tested
Serialize a 16-bit integer in big-endian order
C: Explicit shifts make a byte format independent of the CPU’s native byte order.
C✓ Tested
Set and test bit flags
Use unsigned masks to combine and test C flags.
C✓ Tested