C

From G1Joshi/Agent-Skills · View interactive version

C is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert C programming assistance covering pointers, manual memory management, struct layout, and POSIX APIs. Use when developing systems software, embedded firmware, high-performance libraries, or OS kernels.

How to use this skill

Skill file

C

The foundational language for modern computing, operating systems, and embedded systems.

When to Use

Quick Start

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Core Concepts

Explicit Memory Management & Pointer Arithmetic

Direct manipulation of memory addresses, stack allocation, and heap lifetimes:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    size_t length;
    char *data;
} StringBuffer;

StringBuffer* buffer_create(const char *initial_str) {
    StringBuffer *buf = malloc(sizeof(StringBuffer));
    if (!buf) return NULL;

    buf->length = strlen(initial_str);
    buf->data = malloc(buf->length + 1);
    if (!buf->data) {
        free(buf);
        return NULL;
    }
    memcpy(buf->data, initial_str, buf->length + 1);
    return buf;
}

void buffer_free(StringBuffer *buf) {
    if (buf) {
        free(buf->data);
        free(buf);
    }
}

Modern C Standards (C23 / C17) Features

C23 introduces standardized bool, nullptr, and auto type inference:

// Modern C23 syntax
#include <stdbool.h>

int main(void) {
    bool is_active = true;
    void *ptr = nullptr; // C23 standardized nullptr
    return is_active ? 0 : 1;
}

Memory Sanitizers (ASan & UBSan)

Instruments binary builds to detect buffer overflows, use-after-free, and undefined behavior at runtime:

# Compile with AddressSanitizer and UndefinedBehaviorSanitizer
gcc -std=c17 -Wall -Wextra -Werror -fsanitize=address,undefined -g -O1 main.c -o app

Common Patterns

Safe Dynamic Allocation with Cleanup Labels

Problem: Multi-step initialization with multiple malloc calls causes memory leaks if middle steps fail.

Solution:

Use single exit goto cleanup pattern for resource management:

#include <stdlib.h>
#include <stdio.h>

int process_data(size_t n) {
    int *buf1 = malloc(n * sizeof(int));
    if (!buf1) return -1;

    char *buf2 = malloc(n * sizeof(char));
    if (!buf2) {
        free(buf1);
        return -1;
    }

    // Work with buffers
    int status = 0;

    free(buf2);
    free(buf1);
    return status;
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------------- | :------------------------------------------------------------ | :----------------------------------------------------------------- |

| Segmentation fault | Null pointer dereference, buffer overflow, or use-after-free. | Compile with AddressSanitizer: gcc -fsanitize=address -g main.c. |

| double free or corruption | Calling free() twice on the same memory pointer. | Set pointer to NULL immediately after calling free(ptr). |

| implicit declaration of function | Missing header file #include declaring the target function. | Include the header file matching the C standard library function. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.