lang-c
Use when the user asks about C (C11, C17, C23), pointers, undefined behavior, strict aliasing, restrict, malloc/free ownership, buffer overflows, segfaults…
от j4flmao★ 47 звёзд на GitHub
Установить lang-c в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
C
Purpose
C gives direct control over memory and layout, and the compiler assumes you never break the standard's rules. This skill focuses on writing C whose behaviour is defined, whose ownership is clear, and whose bugs are caught by tooling before production.
Agent Protocol
Trigger
"C language", "C11", "C17", "C23", "pointers", "undefined behavior", "strict aliasing", "restrict", "volatile", "malloc", "buffer overflow", "segfault", "sanitizer".
Input Context
- Standard and compilers (GCC, Clang, MSVC) and target platforms
- Code under review or the API to design; existing build system
- Constraints (freestanding/embedded, real-time, ABI stability)
Output Artifact
C code with ownership comments, build/sanitizer flags, review findings. No file unless requested.
Response Format
Std/compiler: {C17, gcc 14 / clang 19 / MSVC}
Change: {what and why}
Code: {snippet}
UB check: {which rules this code relies on}
Verify: {flags + sanitizers + tests}
1. Undefined Behaviour (UB)
C is not "portable assembly": optimisers assume UB never happens and delete or rearrange code accordingly.
| UB | Example | Defined alternative |
|---|---|---|
| Signed integer overflow | if (a + 1 < a) (optimised to false) |
check before: if (a == INT_MAX), or __builtin_add_overflow, C23 ckd_add |
| Out-of-bounds access | buf[len] |
track lengths, use sizes in APIs |
| Use after free / double free | dangling pointers | single owner, set to NULL after free |
| Null dereference | p->x before checking p |
check first (the compiler may delete later null checks) |
| Strict aliasing violation | *(uint32_t *)&f for a float f |
memcpy(&u, &f, sizeof u) (compiles to a move) |
| Shift by >= width or negative | 1 << 32 |
n < 32 ? 1u << n : 0 |
| Unsequenced modification | i = i++ + 1 |
separate statements |
| Data race | two threads writing a plain variable | <stdatomic.h> or a mutex |
| Uninitialised read | automatic variable used before assignment | initialise at declaration |
2. volatile, restrict, const
volatile: every access is performed as written — for memory-mapped I/O andsig_atomic_tflags in signal handlers. It is not a thread synchronisation primitive (no atomicity, no ordering); use_Atomic/atomic_*for threads. Seereferences/memory-mapped-io.md.restrict: promise that, for the pointer's lifetime, the object is accessed only through it; enables vectorisation. Breaking the promise is UB.conston pointees in APIs documents read-only access (size_t count(const char *s)).
3. Ownership and Memory
- Document who allocates and who frees for every pointer in an API. Common pattern:
obj_create()/obj_destroy(); caller-provided buffers with explicit sizes for output. - Check every
malloc/calloc/reallocresult;reallocinto a temporary so the original is not leaked on failure:
void *tmp = realloc(buf, new_cap);
if (tmp == NULL) { free(buf); return -ENOMEM; }
buf = tmp;
- Overflow-safe allocation:
calloc(n, size)checksn * size; otherwise testn > SIZE_MAX / sizefirst. - Single-exit cleanup with
gotois idiomatic and readable:
int load(const char *path, struct cfg *out)
{
int rc = -1;
FILE *f = fopen(path, "rb");
char *buf = NULL;
if (!f) { perror(path); goto out; }
buf = malloc(BUF_SZ);
if (!buf) goto out;
if (fread(buf, 1, BUF_SZ, f) == 0 && ferror(f)) goto out;
rc = parse_cfg(buf, out);
out:
free(buf);
if (f) fclose(f);
return rc;
}
4. Strings and Buffers
- Always carry lengths; prefer
snprintf(check the return value for truncation),memcpywith known sizes. Avoidstrcpy,strcat,sprintf,gets(removed in C11).strncpydoes not guarantee termination. strlcpy/strlcatare available on BSD/macOS and glibc ≥ 2.38; otherwise write a bounded copy helper.- Parse integers with
strtol/strtouland checkerrnoand the end pointer, notatoi.
5. Modern C Features Worth Using
- C11:
_Static_assert,_Alignas/_Alignof,<stdatomic.h>,<threads.h>(optional), anonymous structs/unions,_Generic. - C23:
nullptr,bool/true/falseas keywords,constexprobjects,typeof,nodiscard/maybe_unusedattributes,#embed,<stdckdint.h>checked arithmetic, binary literals,memset_explicit. Check compiler support (GCC 14+/Clang 18+ have most; MSVC lags).
6. Build Flags and Tooling
# development
cc -std=c17 -O1 -g -Wall -Wextra -Wpedantic -Wconversion -Wshadow -Werror \
-fsanitize=address,undefined -fno-omit-frame-pointer main.c -o app
# hardened release (GCC/Clang, glibc)
cc -std=c17 -O2 -D_FORTIFY_SOURCE=3 -fstack-protector-strong -fPIE -pie \
-Wl,-z,relro,-z,now main.c -o app
- Sanitizers: ASan (memory errors), UBSan (UB at runtime), TSan (data races; not combinable with ASan), MSan (uninitialised reads, Clang only). Run the test suite under them in CI.
- Static
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
- references/inline-assembly.md
- references/memory-mapped-io.md
FAQ
Что делает скилл lang-c?
Use when the user asks about C (C11, C17, C23), pointers, undefined behavior, strict aliasing, restrict, malloc/free ownership, buffer overflows, segfaults, sanitizers or C builds with CMake. Produces portable, warning-clean C with sanitizer setup. Not for microcontroller firmware (use c-cpp-patterns) or C++ (use lang-cpp).
Как установить скилл lang-c?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл lang-c запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
AnthropicToolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
access
AnthropicManage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
configure
AnthropicSet up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
access
AnthropicManage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…