c
它能做什么
本指南教你用 C 语言编写代码的最佳实践。内容包括如何管理内存、正确使用指针和数组、处理错误,以及安全高效地操作字符串。
适用场景
- ✓第一次编写 C 代码时。
- ✓遇到 C 代码中难以理解的错误时。
- ✓想让你的 C 代码更安全、更快时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/c/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“如何在 C 语言中避免管理内存时的常见错误?”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会对比“糟糕”和“良好”的 C 代码示例,并解释其中的区别。它重点关注安全性、效率和清晰度,帮助你写出更好的代码。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
C: Idiomatic Efficiency Reference
When to Use
- Use this skill when the task matches this description: Language-specific super-code guidelines for c.
Table of Contents
- Memory Management
- Pointers & Arrays
- Error Handling
- Strings
- Structs & Enums
- Preprocessor & Headers
- Anti-patterns specific to C
1. Memory Management {#memory}
// ❌ malloc without checking return value
char *buf = malloc(size);
strcpy(buf, src);
// ✅
char *buf = malloc(size);
if (!buf) return -ENOMEM;
memcpy(buf, src, size);
// ❌ Casting malloc result (unnecessary in C, hides missing #include)
int *p = (int *)malloc(n * sizeof(int));
// ✅
int *p = malloc(n * sizeof *p);
// ❌ free without nulling (dangling pointer risk in long-lived scope)
free(ptr);
// ... later code might use ptr
// ✅
free(ptr);
ptr = NULL;
// ❌ Forgetting to free on early-return paths
char *a = malloc(100);
char *b = malloc(200);
if (!b) return -1; // leaks a
// ✅ — single cleanup label
char *a = NULL, *b = NULL;
a = malloc(100);
if (!a) goto cleanup;
b = malloc(200);
if (!b) goto cleanup;
// ... use a, b ...
cleanup:
free(b);
free(a);
Use sizeof *ptr instead of sizeof(Type) — it stays correct when the type changes.
2. Pointers & Arrays {#pointers}
// ❌ Manual array size tracking
void process(int *arr, int len) { ... }
process(data, 10);
// ✅ — pass size alongside pointer, or use a struct
typedef struct { int *data; size_t len; } IntSlice;
// ❌ Pointer arithmetic where array indexing is clearer
*(arr + i) = value;
// ✅
arr[i] = value;
// ❌ VLA in production code (stack overflow risk, optional in C11+)
int arr[n];
// ✅
int *arr = malloc(n * sizeof *arr);
if (!arr) return -ENOMEM;
// ... use arr ...
free(arr);
3. Error Handling {#errors}
// ❌ Using magic numbers for error returns
if (do_thing() == -1) { ... }
// ✅ — define or use named error codes
#include <errno.h>
if (do_thing() < 0) {
perror("do_thing");
return errno;
}
// ❌ Deeply nested error checks
int r1 = step1();
if (r1 == 0) {
int r2 = step2();
if (r2 == 0) {
int r3 = step3();
// ...
}
}
// ✅ — early return / goto cleanup
if (step1() < 0) goto fail;
if (step2() < 0) goto fail;
if (step3() < 0) goto fail;
return 0;
fail:
cleanup();
return -1;
goto cleanup is idiomatic C for resource teardown — don't avoid it out of principle.
4. Strings {#strings}
// ❌ strcpy without bounds checking
strcpy(dest, src);
// ✅
strncpy(dest, src, sizeof(dest) - 1);
dest[sizeof(dest) - 1] = '\0';
// or better: snprintf(dest, sizeof(dest), "%s", src);
// ❌ strcmp misuse
if (str == "hello") { ... } // compares pointers, not content
// ✅
if (strcmp(str, "hello") == 0) { ... }
// ❌ Building strings with repeated strcat (O(n²))
char result[1024] = "";
for (int i = 0; i < n; i++) {
strcat(result, items[i]);
}
// ✅ — track write position
char result[1024];
int pos = 0;
for (int i = 0; i < n && pos < (int)sizeof(result); i++) {
pos += snprintf(result + pos, sizeof(result) - pos, "%s", items[i]);
}
Prefer snprintf over sprintf — always.
5. Structs & Enums {#structs}
// ❌ Bare struct requiring `struct` keyword everywhere
struct point { int x, y; };
struct point p = {1, 2};
// ✅
typedef struct { int x, y; } Point;
Point p = {1, 2};
// ❌ Uninitialized struct
Point p;
use(p.x); // UB
// ✅
Point p = {0};
// ❌ Magic integer constants
if (state == 3) { ... }
// ✅
typedef enum { STATE_IDLE, STATE_RUNNING, STATE_DONE } State;
if (state == STATE_DONE) { ... }
6. Preprocessor & Headers {#preprocessor}
// ❌ Macro where inline function works (no type safety, double eval)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
MAX(x++, y) // x incremented twice if x > y
// ✅
static inline int max_int(int a, int b) { return a > b ? a : b; }
// ❌ No include guard
// my_header.h
struct Foo { int x; };
// ✅
#ifndef MY_HEADER_H
#define MY_HEADER_H
struct Foo { int x; };
#endif
// or: #pragma once (widely supported, not standard)
Keep macros for conditional compilation and constants. Use static inline for logic.
7. Anti-patterns specific to C {#antipatterns}
| Anti-pattern | Preferred |
|---|
…
常见问题
C 语言中的“指针”是什么?
指针就像内存地址,告诉你数据存放在计算机的哪个位置。如果使用不当,就会引发错误。
为什么 `snprintf` 比 `sprintf` 更好?
`snprintf` 可以设置写入文本的最大长度,防止文本“溢出”并破坏程序其他部分的数据。这是一种常见且危险的错误。
什么是“内存管理”?
内存管理是指在需要时为数据分配内存空间,不再需要时释放这些空间的过程,以避免浪费和错误。
详情
热度
- GitHub 星标
- 48k
- 排名
- #5887
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。