Week 9 · Final

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

What an error (bug) is and how to tell its three main types apart
Read compiler error messages
Find bugs with printf and a debugger
Recognize and fix the most common mistakes
Plan a project and build it step by step
Bring together everything you learned across the whole course

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.

The history of the word "bug" is interesting: in 1947 a real insect (a moth) that got inside a computer caused a malfunction. Ever since, an error in a program is called a "bug", and fixing it is called "debugging".

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:

Classify the error choose the type
Click the correct error 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:

terminal
1salom.c:4:18: error: expected ';' before 'return'
2 return 0
3 ^

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 ;.

The most important tip: read and fix the first error, then recompile. Often one error causes many later "errors", and once you fix the first one the rest disappear.

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:

Finding the bug with printf add printf
yigindi.c
1int sum = 0;
2for (int i = 1; i < 5; i++)
3 sum = sum + i;
terminal

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:

Fix the bug choose the correct fix
bug:
tekshir.c

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
There is also a most powerful yet simplest method: the rubber duck. Explain your code line by line out loud to someone (or to a rubber duck). Often, in the process of explaining, you find the bug yourself.

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)
Do not try to write perfect code all at once. Even experienced programmers first make a working, simple version and then improve it gradually. There is a rule: "make it work first, then make it pretty".

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:

Your journey click a week

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?

main.c
1for (int i = 0; i <= 3; i++)
2 printf("*");
3 times
4 times
5 times

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.

In programming there is never a point where you can say "I have learned it all". Even programmers with 20 years of experience learn something new every day. This is a path of constant learning, and you have now set out on it.

Glossary of terms

bugan error in a program.
debuggingthe process of finding and fixing errors.
syntax errora language rule is broken, the compiler finds it.
logic errorthe program runs, but the result is wrong.
breakpointa point that stops the program at a specific line.
gitversion control that keeps the history of code.

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!