Lesson Progress
0%
Lesson  ·  C in the Real World

C Isn't Old — It's Underneath Everything

C looks like a "beginner language" because it's usually taught first. In reality, it's running your car's brakes, your router, your phone's OS, and — surprisingly — the actual number-crunching engine inside every major AI framework you've heard of.

C in daily devices
Why AI needs C
Inside NumPy/PyTorch
Speed comparison
Python calling C
📖

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
💡 The pattern to notice: every item on that list needs to run fast and use memory predictably, on hardware that may have very little of either to spare. That's the exact niche C was designed to fill, and nothing has fully replaced it there.
example 1
1

Why Would AI — Built in Python — Need C at All?

The performance problem

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.

Your Python codemodel.fit(), simple and readable
AI Framework (PyTorch / TensorFlow / NumPy)Python-facing API
C / C++ corethe actual matrix math, compiled and fast
CPU / GPU hardwareexecutes the compiled instructions directly
💡 This is why "Python is used for AI" is only half the story. Python is the interface. C (and C++, and CUDA C for GPUs) is the engine. NumPy's core, much of TensorFlow, and parts of PyTorch are literally written in C/C++ for this reason.
example 2
2

Seeing the Speed Difference: A Dot Product

The same core operation, two languages

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.

Example 2 · dot_product.c
dot_product.c
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;
}
terminal
output
Dot product = 2.410
ApproachRoughly how it runsWhy
Pure Python loopSlowestEvery step re-checks variable types at runtime
NumPy (Python calling C)Much faster — often 10-100xThe loop above runs compiled, type-fixed C code
Hand-written CFastest of the threeNo interpreter overhead at all, direct to machine code
⚠️ This isn't a niche detail. A modern AI training run repeats operations like this dot product trillions of times. A 50x slowdown turns a 1-day training run into nearly 2 months — which is exactly the gap between "loop in Python" and "loop in C."
example 3
3

Seeing It Happen: Python Calling Your C Function

ctypes — the bridge

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.

Example 3 · fast_math.c (compiled to a shared library)
fast_math.c
C
// 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;
}
Example 3 · call_from_python.py
call_from_python.py
Python
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}")
terminal
output
Dot product (via C) = 2.410
💡 This is literally what "Python AI libraries" are. NumPy, PyTorch, and TensorFlow are, at their core, a large collection of C/C++ functions like dotProduct above, wrapped so Python code can call them conveniently. You just built a miniature version of that same bridge.
example 4
4

C on GPUs and Tiny Devices

CUDA C & TinyML

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.
EnvironmentWhy 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 roboticsPredictable timing — no garbage collector pauses
Autonomous vehicle sensorsHard millisecond deadlines for obstacle detection
quiz
Q

Quick Quiz

Question 1 of 5

Which of these is written substantially in C?

Question 2 of 5

Why do AI frameworks like NumPy and PyTorch use C/C++ underneath Python?

Question 3 of 5

What does the Python ctypes module let you do?

Question 4 of 5

What is CUDA C used for?

Question 5 of 5

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