Reading Errors

Mistakes are a normal part of programming. Even experienced programmers make them every day. What matters is finding and fixing them quickly, and error messages help you with that.

Before a program runs, the compiler goes through all of it. When it finds a mistake, it doesn't run the program at all, not even the lines before the mistake, and it shows a red error message in the output. For example, this program is missing a semicolon on line 4:

#include <iostream>

int main() {
    std::cout << "Hello!" << std::endl
    std::cout << "How are you?" << std::endl;
}

Clang, the compiler that compiles programs on this site, prints:

main.cpp:4:39: error: expected ';' after expression
    4 |     std::cout << "Hello!" << std::endl
      |                                       ^
      |                                       ;
1 error generated.

The most important part is the first line of the message. Read it from the left:

  • main.cpp is the name of the file with the program, the one in the editor.
  • 4 is the number of the line with the mistake. You can see the line numbers on the left of the editor.
  • 39 is the number of the column: which character on the line it is.
  • error: says that this is an error. After it comes a description: expected ';' after expression means that the compiler expected a ; after the expression.

Below that, Clang shows the line with the mistake and marks the place with ^. Often it even writes what is missing there, here ;. The last line, 1 error generated., just says how many errors Clang found.

The message looks a little different when the quote at the end of a text is missing:

#include <iostream>

int main() {
    std::cout << "Hello! << std::endl;
}
main.cpp:4:18: warning: missing terminating '"' character [-Winvalid-pp-token]
    4 |     std::cout << "Hello! << std::endl;
      |                  ^
main.cpp:4:18: error: expected expression
1 warning and 1 error generated.

A line marked warning: is a warning: the compiler points out something that looks suspicious. Here, it's even more useful than the error itself. missing terminating '"' character means that the closing quote is missing, and ^ points to the quote that has no partner. Without it, the compiler can't tell where the text ends, so after << it finds nothing that makes sense: expected expression means that it expected an expression, something to print.

Sometimes one mistake causes several messages. For example, when the line #include <iostream> is missing, you get the error use of undeclared identifier 'std' (a name the compiler doesn't know) for every std:: in the program. That's why you should always fix the first error first and run the program again. The other messages often disappear, because they were only caused by the first one. And the other way round: after a fix, a new error can appear that the compiler didn't find before.

Other common mistakes and their messages:

  • std:: is missing before cout: use of undeclared identifier 'cout'; did you mean 'std::cout'? Clang even suggests what you probably meant.
  • An uppercase letter instead of a lowercase one: no member named 'Cout' in namespace 'std'; did you mean 'cout'?
  • The } at the end of the program is missing: expected '}'

Lines with note: are just extra notes. They often point into files of the standard library, so you don't need to pay attention to them.

Instructions

The program in the editor has two mistakes: a semicolon and a quote are missing. Run it and read the first error message: on which line and in which column is the mistake, and what is wrong? Fix the mistake and run the program again. Repeat this until the program prints:

Tic-tac-toe tournament
When: Saturday at 10:00
Where: classroom 2B

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/6