Solved C exercises (IC2)
Every solved C exercise on SergioLearns, by topic and in the order of IC2: types and variables, conditionals, loops, functions and pointers, then dynamic memory, structs and strings, the preprocessor, processes and system calls. Each topic comes with challenges from easy to hard, commented solutions with the real gcc output, and its cheat sheet.
sergio@learns:~$ ls -l ~/exercises/c --sort=level
total 12 topics · 41 exercises
[ OK ] every solution is commented and was run before publishing
Exercises by topic
From scratchIC24 topics
IntermediateIC28 topics
- pointers.cPointerstheoryguided practicecheat sheet
- functions.cFunctionstheoryguided practicecheat sheet
- dynamic-memory.cDynamic memorytheoryguided practicecheat sheet
- structs.cStructstheoryguided practicecheat sheet
- preprocessor.cPreprocessortheoryguided practicecheat sheet
- processes.cProcessestheoryguided practicecheat sheet
- system-calls.cSystem callstheoryguided practicecheat sheet
- threads.cThreadstheoryguided practicecheat sheet
How each exercises article works
They all follow the same pattern: “How to use this article”, challenges on three levels (basic, intermediate and a final challenge), three “What’s wrong here?” snippets, the commented solutions at the end, the Python Tutor section and the cheat sheet. Every solution is compiled with gcc and run before it is published, and the output you see is the one it really gave.
In the list above each line is a topic with its number of exercises and its name, which takes you to the article; next to it, the topic’s guided practice and its cheat sheet. The C topics have no multiple-choice quiz (that is why there is a dash in that column): here what counts is writing, compiling and reading code. The list updates itself when I publish a new topic.
Which order to do them in
First the basics, which look like what you already know from Python and Java: types and variables, conditionals, loops and functions. Then comes what makes C different: pointers. Don’t move on until you understand them, because dynamic memory, arrays of structs, strings and system calls all rely on them. At the end, the preprocessor and processes with fork and exec.
With the exam close, do the final challenge of each topic without looking. If it resists, go back to that topic’s basic ones before moving on.
How to practice so it sticks
Always compile with warnings, for example gcc -Wall -Wextra program.c -o program, and don’t call an exercise finished while a warning shows up: in C, many serious bugs start out as just a warning. If you don’t have Linux at hand, the C playground compiles and runs in your browser, and if you want a real one, here is how to install Fedora on VirtualBox.
Before you open each “What’s wrong here?”, try to say what the program does. In C a bug doesn’t always give an error: sometimes it prints garbage, or works on your computer and fails on another, and learning to spot it by reading is what saves the most time.
The cheat sheet and Python Tutor (which also speaks C)
Each topic’s cheat sheet puts the syntax, the standard library functions of the topic and the typical mistakes on one page, to revise the night before. They are all together in Resources.
Python Tutor runs C step by step and draws the stack, the heap and where every pointer points. In “See it step by step in Python Tutor” you have the code ready to open. With pointers and dynamic memory it is by far the quickest way to understand what is going on.
For the practical part of the exam you also have the IC2 exams and mock exams.
IC2 is not only C
IC2 also covers Linux, networks and operating systems, and that part has solved exercises too. To start with operating systems: what is an operating system exercises, interrupts and memory hierarchy exercises and processes and threads exercises, which fit with what you do here with fork.
If you are coming from first year, the solved Python exercises of FP1 and the solved Java exercises of FP2 are still there to revise. Everything else for practising is in Practice.

IC2 practical mock exam — flowcharts, fork and threads in C (and the screenshots you can’t leave out)