FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
CNumbers & memory

Allocate a zeroed array

Check calloc, use the allocation, and release it in C.

C✓ Tested
CDynamic memory & ownership

Allocate an independent copy of a C string

C: Include one extra byte for the terminating null character.

C✓ Tested
CDynamic memory & ownership

Allocate and free a typed array

C: Use sizeof on the pointed-to type and check allocation success before writing.

C✓ Tested
CDynamic memory & ownership

Allocate and initialize a structure

C: Use a compound literal assignment to initialize all ordinary structure fields.

C✓ Tested
CStrings & byte buffers

Append text only when the destination has enough capacity

C: Check the combined lengths including the terminator before using strcat.

C✓ Tested
CFiles

Append text to a file

Use append mode in C without replacing the existing file.

C✓ Tested
CArray transformations

Calculate a cumulative sum for each position

C: Keep a running total and store each prefix result in a separate output array.

C✓ Tested
CSmall algorithms & bit operations

Calculate a fixed-window moving average

C: For this small window, sum each complete group and skip incomplete prefixes.

C✓ Tested
CSmall algorithms & bit operations

Calculate a least common multiple using a GCD

C: Divide before multiplying to reduce the risk of intermediate overflow.

C✓ Tested
CNumeric parsing & arithmetic

Calculate a page count without an overflowing addition

C: Divide first and add one only when a nonzero remainder remains.

C✓ Tested
CGetting Started

Calculate a simple total

C: Multiply a unit price by a quantity, then add a delivery fee.

C✓ Tested
CSmall algorithms & bit operations

Calculate a small factorial iteratively

C: Multiply a bounded sequence using an unsigned accumulator.

C✓ Tested