C Tutorial
The fast, low level language behind operating systems.
Getting started5 lessons
Basics23 lessons
- 6Variables and TypesDeclaring values.
- 7Data Types and sizeofHow much memory each type uses.
- 8Type ConversionImplicit and explicit conversion.
- 9OperatorsArithmetic, comparison and logic.
- 10Booleanstrue and false with stdbool.h.
- 11User Inputscanf and fgets.
- 12printf and Format Specifiersprintf prints formatted text.
- 13Constants and Macrosconst makes a variable read only.
- 14Integer Types and LimitsC has char, short, int, long and long long, each signed or unsigned.
- 15Floating Point Numbersfloat, double and long double store decimals with limited precision, so 0.1 + 0.2 is not e.
- 16Characters and ctypechar holds a small integer that represents a character.
- 17Bitwise Operators& | ^ ~ << >> work on individual bits.
- 18Variables and TypesDeclare a variable with a type before use.
- 19sizeof Operatorsizeof gives the size in bytes of a type or variable.
- 20Integer and Float DivisionDividing two ints drops the fraction.
- 21Reading Input with scanfscanf reads formatted input.
- 22Strings as char ArraysA C string is a char array ending with \0.
- 23string.h Functionsstrlen, strcpy, strcat, strcmp and strchr work on C strings.
- 24math.h Functionssqrt, pow, fabs, ceil, floor and round need #include <math.h>.
- 25Constants and enumconst makes values read only.
- 26OperatorsArithmetic, comparison, logical, bitwise and assignment operators.
- 27Type CastingCast with (type).
- 28printf FormattingWidth, precision and flags control output: %5d, %-5d, %05d, %.3f, %x, %o and %%.
Control flow13 lessons
- 29If ... ElseMaking decisions.
- 30SwitchChoosing between many values.
- 31Loopswhile, do...while and for.
- 32Ternary, Comma and gotocond ? a : b is a short if expression.
- 33do while, break and continuedo while runs the body at least once.
- 34Nested LoopsLoops inside loops build tables and patterns.
- 35if, else if and elseAny non zero value is true.
- 36switch and breakswitch compares an integer with cases.
- 37for, while and do whilefor repeats a known number of times, while checks first and do while runs once at least.
- 38break and continuebreak exits a loop and continue skips to the next round.
- 39Nested Loops and PatternsLoops inside loops draw tables and shapes.
- 40Conditional Operatorcond ? a : b is a short if expression.
- 41Example: Primes and GCDCombine loops and functions to solve small problems.
Data and functions21 lessons
- 42ArraysFixed size lists.
- 43StringsArrays of characters.
- 44FunctionsReusable blocks of code.
- 45RecursionA function calling itself.
- 46Two Dimensional ArraysA 2D array is an array of arrays, stored row by row.
- 47String Functions<string.h> provides strlen, strcpy, strncpy, strcat, strcmp, strchr and strstr.
- 48Converting Strings and Numbersatoi, strtol and strtod turn text into numbers, and snprintf builds strings safely.
- 49Function Prototypes and HeadersA prototype declares a function before it is defined.
- 50Scope, static and externLocals live inside their block.
- 51Function Pointers and CallbacksA function pointer stores the address of a function.
- 52Variadic Functions<stdarg.h> lets a function accept a variable number of arguments, like printf.
- 53ArraysArrays hold items of one type in a row.
- 54Passing Arrays to FunctionsAn array decays to a pointer, so pass its length too.
- 55FunctionsDeclare return type, name and parameters.
- 56Pass by Value and PointersC copies arguments.
- 57RecursionA function can call itself with a base case.
- 58static Local Variablesstatic inside a function keeps its value between calls.
- 59Two Dimensional ArraysArrays of arrays model grids.
- 60ctype Functionsisalpha, isdigit, isspace, toupper and tolower test and convert characters.
- 61Converting Stringsatoi, strtol and strtod parse numbers.
- 62Bitwise Operators& | ^ ~ << >> work on bits.
Memory17 lessons
- 63PointersVariables that hold addresses.
- 64Pointers and FunctionsChanging values through a function.
- 65Pointer Arithmetic and ArraysMoving through memory.
- 66Dynamic Memorymalloc and free.
- 67Pointers to Pointers and constA pointer can point to another pointer.
- 68Arrays and Pointers TogetherAn array name decays to a pointer to its first element.
- 69Common Memory BugsTypical bugs: buffer overflow, use after free, double free, memory leaks, uninitialised va.
- 70calloc, realloc and Dynamic Arrayscalloc allocates zeroed memory, and realloc resizes a block.
- 71memcpy, memset and memmovememcpy copies bytes between non overlapping blocks, memmove handles overlap, memset fills .
- 72PointersA pointer stores an address.
- 73Pointer ArithmeticAdding to a pointer moves by whole elements.
- 74malloc and freemalloc reserves heap memory and free releases it.
- 75calloc and realloccalloc gives zeroed memory and realloc resizes a block.
- 76Dynamic StringsAllocate length + 1 for the terminator.
- 77NULL and Dangling PointersSet pointers to NULL after free and check before use.
- 78Pointers to PointersA pointer can point to another pointer.
- 79memcpy and memsetmemcpy copies bytes and memset fills them.
Structures and files23 lessons
- 80StructuresGrouping related data.
- 81Enums and UnionsNamed constants and shared memory.
- 82File HandlingReading and writing files.
- 83The Preprocessor and math.h#define, #include and maths functions.
- 84Structs and PointersUse -> to reach fields through a struct pointer.
- 85Linked ListsA linked list is made of nodes that each hold a value and a pointer to the next node.
- 86Unions and Bit FieldsA union stores different types in the same memory.
- 87Reading and Writing Text Filesfopen opens a file with a mode ("r", "w", "a").
- 88Binary Filesfread and fwrite move raw bytes.
- 89Error Handling with errnoC has no exceptions.
- 90Structuresstruct groups related fields.
- 91typedeftypedef gives a type a new name.
- 92Arrays of StructuresAn array of structs stores many records.
- 93Structures and FunctionsPass a pointer to a struct to avoid copying and to change it.
- 94UnionsA union shares memory between members.
- 95Linked ListNodes hold data and a pointer to the next node.
- 96Writing and Reading Filesfopen opens a file, fprintf and fgets write and read, fclose closes.
- 97Command Line Argumentsmain can take argc and argv.
- 98Error HandlingCheck return values.
- 99Header Files and ModulesPut declarations in .h files and definitions in .c files.
- 100Practice: Word CounterCount words in a string with a loop and a state flag.
- 101Practice: Reverse a StringSwap characters from both ends.
- 102Practice: Bubble SortRepeatedly swap neighbours that are out of order.
Advanced CPremium7 lessons
- 103sizeof, Padding and AlignmentThe compiler pads structs so fields are aligned.
- 104Multiple Files and MakefilesSplit code into .c and .h files and compile each to an object file.
- 105Enums and Flag SetsEnums name integer constants.
- 106Project: Hash TableA hash table maps keys to values by hashing the key to an index.
- 107Threads with pthreadsPOSIX threads run code concurrently.
- 108Processes and Signalsfork creates a child process, exec runs a program and wait collects it.
- 109Sockets OverviewSockets let programs talk over a network.
C in practicePremium5 lessons
- 110Debugging with gdb and valgrindCompile with -g, then use gdb to set breakpoints, step and inspect variables.
- 111Testing and assertassert from <assert.h> aborts when a condition is false and is removed with -DNDEBUG.
- 112Writing Portable and Safe CUse fixed width types, check every return value, avoid gets and unbounded strcpy, free wha.
- 113C for Embedded SystemsC controls microcontrollers such as Arduino and ARM boards.
- 114C Best PracticesInitialise variables, check allocations and file results, keep functions short, use const,.