Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 3 · LANGUAGES

How to Code in C/C++ Using AI

Use an AI assistant to write a small C function, compile it, and prove it with tests. C and C++ still power operating systems, embedded devices, games and high-speed services.

In short: say C11 or C++17, “no extra libraries”, give one example with the exact answer, ask for a tiny test file, then run gcc (or g++) yourself.

Why C/C++ with AI

WHY

Banks, telecom, cars and game engines still ship a lot of C and C++. AI writes the boilerplate fast, but memory bugs in these languages crash hard. You must compile with warnings on and run tests.

What you need

WHAT
  • A compiler: gcc/clang on Linux or Mac; MinGW or MSVC on Windows. Check with gcc --version.
  • Make (optional): a Makefile to build and test with one command.
  • IDE + assistant: VS Code with C/C++ extension + Copilot or Claude Code; or CLion with Copilot.
  • Sanitizers (later): AddressSanitizer catches many memory bugs: compile with -fsanitize=address.

How it flows

1 · Describe the function C11, no libs, example 2 · AI writes .c file read every line 3 · Compile with -Wall fix warnings 4 · Ask for a test file asserts 5 · Run the tests all pass = keep

Install and check

HOW
  1. Linux: sudo apt install build-essential (Debian/Ubuntu) or the equivalent for your distro.
  2. Mac: install Xcode Command Line Tools (xcode-select --install).
  3. Windows: install MinGW-w64 or use Visual Studio Build Tools.
  4. Check: gcc --version. For C++ also check g++ --version.

Step 1 — the prompt

COPY THIS PROMPT

Write a C11 function int is_valid_pin(const char *pin) that checks an Indian PIN code shape: exactly 6 digits after trimming spaces, first digit 1–9 (not 0). Return 1 if valid, 0 otherwise. No extra libraries. Example: "411001" → 1, "000001" → 0. Only return the .c file with the function.

Step 2 — the code (tested here)

pin.c
/* pin.c — Indian PIN code shape check (6 digits, first 1–9). */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int is_valid_pin(const char *pin) {
    if (pin == NULL) return 0;
    /* skip leading spaces */
    while (*pin && isspace((unsigned char)*pin)) pin++;
    size_t n = 0;
    while (pin[n] && !isspace((unsigned char)pin[n])) n++;
    if (n != 6) return 0;
    if (pin[0] < '1' || pin[0] > '9') return 0;
    for (size_t i = 1; i < 6; i++) {
        if (pin[i] < '0' || pin[i] > '9') return 0;
    }
    return 1;
}

#ifdef PIN_MAIN
int main(void) {
    printf("%d\n", is_valid_pin("411001")); /* Pune sample */
    return 0;
}
#endif
gcc -Wall -Wextra -std=c11 -DPIN_MAIN -o pin pin.c
./pin
# -> 1

Step 3 — ask for tests, then run them

FOLLOW-UP PROMPT

Write a separate test_pin.c that uses assert() for: valid Pune and Delhi PINs, leading zero, too short, a letter, trimmed spaces, and NULL. Print how many checks passed.

test_pin.c
/* test_pin.c — tiny assert tests for is_valid_pin */
#include <stdio.h>
#include <assert.h>

int is_valid_pin(const char *pin);

int main(void) {
    assert(is_valid_pin("411001") == 1);   /* Pune */
    assert(is_valid_pin("110001") == 1);   /* Delhi */
    assert(is_valid_pin("000001") == 0);   /* first digit 0 */
    assert(is_valid_pin("41100") == 0);    /* too short */
    assert(is_valid_pin("411001a") == 0);  /* letter */
    assert(is_valid_pin(" 411001 ") == 1); /* trim spaces */
    assert(is_valid_pin(NULL) == 0);
    printf("7 checks passed\n");
    return 0;
}
gcc -Wall -Wextra -std=c11 -o test_pin pin.c test_pin.c
./test_pin
# -> 7 checks passed
Note: this only checks the shape of a PIN. It does not prove the PIN exists in the India Post database.

C++ tip

  1. For C++, say “C++17, use std::string_view, no raw new” in the prompt.
  2. Compile with g++ -Wall -Wextra -std=c++17.
  3. Prefer std::optional / exceptions for errors in library code; avoid silent failures.

Settings and config

  1. Rules file: “C11/C++17, -Wall -Wextra, no gets(), check every malloc, add a small test file”.
  2. VS Code: install the Microsoft C/C++ extension so IntelliSense and compile errors show in the editor.
  3. Turn warnings into errors in CI: -Werror.

Watch out (common AI mistakes in C/C++)

  • Buffer overflows — always pass a max length; prefer snprintf over sprintf.
  • Forgetting to free — or double-free; ask for clear ownership rules.
  • Wrong size with sizeof on pointers vs arrays.
  • Mixing C and C++ headers — say which language in the prompt.
  • Ignoring compiler warnings — fix every warning before you trust the code.

Quick quiz

1. What does is_valid_pin("411001") return in our example?

1 (valid Pune-shaped PIN).

2. Why reject a PIN starting with 0?

Indian PIN codes use first digit 1–9.

3. Which gcc flags catch many mistakes early?

-Wall -Wextra (and -Werror in CI).

4. Does our function prove the PIN is a real post office code?

No. It only checks the 6-digit shape.

Save tokens and money

SMART AI USE

Ask for one function and one test file. Paste only the first compiler error; fixing it often clears the next ones. Prefer local compile over sending the whole project to chat.

Save these cards

INTERVIEW CARDAI Coding

How do you check AI-written C code?

Compile with -Wall -Wextra; fix every warning.
Run a small assert test file; use sanitizers for memory bugs.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

C vs C++ prompts

C: say C11, no libs, explicit sizes.
C++: say C++17, std::string_view, no raw new.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

C with AI in 3

Shape + example in the prompt.
gcc -Wall, then assert tests.
Never ship with warnings ignored.
Ravindra Bagale · ravindrabagale.com/ai-coding