C

C was created by Dennis Ritchie at Bell Labs around 1972 to write the Unix operating system. It has a small set of structured statements, functions, and direct access to memory through pointers. Most of the software world is built on it or on languages it inspired.

Paradigm: Structured Programming

What Makes C Special

  • A tiny language. The core is small: if, while, for, switch, functions and a handful of types. Everything else, including input and output, is a library.
  • Pointers and manual memory. You ask for memory with malloc and give it back with free. A pointer is an address, and arithmetic on it is allowed. This is power and danger together.
  • Close to the machine. Types map to hardware words, structs have a predictable layout, and the compiler emits little you did not ask for.
  • Portable assembly. C runs almost everywhere and is used as the lingua franca for calling between languages (the "C ABI").
  • No safety net. There are no bounds checks, and mistakes become undefined behavior: crashes, corruption, or security holes. Tools such as compiler warnings and sanitizers are essential.

Example: Allocating and using an array

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

int main(void) {
    int n = 5;
    int *squares = malloc(n * sizeof *squares);
    if (squares == NULL) {
        return 1;
    }

    for (int i = 0; i < n; i++) {
        squares[i] = i * i;
    }
    for (int i = 0; i < n; i++) {
        printf("%d ", squares[i]);
    }
    printf("\n");

    free(squares);
    return 0;
}

How It Works

  • malloc reserves room for five int values on the heap and returns a pointer to it, or NULL when memory is exhausted.
  • The two for loops are structured repetition; the if before them is a structured decision that handles failure.
  • squares[i] is shorthand for pointer arithmetic: the address of the first element plus i elements.
  • free returns the memory. Forgetting it leaks memory, and using the pointer afterwards is a bug. The program prints 0 1 4 9 16.

History and Where It Is Used

C powers operating system kernels (Linux, Windows, macOS), databases, language runtimes, embedded firmware and network stacks. Learning it teaches how memory, the stack and the compiler really work, which helps in every other language. See the C roadmap for a complete course.

Learn More