Langsung ke konten
Skillpedia
Pemrograman umum48k Β· #5887

c

Fungsinya

Panduan ini mengajarkan praktik terbaik dalam menulis kode dengan bahasa pemrograman C. Panduan ini menunjukkan cara mengelola memori, menggunakan pointer dan array dengan benar, menangani error, serta memanipulasi string dengan aman dan efisien.

Kapan dipakai

  • βœ“Saat menulis kode C untuk pertama kalinya.
  • βœ“Saat Anda menemui error yang sulit dipahami di kode C Anda.
  • βœ“Untuk membuat kode C Anda lebih aman dan lebih cepat.

Link skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/c/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/c/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBagaimana cara menghindari kesalahan umum saat mengelola memori di C?”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

AI membandingkan contoh kode C yang 'buruk' dengan yang 'baik', lalu menjelaskan perbedaannya. Fokusnya adalah keamanan, efisiensi, dan kejelasan agar Anda bisa menulis kode yang lebih baik.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari 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

  1. Memory Management
  2. Pointers & Arrays
  3. Error Handling
  4. Strings
  5. Structs & Enums
  6. Preprocessor & Headers
  7. 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

…

Pertanyaan umum

Apa itu 'pointer' di C?

Pointer seperti alamat memori yang menunjukkan di mana data disimpan di komputer. Jika digunakan dengan cara yang salah, pointer bisa menyebabkan error.

Mengapa `snprintf` lebih baik daripada `sprintf`?

`snprintf` memungkinkan Anda menentukan ukuran maksimum teks yang akan ditulis, sehingga teks tidak 'meluap' dan merusak bagian lain dari program Anda. Ini adalah kesalahan yang umum dan berbahaya.

Apa itu 'memory management'?

Memory management atau manajemen memori adalah proses mengalokasikan ruang di memori untuk data saat dibutuhkan, lalu membebaskan ruang tersebut ketika sudah tidak dipakai, agar tidak terjadi pemborosan dan error.

Detail

Deskripsi asli dari pembuat

Language-specific super-code guidelines for c.

repo: sickn33/agentic-awesome-skills

Popularitas

Bintang GitHub
48k
Peringkat
#5887
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa