The snippet library.
Small, practical examples. Search by task or narrow things down below.
Move overlapping bytes safely
C: memmove supports overlapping ranges, unlike memcpy.
C✓ Tested
Name a value that stays fixed
C: Use a constant declaration or the language’s constant naming convention for a fixed limit.
C✓ Tested
Parse a bounded simple record with sscanf
C: A width limit protects the destination buffer and the consumed count detects trailing junk.
C✓ Tested
Parse an integer with validation
Validate range and trailing characters when parsing a C integer with strtol.
C✓ Tested
Parse an unsigned hexadecimal integer
C: Use an explicit base and check both range and full consumption.
C✓ Tested
Pass a function as a callback
C: A typed function pointer permits behavior to be supplied to another function.
C✓ Tested
Print a number with two decimal places
C: Format a small decimal value consistently for display.
C✓ Tested
Quote a CSV field and double embedded quotes
C: A quoted CSV field represents a quote character by repeating it.
C✓ Tested
Read a stream again from its beginning
C: rewind returns a seekable stream to offset zero and clears its error and EOF indicators.
C✓ Tested
Read bytes from a chosen file offset
C: Check fseek before reading so an unsupported or invalid seek is not ignored.
C✓ Tested
Read characters without confusing EOF with a byte
C: Store fgetc in int so every unsigned-char value remains distinguishable from EOF.
C✓ Tested
Read one character from ASCII text
C: Use a valid zero-based index to read the first character.
C✓ Tested