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
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
- 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
Install and check
- Linux:
sudo apt install build-essential(Debian/Ubuntu) or the equivalent for your distro. - Mac: install Xcode Command Line Tools (
xcode-select --install). - Windows: install MinGW-w64 or use Visual Studio Build Tools.
- Check:
gcc --version. For C++ also checkg++ --version.
Step 1 — the 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;
}
#endifgcc -Wall -Wextra -std=c11 -DPIN_MAIN -o pin pin.c
./pin
# -> 1Step 3 — ask for tests, then run them
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 — 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 passedC++ tip
- For C++, say “C++17, use
std::string_view, no rawnew” in the prompt. - Compile with
g++ -Wall -Wextra -std=c++17. - Prefer
std::optional/ exceptions for errors in library code; avoid silent failures.
Settings and config
- Rules file: “C11/C++17, -Wall -Wextra, no gets(), check every malloc, add a small test file”.
- VS Code: install the Microsoft C/C++ extension so IntelliSense and compile errors show in the editor.
- Turn warnings into errors in CI:
-Werror.
Watch out (common AI mistakes in C/C++)
- Buffer overflows — always pass a max length; prefer
snprintfoversprintf. - Forgetting to free — or double-free; ask for clear ownership rules.
- Wrong size with
sizeofon 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
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.