FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

125 snippets foundClear filters
CStrings & byte buffers

Find the final path separator

C: strrchr locates the last matching character without modifying the input.

C✓ Tested
CStrings & byte buffers

Find the first occurrence of a character

C: strchr returns a pointer into the input or NULL if the character is absent.

C✓ Tested
CArrays

Find the largest array value

Track the maximum in a nonempty C integer array.

C✓ Tested
CArray transformations

Find the smallest value in a nonempty array

C: Initialize from the first element instead of assuming a numeric sentinel.

C✓ Tested
CGetting Started

Find what remains after division

C: Use the remainder operator to count items left after forming complete groups.

C✓ Tested
CNumbers & memory

Format a decimal value

Print two fractional digits with C printf.

C✓ Tested
CBinary formats & structured parsing

Format a known timestamp in UTC

C: gmtime converts to UTC calendar fields and strftime formats them.

C✓ Tested
CBinary formats & structured parsing

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
CSmall algorithms & bit operations

Generate a bounded Fibonacci sequence

C: Keep only the two previous values instead of using exponential recursive calls.

C✓ Tested
CStructures & function interfaces

Give a reusable record type a short name

C: A typedef names a structure type without changing its representation.

C✓ Tested
CStructures & function interfaces

Group related values in nested structures

C: Nested members model a record that owns another simple record.

C✓ Tested
CDynamic memory & ownership

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