CC

Learn C – tutorial, cheatsheet & exercises

C was developed by Dennis Ritchie in 1972 to write the Unix operating system – and it has been the foundation of modern IT ever since: Linux, Windows and macOS kernels, databases (SQLite, PostgreSQL), browser engines, compilers and interpreters (including Python's!) are written entirely or partly in C. On microcontrollers and in device drivers, C is still the standard today.

C is small, fast and honest: there is no garbage collector, no classes, no exceptions. You see exactly what the computer does – and you are responsible for it. You manage memory yourself (malloc/free), work with pointers, and treat strings as char arrays. This is demanding, but that is exactly why C teaches you how computers really work.

This site takes you from "Hello World" through pointers, strings, structs and dynamic memory all the way to function pointers, bit operations and the typical security pitfalls (buffer overflows, undefined behavior). The code is only displayed here – compile and run it locally with gcc or clang; that is part of learning.

Operating systemsEmbeddedDriversPerformance-critical software

Hello World in C

#include <stdio.h>

int main(void) {
    printf("Hello World!\n");
    return 0;
}

main is the entry point, printf prints text. Without \n there would be no line break. Compile with: gcc -Wall -o hallo hallo.c.

How to get started with C

C programs are translated into machine code by a compiler. The two common compilers are gcc (GNU) and clang (LLVM). Both understand the same options, and the main examples here work with either.

macOS

xcode-select --install
cc --version

This command installs Apple's Command Line Tools (including clang, make, git, lldb). On macOS, gcc is actually just an alias for clang. Alternatively: brew install gcc.

Linux

sudo apt install build-essential gdb valgrind   # Debian/Ubuntu
sudo dnf install gcc make gdb valgrind            # Fedora
gcc --version

Windows

The easiest way is WSL (Windows Subsystem for Linux): you get a real Linux environment and work as described above. Alternatively, use MSYS2 with the MinGW compiler if you want to build native Windows programs.

wsl --install                      # PowerShell as administrator, then restart

# Alternative: install MSYS2 from msys2.org, then in the UCRT64 shell:
pacman -S mingw-w64-ucrt-x86_64-gcc make gdb
gcc --version

Compiling and running

Save the program as hallo.c, compile it and run the generated file:

gcc -Wall -Wextra -std=c17 -o hallo hallo.c
./hallo          # on Windows (MinGW): hallo.exe
TipAlways enable -Wall -Wextra: the compiler then warns about many typical mistakes (uninitialized variables, wrong printf formats, forgotten return values). For debugging, also add -g -fsanitize=address,undefined – more on that in the lesson on undefined behavior.
WarningIf ./hallo reports "Permission denied" or "command not found": do not forget the ./ – otherwise the shell only looks for programs in the PATH folders, not in the current directory.

C learning path

Beginner
Intermediate
Advanced

At a glance

First released1972
CreatorDennis Ritchie
ParadigmProcedural, imperative
TypingStatic, weak
ExecutionCompiled to machine code
File extension.c / .h

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

C compared

Hello World: C comparedVariables: C comparedConditionals: C comparedLoops: C comparedFunctions: C comparedLists and Arrays: C comparedDictionaries and Maps: C comparedClasses and Objects: C comparedError Handling: C comparedReading Files: C compared