Communitygithub.com

JantonioFC/skillsbank

C 언어에 대한 언어별 우수 코드 가이드라인입니다.

skillsbank란 무엇인가요?

이 스킬은 C 언어 개발자를 위한 종합적인 코드 가이드라인으로, 메모리 관리, 포인터, 에러 처리, 문자열, 구조체, 전처리기 및 안티패턴 등 핵심 영역을 다룹니다. 메모리 누수, 댕글링 포인터, 버퍼 오버플로우와 같은 일반적인 C 오류를 방지하는 방법을 실제 코드 예제를 통해 제시합니다. 특히 `goto cleanup` 패턴을 사용한 리소스 정리, `snprintf`를 통한 안전한 문자열 처리, `const`와 `typedef`를 활용한 타입 안전성 확보 등 C 언어의 관용적이고 효율적인 코딩 방식을 강조합니다. Claude Code, Cursor, Codex 등 다양한 AI 코딩 에이전트에서 활용할 수 있으며, 코드 생성 시 C의 미묘한 함정을 피하고 생산성을 높이는 데 도움을 줍니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/JantonioFC/skillsbank/tree/HEAD/skills/c

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

C: 관용적이고 효율적인 레퍼런스

목차

  1. 메모리 관리
  2. 포인터와 배열
  3. 에러 처리
  4. 문자열
  5. 구조체와 열거형
  6. 전처리기와 헤더
  7. C 언어 특성 안티패턴

1. 메모리 관리 {#memory}

// ❌ 반환값 검사 없이 malloc 사용
char *buf = malloc(size);
strcpy(buf, src);

// ✅
char *buf = malloc(size);
if (!buf) return -ENOMEM;
memcpy(buf, src, size);
// ❌ malloc 결과를 캐스팅 (C에서는 불필요하며, #include 누락을 숨김)
int *p = (int *)malloc(n * sizeof(int));

// ✅
int *p = malloc(n * sizeof *p);
// ❌ free 후 널로 설정하지 않음 (오래 지속되는 범위에서 댕글링 포인터 위험)
free(ptr);
// ... 이후 코드에서 ptr을 사용할 수 있음

// ✅
free(ptr);
ptr = NULL;
// ❌ 초기 반환 경로에서 free를 잊음
char *a = malloc(100);
char *b = malloc(200);
if (!b) return -1; // a 누수

// ✅ — 단일 정리 레이블 사용
char *a = NULL, *b = NULL;
a = malloc(100);
if (!a) goto cleanup;
b = malloc(200);
if (!b) goto cleanup;
// ... a, b 사용 ...
cleanup:
    free(b);
    free(a);

sizeof(Type) 대신 sizeof *ptr을 사용하세요 — 타입이 변경되어도 정확합니다.


2. 포인터와 배열 {#pointers}

// ❌ 수동 배열 크기 추적
void process(int *arr, int len) { ... }
process(data, 10);

// ✅ — 포인터와 함께 크기를 전달하거나 구조체 사용
typedef struct { int *data; size_t len; } IntSlice;
// ❌ 배열 인덱싱이 더 명확한데 포인터 연산 사용
*(arr + i) = value;

// ✅
arr[i] = value;
// ❌ 프로덕션 코드에서 VLA 사용 (스택 오버플로 위험, C11+에서 선택적)
int arr[n];

// ✅
int *arr = malloc(n * sizeof *arr);
if (!arr) return -ENOMEM;
// ... arr 사용 ...
free(arr);

3. 에러 처리 {#errors}

// ❌ 에러 반환에 매직 넘버 사용
if (do_thing() == -1) { ... }

// ✅ — 명명된 에러 코드 정의 또는 사용
#include <errno.h>
if (do_thing() < 0) {
    perror("do_thing");
    return errno;
}
// ❌ 깊게 중첩된 에러 검사
int r1 = step1();
if (r1 == 0) {
    int r2 = step2();
    if (r2 == 0) {
        int r3 = step3();
        // ...
    }
}

// ✅ — 조기 반환 / 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은 C에서 리소스 정리를 위한 관용적 패턴입니다 — 원칙적으로 피하지 마세요.


4. 문자열 {#strings}

// ❌ 범위 검사 없이 strcpy 사용
strcpy(dest, src);

// ✅
strncpy(dest, src, sizeof(dest) - 1);
dest[sizeof(dest) - 1] = '\0';
// 또는 더 나은 방법: snprintf(dest, sizeof(dest), "%s", src);
// ❌ strcmp 잘못 사용
if (str == "hello") { ... } // 포인터를 비교, 내용 비교 아님

// ✅
if (strcmp(str, "hello") == 0) { ... }
// ❌ 반복된 strcat으로 문자열 생성 (O(n²))
char result[1024] = "";
for (int i = 0; i < n; i++) {
    strcat(result, items[i]);
}

// ✅ — 쓰기 위치 추적
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]);
}

sprintf보다 snprintf를 선호하세요 — 항상.


5. 구조체와 열거형 {#structs}

// ❌ 모든 곳에서 `struct` 키워드가 필요한 베어 구조체
struct point { int x, y; };
struct point p = {1, 2};

// ✅
typedef struct { int x, y; } Point;
Point p = {1, 2};
// ❌ 초기화되지 않은 구조체
Point p;
use(p.x); // 정의되지 않은 동작

// ✅
Point p = {0};
// ❌ 매직 정수 상수
if (state == 3) { ... }

// ✅
typedef enum { STATE_IDLE, STATE_RUNNING, STATE_DONE } State;
if (state == STATE_DONE) { ... }

6. 전처리기와 헤더 {#preprocessor}

// ❌ 인라인 함수로 가능한 매크로 (타입 안전성 없음, 이중 평가)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
MAX(x++, y) // x > y이면 x가 두 번 증가

// ✅
static inline int max_int(int a, int b) { return a > b ? a : b; }
// ❌ include 가드 없음
// my_header.h
struct Foo { int x; };

// ✅
#ifndef MY_HEADER_H
#define MY_HEADER_H
struct Foo { int x; };
#endif
// 또는: #pragma once (널리 지원되지만 표준은 아님)

매크로는 조건부 컴파일과 상수에 사용하세요. 로직에는 static inline을 사용하세요.


7. C 언어 특성 안티패턴 {#antipatterns}

안티패턴권장 방식
sprintfsnprintf와 버퍼 크기
getsfgets (gets는 C11에서 제거됨)
malloc 결과 캐스팅암시적 void* 변환 사용
malloc에서 sizeof(Type)sizeof *ptr
대형/런타임 배열에 VLA힙 할당
컨텍스트 매개변수 없는 void* 콜백함수 포인터와 함께 void *ctx 전달
전역 가변 상태구조체 포인터를 통해 상태 전달
런타임 에러 처리에 assert 사용적절한 에러 반환 코드
읽기 전용 포인터 매개변수에 const 누락const char *str
비교에서 signed/unsigned 혼합일관된 타입 사용, 명시적 캐스팅

제한사항

  • 이는 언어별 가이드라인이며 전체 아키텍처 결정을 다루지 않습니다.
  • 과도한 압축은 가독성을 떨어뜨릴 수 있으니 판단하여 적용하십시오.

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

JantonioFC/skillsbank

AI-powered image editing with style transfer and object removal

JantonioFC/skillsbank

Internationalization and localization patterns. Detecting hardcoded strings, managing translations, locale files, RTL support.

JantonioFC/skillsbank

Analise e auditoria de editais de leilao judicial e extrajudicial. Riscos ocultos, clausulas perigosas, debitos, ocupante e classificacao da oportunidade.

JantonioFC/skillsbank

Embed Photopea in web apps using photopea.js. Covers embedding, file I/O, scripting, exporting, layers, text, filters, and the full Photoshop-compatible API.

JantonioFC/skillsbank

Best practices for Remotion - Video creation in React

JantonioFC/skillsbank

Generate walkthrough videos from Stitch projects using Remotion with smooth transitions, zooming, and text overlays

JantonioFC/skillsbank

Seek and analyze video content using Memories.ai Large Visual Memory Model for persistent video intelligence

JantonioFC/skillsbank

Writes complete, structured landing pages optimized for SEO ranking, AEO citation, and visitor conversion. Activate when the user wants to write or generate a landing page for a product, service, or offer.

JantonioFC/skillsbank

Video and audio perception, indexing, and editing. Ingest files/URLs/live streams, build visual/spoken indexes, search with timestamps, edit timelines, add overlays/subtitles, generate media, and create real-time alerts.

JantonioFC/skillsbank

Upload, stream, search, edit, transcribe, and generate AI video and audio using the VideoDB SDK.

JantonioFC/skillsbank

Skill for discovering and researching autonomous AI agents, tools, and ecosystems using the AgentFolio directory.

JantonioFC/skillsbank

Expert in building portfolios that actually land jobs and clients - not just showing work, but creating memorable experiences. Covers developer portfolios, designer portfolios, creative portfolios, and portfolios that convert visitors into opportunities.

JantonioFC/skillsbank

Tool lifecycle UI components for React/Next.js from ui.inference.sh. Display tool calls: pending, progress, approval required, results. Capabilities: tool status, progress indicators, approval flows, results display. Use for: showing agent tool calls, human-in-the-loop approvals, tool output. Triggers: tool ui, tool calls, tool status, tool approval, tool results, agent tools, mcp tools ui, function calling ui, tool lifecycle, tool pending

JantonioFC/skillsbank

Security audit, hardening, threat modeling (STRIDE/PASTA), Red/Blue Team, OWASP checks, code review, incident response, and infrastructure security for any project.

JantonioFC/skillsbank

When the user wants help with paid advertising campaigns on Google Ads, Meta (Facebook/Instagram), LinkedIn, Twitter/X, or other ad platforms. Also use when the user mentions 'PPC,' 'paid media,' 'ROAS,' 'CPA,' 'ad campaign,' 'retargeting,' 'audience targeting,' 'Google Ads,' 'Facebook ads,'...

JantonioFC/skillsbank

Create AI avatar and talking head videos with OmniHuman, Fabric, PixVerse via inference.sh CLI. Models: OmniHuman 1.5, OmniHuman 1.0, Fabric 1.0, PixVerse Lipsync. Capabilities: audio-driven avatars, lipsync videos, talking head generation, virtual presenters. Use for: AI presenters, expla...

JantonioFC/skillsbank

Create AI marketing videos for ads, promos, product launches, and brand content. Models: Veo, Seedance, Wan, FLUX for visuals, Kokoro for voiceover. Types: product demos, testimonials, explainers, social ads, brand videos. Use for: Facebook ads, YouTube ads, product launches, brand awarene...

JantonioFC/skillsbank

Generate AI videos with Google Veo, Seedance, Wan, Grok and 40+ models via inference.sh CLI. Models: Veo 3.1, Veo 3, Seedance 1.5 Pro, Wan 2.5, Grok Imagine Video, OmniHuman, Fabric, HunyuanVideo. Capabilities: text-to-video, image-to-video, lipsync, avatar animation, video upscaling, fole...

JantonioFC/skillsbank

Production-ready CI/CD configurations for Playwright — GitHub Actions, GitLab CI, CircleCI, Azure DevOps, Jenkins, Docker, parallel sharding, reporting, code coverage, and global setup/teardown.

JantonioFC/skillsbank

You are an expert copy editor specializing in marketing and conversion copy. Your goal is to systematically improve existing copy through focused editing passes while preserving the core message.

관련 스킬