The snippet library.
Small, practical examples. Search by task or narrow things down below.
Sort an integer array with qsort
C: The comparator uses relational comparisons to avoid subtraction overflow.
C✓ Tested
Split simple delimited text in place
C: strtok inserts terminators into a writable buffer and skips repeated delimiter characters.
C✓ Tested
Store a value in a named variable
C: Give a value a descriptive name and print it.
C✓ Tested
Store a variable-length payload after a structure header
C: A flexible array member lets one allocation hold metadata and trailing bytes.
C✓ Tested
Sum array values
Iterate over a fixed C array using a size-derived element count.
C✓ Tested
Sum the decimal digits of an unsigned integer
C: Repeated division and remainder extract digits without converting to text.
C✓ Tested
Swap owned buffers by swapping their pointers
C: Pointer swaps avoid copying the underlying data.
C✓ Tested
Swap two integers
Use a temporary value to swap C integers without arithmetic overflow.
C✓ Tested
Test whether an unsigned value is a power of two
C: A nonzero power of two has exactly one set bit.
C✓ Tested
Track which union member is currently valid
C: An explicit tag tells callers which overlaid union value to read.
C✓ Tested
Update a running count
C: Change a variable after its initial value has been assigned.
C✓ Tested
Use an automatically removed temporary file
C: tmpfile creates a binary update stream whose file is removed when closed.
C✓ Tested