C code snippets with use cases
32 ready-made code building blocks, sorted by topic.
Basics
- Hello World – A first test of whether the compiler and environment work.
- Variables and types – Store values and check their size in memory.
- Random numbers (dice) – Games, simulations or test data with random values.
- Printing the sizes of the basic types – Check how wide types really are on the target platform (portability, file formats).
Control flow
- if / else if / else – React differently depending on the case, e.g. to evaluate grades, price tiers or status…
- for loop – Repeat n times or process every element of an array.
- enum with switch – Name states cleanly, e.g. traffic light phases, protocol states or error codes.
Functions
- Defining and calling functions – Bundle recurring logic, e.g. calculations or output formats.
- Dispatch table with function pointers – Map commands or states to functions without long if chains (menus, interpreters, state…
- static variable as a counter – Keep state across multiple calls, e.g. running IDs or call counters.
Data structures
- Array: sum and maximum – Process series of measurements, e.g. determine the sum, average or maximum.
- Key-value table (linear search) – Resolve small mappings such as configuration names or command names.
- struct with functions (instead of a class) – Bundle data and related operations, e.g. users, products or configurations.
- Sorting structs with qsort – Order records by a field, e.g. users by age or products by price.
- Linked list – Insert and remove data dynamically when order matters (queues, history lists).
Files
- Write a file and read it line by line – Save and load logs, configurations or text data.
- Counting lines and words of a file – Simple text analysis: estimate the size of logs or text files (like the wc program).
- Binary data with fwrite / fread – Store number series or structs compactly and load them again (save games, caches).
Strings
- Reading a line safely – Read user input from the keyboard without risking a buffer overflow.
- Reversing a string – Process strings in place, e.g. palindrome checks or reversing numbers as text.
- Formatted strings with snprintf – Safely assemble file names, log lines or messages.
- Counting letters (histogram) – Text statistics, frequency analysis or simple cipher analysis.
More snippets
- Errors via return value and errno – Detect and report failed file accesses and system calls.
- Parsing a number from text (strtol) – Convert user input or configuration values into numbers and detect errors.
- Swapping values via pointers – Change values of a calling function (call by reference), e.g. in sorting algorithms.
- Command-line arguments – Build your own CLI tools that accept file names or options.
- Dynamic array with calloc – Store amounts of data whose size is only known at runtime (e.g. number of measurements).
- Growing array (vector) with realloc – A list that grows while reading input, without knowing the number of elements beforehand.
- Setting and testing bit flags – Store options, permissions or states compactly in a single number (file permissions…
- Macros: parentheses are mandatory – Write small helper macros safely and avoid a typical trap.
- Header with include guard – Provide a library's declarations without multiple includes causing errors.
- Finding a buffer overflow with AddressSanitizer – Automatically track down memory errors during development before they become security…