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に貼り付けるだけ。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言語の「ポインタ」とは何ですか?
ポインタは、データがコンピュータのどこに保存されているかを示すメモリアドレスのようなものです。誤った使い方をするとエラーの原因になります。
`sprintf`より`snprintf`の方が良いのはなぜですか?
`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エージェントが特定の業界に関する要約を作成できるようにします。信頼できる情報源を使って、プロジェクトの背景、ルール、利用パターン、リスクを把握するのに役立ちます。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。