The snippet library.
Small, practical examples. Search by task or narrow things down below.
Find the final path separator
C: strrchr locates the last matching character without modifying the input.
C✓ Tested
Find the first occurrence of a character
C: strchr returns a pointer into the input or NULL if the character is absent.
C✓ Tested
Find the largest array value
Track the maximum in a nonempty C integer array.
C✓ Tested
Find the smallest value in a nonempty array
C: Initialize from the first element instead of assuming a numeric sentinel.
C✓ Tested
Find what remains after division
C: Use the remainder operator to count items left after forming complete groups.
C✓ Tested
Format a decimal value
Print two fractional digits with C printf.
C✓ Tested
Format a known timestamp in UTC
C: gmtime converts to UTC calendar fields and strftime formats them.
C✓ Tested
Format a nonnegative integer amount as whole and fractional units
C: Keep the calculation in integer minor units and use two digits for the remainder.
C✓ Tested
Generate a bounded Fibonacci sequence
C: Keep only the two previous values instead of using exponential recursive calls.
C✓ Tested
Give a reusable record type a short name
C: A typedef names a structure type without changing its representation.
C✓ Tested
Group related values in nested structures
C: Nested members model a record that owns another simple record.
C✓ Tested
Grow an allocation without losing it on failure
C: Assign realloc to a temporary pointer so the original allocation remains available if growth fails.
C✓ Tested