The snippet library.
Small, practical examples. Search by task or narrow things down below.
Compare binary buffers including zero bytes
C: memcmp checks an explicit byte length rather than stopping at a string terminator.
C✓ Tested
Compare small floating-point results with a tolerance
C: Use an absolute difference threshold for this known bounded magnitude.
C✓ Tested
Compare two C strings for equality
C: strcmp returns zero only when both null-terminated byte strings are equal.
C✓ Tested
Compute a simple XOR checksum
C: XOR all payload bytes into one byte to demonstrate a basic error-detection field.
C✓ Tested
Compute a two-dimensional vector length
C: hypot avoids unnecessary intermediate overflow compared with squaring both inputs directly.
C✓ Tested
Convert ASCII text to lowercase safely
C: Cast to unsigned char before calling ctype functions on a possibly signed char.
C✓ Tested
Convert ASCII text to uppercase
Convert characters with toupper while avoiding signed-char undefined behavior.
C✓ Tested
Convert known numeric text to an integer
C: Convert a trusted literal to an integer before adding. Validate external input before converting it.
C✓ Tested
Copy a file in bounded binary chunks
C: Check read, write, and close errors while copying without loading the entire file.
C✓ Tested
Copy text into a fixed buffer
Use snprintf and check for truncation when copying a C string.
C✓ Tested
Count a leading run of allowed characters
C: strspn counts bytes at the start that belong to a specified character set.
C✓ Tested
Count matching values
Count even numbers in a fixed C array.
C✓ Tested