Skip to main content

Section 2.9 Debugging Syntax Errors

One type of error (or bug) a program can have is a syntax error. This is when the code violates the rules (syntax) of the programming language. When there is a syntax error, the compiler will not be able to understand the code. (It could perhaps be programmed to guess at what you meant, but incorrect guesses would likely cause all kinds of harm.) So when a compiler encounters a syntax error, it prints out one or more error messages and does not build the program.
The messages that the compiler gives for syntax errors can be cryptic take some practice to learn how to read. However, they can be essential for figuring out exactly what is wrong in a program. In this section, we will look at some common syntax errors and the error messages that they produce.
Here is a sample error message:
test.cpp: In function β€˜int main()’:
test.cpp:7:28: error: expected β€˜;’ before β€˜return’
    7 |     cout << "Hello, World!"
      |                            ^
      |                            ;
    8 |     return 0;
      |     ~~~~~~
This error message is pretty helpful. It is pointing right at the issue - there is a missing ; at the end of the statement.
Each error message starts with a location that looks like test.cpp:7:28 that tells you where the issue was identified. That location consists of a file name, then a colon (:), then the line number, then another colon followed by the column. Here, the error is at line 7, character number 28 in the file β€œtest.cpp”.
Unfortunately, the error message you get from a syntax error may not be so easy to read. It may use terms you do not understand, or point to a location other than where your real issue is. The compiler doesn’t know what you meant to type, it just knows that at some point, it hit something that did not make sense. At that point it has to make its best guess about what you did wrong. Here is an example of a less helpful message:
test.cpp:7:26: error: missing terminating " character [-Werror]
    7 |     cout << Hello, World!"
      |                          ^
test.cpp:7:26: error: missing terminating " character
test.cpp: In function β€˜int main()’:
test.cpp:7:13: error: β€˜Hello’ was not declared in this scope; did you mean β€˜ftello’?
    7 |     cout << Hello, World!"
      |             ^~~~~
      |             ftello
test.cpp:7:20: error: β€˜World’ was not declared in this scope
    7 |     cout << Hello, World!"
      |                    ^~~~~
At least the line number is correct. There is an issue on line 7. But the issue is that there is no " to start the string (before Hello). The compiler thinks you started a string where the first " is after World! and that then you failed to terminate (end) the string. And because Hello and World were not inside a string, it thought that those were supposed to be code and got confused by them, producing more errors.
Try replicating that issue by deleting the first " in this program:
Listing 2.9.1.
Then try fixing the issue by putting it back. Notice that fixing the one real syntax error fixes all three reported errors - they were all symptoms of the one real error.

Insight 2.9.1.

When fixing errors, focus on fixing the first one identified by the compiler. That early error often causes a series of problems that result in multiple error messages. If you skip ahead, you likely will try to β€œfix” something that is not really an error!

Checkpoint 2.9.1.

Remove the { that is at the end of line 5 in the Hello World program above. What is the first error reported?
  • On line 5 - error: expected initializer before β€˜cout’
  • That is not the line number reported.
  • On line 7 - error: expected initializer before β€˜cout’
  • On line 9 - error: expected declaration before β€˜}’ token
  • That is not the first error reported.
You may have noticed that the error report for the missing { was particular unhelpful. The best tip in the errors is that there was a problem detected right at the start of line 7. That probably means there was an issue at the end of the previous line of code. Line 6 was a comment. So line 5, the last line with actually code, is the likely suspect.

Insight 2.9.2.

If an error message does not make sense, look at the line above it and see if something is missing there.
Despite the difficulties sometimes involved in understanding them, error messages contain useful information. During the first few weeks of your programming career, you will probably spend a lot of time learning how to interpret the messages that will help you track down syntax and other compile-time errors. As you gain experience, you will make fewer mistakes and find them more quickly. Becoming a proficient programmer is as much about learning how to debug code as it is about writing code.
You have attempted of activities on this page.