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.
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 --versionThis 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 --versionWindows
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 --versionCompiling 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-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../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
Hello World, main & the Compiler
Your first C program, its parts and the path from source code to an executable file.
Variables, Data Types & Arithmetic
int, double, char, bool, sizeof, constants and the pitfalls of integer division.
Input and Output: printf, scanf, fgets
Formatted output and safe keyboard input.
Conditions: if, switch & Operators
Branching, comparisons, logical operators and the ternary operator.
Loops: for, while, do-while
Repetition, exit conditions, break/continue and nested loops.
Functions, Scope & Recursion
Defining and declaring functions, call by value, static variables and recursion.
Arrays
Fixed-size arrays: indexing, initialization, passing to functions and multidimensional arrays.
Pointers: Addresses & Dereferencing
What a pointer is, call by reference, pointer arithmetic and the relationship with arrays.
Strings as char Arrays
Null-terminated strings, string.h, safe copy and format functions.
structs, typedef, enum & union
Building your own data types: structures, enumerations, variants and access via pointers.
Dynamic Memory: malloc & free
Requesting, resizing and releasing memory at runtime – and avoiding typical mistakes.
Files & Error Handling
Reading and writing files with stdio, checking return codes, errno and cleaning up with goto.
Preprocessor, Headers & Multiple Files
#define, macros and their pitfalls, include guards, splitting into .h/.c and a Makefile.
Function Pointers & Callbacks
Functions as values: callbacks, qsort and dispatch tables.
Data Structures: List & Stack
Linked list with pointer to pointer, array-based stack and ownership rules for memory.
Bit Operations & Fixed-Width Integer Types
Setting, clearing and testing bits; packing flags and color values; stdint.h.
Memory Layout, Padding & Qualifiers
Stack, heap and static memory; struct alignment; const, volatile and restrict.
Undefined Behavior, Security & Debugging
Recognizing and avoiding buffer overflows and other pitfalls, and tracking them down with sanitizers, Valgrind and gdb.
At a glance
More about C
Test & practice
Interactive quiz, timed exam with certificate, and exercises with an editor: