Where C Actually Runs, Right Now
C was built in 1972 for one job: talking directly to hardware with almost no overhead. That exact quality is why, more than 50 years later, it's still the language of choice anywhere speed, memory control, and predictability matter more than convenience.
- Operating systems — Windows, Linux, and macOS all have kernels written substantially in C
- Embedded devices — microwave controllers, car ECUs (engine control units), traffic lights, ATMs, elevators
- Networking hardware — routers, modems, and the firmware inside your Wi-Fi chip
- Databases — MySQL, PostgreSQL, SQLite, and Redis are written in C
- Other languages' interpreters — Python itself (CPython) is implemented in C
Why Would AI — Built in Python — Need C at All?
Almost every AI tutorial you'll see uses Python. But training a neural network means doing billions of multiplications and additions on huge grids of numbers (matrices). Python, as a language, is convenient specifically because it hides low-level details — and hiding those details makes each individual operation slower.
The fix the AI world settled on: write the code in Python, but make the actual number-crunching loops run in compiled C code underneath. You get Python's easy syntax on top, and C's raw speed doing the real work underneath.
Seeing the Speed Difference: A Dot Product
A "dot product" — multiplying matching elements of two lists and adding the results — is one of the single most repeated operations in AI (it's the core of every neural network layer). Here's the same operation written in C.
#include <stdio.h> float dotProduct(float a[], float b[], int n) { float result = 0; for (int i = 0; i < n; i++) { result += a[i] * b[i]; // the operation neural networks repeat billions of times } return result; } int main() { float weights[] = {0.5, 1.2, -0.3, 2.0}; float inputs[] = {1.0, 0.8, 2.5, 0.1}; printf("Dot product = %.3f\n", dotProduct(weights, inputs, 4)); return 0; }
Dot product = 2.410
| Approach | Roughly how it runs | Why |
|---|---|---|
| Pure Python loop | Slowest | Every step re-checks variable types at runtime |
| NumPy (Python calling C) | Much faster — often 10-100x | The loop above runs compiled, type-fixed C code |
| Hand-written C | Fastest of the three | No interpreter overhead at all, direct to machine code |
Seeing It Happen: Python Calling Your C Function
This is exactly the mechanism NumPy and PyTorch use internally, just visible at a small scale. Compile a C function into a shared library, then load and call it directly from Python using the built-in ctypes module — no separate AI framework required to see the pattern.
// compiled with: gcc -shared -o fast_math.so -fPIC fast_math.c float dotProduct(float *a, float *b, int n) { float result = 0; for (int i = 0; i < n; i++) result += a[i] * b[i]; return result; }
import ctypes # load the compiled C library, straight from Python lib = ctypes.CDLL("./fast_math.so") lib.dotProduct.restype = ctypes.c_float a = (ctypes.c_float * 4)(0.5, 1.2, -0.3, 2.0) b = (ctypes.c_float * 4)(1.0, 0.8, 2.5, 0.1) result = lib.dotProduct(a, b, 4) print(f"Dot product (via C) = {result:.3f}")
Dot product (via C) = 2.410
dotProduct above, wrapped so Python code can call them conveniently. You just built a miniature version of that same bridge.C on GPUs and Tiny Devices
C's reach into AI goes even further than CPU libraries:
- CUDA C — NVIDIA's language for programming GPUs is a direct extension of C. When your AI model "runs on the GPU," CUDA C is what's actually issuing instructions to the thousands of GPU cores.
- TinyML / Embedded AI — running a small AI model on a microcontroller (a smart doorbell, a wearable, a factory sensor) leaves no room for Python's overhead. These models are deployed as compact C code that runs directly on the chip.
- Robotics & drones — real-time obstacle avoidance can't tolerate an unpredictable pause for garbage collection; C's predictable timing is why it still shows up in safety-critical AI control loops.
| Environment | Why C (or CUDA C) is used |
|---|---|
| GPU training (CUDA C) | Direct hardware-level control over thousands of parallel cores |
| Microcontrollers (TinyML) | Kilobytes of memory, no room for a Python interpreter |
| Real-time robotics | Predictable timing — no garbage collector pauses |
| Autonomous vehicle sensors | Hard millisecond deadlines for obstacle detection |
Quick Quiz
Which of these is written substantially in C?
Why do AI frameworks like NumPy and PyTorch use C/C++ underneath Python?
What does the Python ctypes module let you do?
What is CUDA C used for?
Why is C used in TinyML on microcontrollers instead of Python?
Lesson Checklist
- I can name 3 everyday devices or systems that run on C
- I understand why raw Python loops are slower than compiled C loops
- I can describe the layered stack: Python API → C/C++ core → hardware
- I understand what a dot product is and why AI repeats it constantly
- I understand how ctypes lets Python call a compiled C function
- I know what CUDA C is used for
- I understand why TinyML/embedded AI still relies on C
- I completed the quiz