Skip to content
BENIBIX Learn

Print Debugging

Development🛠️ Editors & ToolsCode Quality Tools

Also known as Printf Debugging, Debug Print Statements, console.log Debugging

Adding lines that print values as the code runs. They show where it goes wrong.

Think of it like

It is like leaving notes on a trail. Each note says I was here, and this is what I saw.

Example

Jack's shopping cart always shows a total of 0. He adds a print line before and after the sum. The first one already prints a price of 0, so the bug is where the price is read in.

Why it matters

It works in any language and on any machine. It even works where a debugger cannot reach.

Definition

Print debugging is finding a bug by adding lines that print values while the program runs. You put a print line at each step you are unsure about. When you run the code, the lines show each value and the order the steps ran in. The first value that looks wrong points to the bug. Once the bug is fixed, you take the extra lines out again.

Example code

python
price = read_price()
print("DEBUG price:", price)
total = price * qty
print("DEBUG total:", total)
Running it prints DEBUG price: 0, so the price is wrong before the total is worked out.
Print lines show each value as the code runs. The first wrong one points to the bug.

Common mistake

Forgetting to take the print lines out. They clutter the output, and they can leak private data into logs.

Key takeaways

  1. 1
    Print the values. At each step you doubt.
  2. 2
    Read the order. The output shows what ran.
  3. 3
    Clean up. Delete the lines once it is fixed.

Compare

Learn it properly