Syntax Errors and Debugging

Computers don't actually speak C++ so we have to compile (translate) C++ source files that we write into class files which is code that a computer can understand and run.

Syntax errors are reported to you by the compiler if your C++ code is not correctly written. Examples of syntax errors are a semicolon ; missing or if the code has a open curly brace { or open quote ", but no close curly brace } or close quote ".

Informally, a syntax error is called a bug, and the process of removing errors is called debugging. An early computer science pioneer Grace Hopper documented a real bug, a moth that flew into a computer in 1947!

firstbug Grace Hopper's log showing a real bug, 1947.

The compiler tries to run your code, but if your code has syntax errors, you will see error messages displayed below the code. Compiler error messages will tell the line number that the compiler found the error and the type of error. The error messages are not always easy to understand and sometimes the actual error is before the line that the compiler says is the problem.

Debugging is a normal part of coding. It can be frustrating at times, but you will get better at it with practice! Sometimes another pair of eyes really helps, so ask a friend if you get stuck or try explaining your code line by line to someone or even a rubber duck. Rubber duck debugging is a lot of fun!

Knowledge Check

Debug the following program

#include <iostream>

int main()
{
    Cout << "Hello, World! << endl
    return 0;
}