Skip to content
Skills Library
Programming · Learning & explanation

c-patterns

/c-patterns

C language patterns and best practices for safe, maintainable C: ownership, API contracts, error handling, integer safety, portability, and concurrency boundaries. Use when writing or reviewing C code (C11+), designing module interfaces, refactoring legacy C, or hardening low-level code paths.

Repository
malskill ↗
Licence
MIT
Last repository change
13.11.2026

Assessment and security

90/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content25 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
6
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 13.11.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

C language patterns and best practices for safe, maintainable C: ownership, API contracts, error handling, integer safety, portability, and concurrency boundaries. Use when writing or reviewing C code (C11+), designing module interfaces, refactoring legacy C, or hardening low-level code paths.

Original author description

C language patterns and best practices for safe, maintainable C: ownership, API contracts, error handling, integer safety, portability, and concurrency boundaries. Use when writing or reviewing C code (C11+), designing module interfaces, refactoring legacy C, or hardening low-level code paths.

How it works

  1. When to activate
  2. Core rules (high signal)
  3. Recommended workflow
  4. Quick review checklist
  5. Resources
Original SKILL.md headings · 84 lines, 443 words
SKILL.md preview
---
name: c-patterns
description: "C language patterns and best practices for safe, maintainable C: ownership, API contracts, error handling, integer safety, portability, and concurrency boundaries. Use when writing or reviewing C code (C11+), designing module interfaces, refactoring legacy C, or hardening low-level code paths."
license: MIT
compatibility: "C11 baseline (C23 notes included: GCC 14+/Clang 18+ ship most of C23; MSVC via /std:clatest). Toolchains: Clang, GCC, MSVC (C mode), MinGW-w64. Build: CMake or Make. Binary inspection: objdump/nm (MinGW/Linux), dumpbin (MSVC), readelf/ldd/strace (Linux)."
metadata:
  author: AeonDave
  version: "1.1"
---

# C Patterns

This skill focuses on **safe C**: explicit ownership, explicit errors, predictable control flow, and contracts that survive refactors.

If you’re writing tests, use `c-testing`.

## When to activate

- Writing/refactoring C modules and APIs
- Reviewing for memory safety and integer safety
- Designing error-handling conventions and cleanup paths
- Hardening code for sanitizer/static analysis adoption
- Porting C code across GCC/Clang/MSVC/MinGW

---

## Core rules (high signal)

- Make ownership explicit: allocate/free at the same abstraction level.
- Use a consistent error strategy (return codes, out-params) and document it in headers.
- Avoid in-band error indicators when possible.
- Use a single-exit cleanup path for functions managing multiple resources.
- Treat integer conversions and size calculations as potential bugs.

---

## Recommended workflow

### Phase 1 — Define API contract first

Before coding:

- Define ownership of every pointer parameter (`borrowed`, `owned`, `transferred`).
- Define preconditions/postconditions in the header, not only in comments inside `.c`.
- Define error domain (`enum` or code space) and whether `errno` is used.

### Phase 2 — Implement with explicit lifetimes

- Use `create/destroy`, `init/deinit`, or `open/close` pairs consistently.
- Keep one cleanup exit path for multi-resource functions.
- Do not mix allocator families (`malloc/free` vs custom pool vs Win32 heaps).

### Phase 3 — Harden integer and bounds logic

- Use `size_t` for sizes/counts and guard all multiplication/addition used in allocation.
- Validate all signed/unsigned conversions and indexing arithmetic.
- Reject impossible sizes early (`n == 0`, `n > MAX_ALLOWED`, overflow guards).

### Phase 4 — Concurrency boundaries

- Prefer immutable data or message passing where possible.
- If locks are needed, document lock ownership and lock order in header comments.
- Use atomics for single-value state; mutexes for compound invariants.

### Phase 5 — Toolchain-aware portability

- Keep warning set strict on all compilers (GCC/Clang/MSVC).
- Avoid UB-dependent behavior and compiler-specific assumptions.
- Guard compiler-specific attributes/macros with feature checks.

---

## Quick review checklist

- [ ] Every pointer has clear ownership semantics in the API.
- [ ] Every non-trivial function has deterministic cleanup path.
- [ ] Every allocation arithmetic path has overflow guard.
- [ ] Every error path returns actionable status.
- [ ] Concurrency primitives protect explicitly documented data.
- [ ] Code builds clean with strict warnings on at least two toolchains.

---

## Resources

Load on demand:

- `references/error-handling.md` — error policy, return codes, cleanup patterns
- `references/memory-ownership.md` — allocation/free rules, zero-length alloc, free-null
- `references/integers.md` — overflow, size_t usage, bounds checking
- `references/concurrency.md` — pthreads, C11 atomics, Win32 threads, race detection
- `references/tooling.md` — warnings, sanitizers, static analysis, binary inspection (MinGW/objdump, MSVC/dumpbin, Linux tools)
Files in this package (6)
  • SKILL.md 4 KB
  • references/concurrency.md 3 KB
  • references/error-handling.md 2 KB
  • references/integers.md 2 KB
  • references/memory-ownership.md 2 KB
  • references/tooling.md 4 KB

Versions

The database has no other skills named c-patterns.

Database snapshot: 13.11.2026 · package version 4ae5654c982c (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100rust-patterns
    Idiomatic Rust patterns, ownership, error handling, traits, concurrency, and best practices for building safe, performant applications. Use when writing or reviewing Rust code and ownership, error handling, traits, or concurrency is in question.
  • 90cpp-patterns
    Modern C++ patterns and best practices for readable, safe, maintainable C++ code: RAII, ownership, error handling, API design, concurrency basics, and build/tooling hygiene. Use when writing or reviewing C++ (C++20+) code.
  • 85swift-engineering
    Expert Swift programming, modern language features, concurrency, type safety, error handling, and maintainable Apple-platform code. Use when writing, reviewing, refactoring, or debugging Swift.
  • 100go-best-practices
    Production Go patterns — lifecycle, concurrency, config, testing, CI. Use when writing, reviewing, or refactoring Go code. Triggers on Go code review, Go refactoring, or 'go best practices'.
  • 90python-best-practices
    Python best practices across design patterns, error handling, project structure, and testing. Use when writing Python code, structuring new Python projects, designing APIs for libraries, implementing error handling, or setting up pytest test suites.
  • 100golang-patterns
    Go-specific design patterns and best practices including functional options, small interfaces, dependency injection, concurrency patterns, error handling, and package organization. Use when working with Go code to apply idiomatic Go patterns.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.