Debugging and the final project
Here you are, you have reached the last week. Now we learn every programmer's daily work, debugging, that is, finding and fixing errors. Then we bring together everything you have learned and see how a final project is built. This is the end of your journey and at the same time the beginning of the real path of a programmer.
What you will learn in this lesson
9.1 What is debugging?
Debugging (fixing errors) means the process of finding and correcting errors (bugs) in a program. No programmer in the world writes code without mistakes. In fact, a large part of a programmer's working time goes not into writing new code but into fixing existing code. So being able to debug is one of a programmer's most important skills.
Here is the good news: debugging is a skill you can learn. With an orderly approach, any bug can be found. This week we go over the error types, the ways to find them and the most common mistakes.
9.2 Error types
Errors fall into three main types, and we find each one differently:
- Syntax error: a language rule is broken, for example a semicolon is forgotten. The compiler does not create the program at all.
- Runtime error: the program starts running, but crashes while it runs, for example going out of an array's bounds.
- Logic error: the program runs and does not crash, but the result is wrong. These are the hardest to find, because the compiler does not help.
Below, choose the correct type for each case:
9.3 Reading an error message
With a syntax error the compiler helps you: it tells you which line the error is on and what it is. At first these messages feel scary, but once you learn to read them, debugging becomes much easier. The message below contains the file name, the line number and the error description:
Let us break it into parts: salom.c is the file, 4 is the line, 18 is the column, and expected ';' means "a semicolon was expected here". So on line 4, before return, we forgot to put a ;.
9.4 Debugging with printf
With a logic error the compiler stays silent: the program runs, but the answer is wrong. The simplest and most powerful method is debugging with printf: you put a printf in suspicious places and see the values of the variables. The program below should add up from 1 to 5 (the answer is 15), but it prints 10. Why?
Click the Add printf button and watch the value of sum at each step:
9.5 Fix the bug
Now it is practice time. Below are the three most common mistakes. Each one is marked in red. Choose the correct fix, and the code turns green:
9.6 Debugger
printf is good, but there is also a special tool: the debugger. It runs the program line by line, stepping through it, and at each step shows the values of all the variables. In fact, this is very much like the live tracers we used throughout the whole course: the loop tracer in week 3, the call stack tracer in week 8, all of these are the idea of a debugger.
The main operations in a debugger:
- Step: execute one line and move to the next
- Breakpoint: stop the program at a specific line
- Watch: continuously keep an eye on the value of some variable
9.7 The final project: planning
Now the second part: building a big project. Beginners often jump straight into writing code and get confused. An experienced programmer, however, first plans: they break a big task into small, doable steps.
For example, suppose we want to make a simple "To-do list" program. We break it down like this:
- Choose a data structure to store the tasks (an array or a list)
- A function to add a new task
- A function to print the tasks to the screen
- Mark a task as done
- Save everything to a file and read it back later
Notice this: every step in this list is exactly the things you learned in this course. Arrays, functions, data structures, files. A big project is simply assembled from small, familiar parts.
9.8 Building step by step
Once the plan is ready, we do not write it all at once. We build it step by step: we write one small piece, test it, and if it works we move to the next. This is the most important habit, because if a bug appears, it will only be in the last small piece you wrote, and it is easy to find.
Practical rules:
- First make the simplest working version, then improve it
- After each new piece, run the program and test it
- Change only one thing at a time, so it is clear where the bug is
- Keep saving the working version (more on git below)
9.9 Wrapping up the journey
Nine weeks ago, you probably knew nothing about programming. Today you know everything from how a computer works to recursion and data structures. See the whole path below: click each week and recall what you learned:
With this foundation, many paths are now open: building your own projects, learning a new language (Python, JavaScript) or deepening this knowledge. Most importantly, you have learned programming, which means you have learned how to teach yourself.
Make a prediction
How many times does this loop print a star to the terminal?
9.10 Going deeper advanced
Two more useful concepts to keep going on your path.
Version control (git)
Git is a tool that keeps the history of your code. You save every working version, and if a new change breaks your code, you easily roll back to a working state. In addition, through services like GitHub you can work on code together with others. Every serious programmer uses git.
Where to go next
Since you know C, other languages are now much easier for you. Python is simple and popular for artificial intelligence. JavaScript is for websites. Or, right here, you can go deeper into building games and systems with C. Most importantly, constant practice: building small projects, writing a bit of code every day.
Glossary of terms
9.11 Final quiz
16 questions, a general review of the whole course. To complete Foundation, answer at least 11 of them correctly.
Congratulations! You have completed the Foundation course!
Over 9 weeks you started from zero and walked the path from how a computer works to algorithms, pointers, data structures and recursion. This is a serious achievement. Now you have a real programming foundation.
This is not the end, but the beginning. May your path be open!