Tsallaka zuwa abun ciki
Justice Tech School

C Tutorial

The fast, low level language behind operating systems.

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