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. For example, this program is missing a semicolon on line 4:
#include <stdio.h>
int main(void) {
printf("Hello!\n")
printf("How are you?\n");
return 0;
}Clang, the compiler on this site, prints:
main.c:4:23: error: expected ';' after expression
4 | printf("Hello!\n")
| ^
| ;
1 error generated.The most important part is the first line of the message. Read it from the left:
main.cis the name of the file with the program, the one in the editor.4is the number of the line with the mistake. You can see the line numbers on the left of the editor.23is the number of the column: which character on the line it is. The four spaces at the start of the line count too.error:says that this is an error. After it comes a description in English:expected ';' after expressionmeans that the compiler expected a;after the expression, here afterprintf(...).
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., says how many errors Clang found.
The message looks a little different when the closing quote of a text is missing:
#include <stdio.h>
int main(void) {
printf("Hello!\n);
return 0;
}main.c:4:12: warning: missing terminating '"' character [-Winvalid-pp-token]
4 | printf("Hello!\n);
| ^
main.c:4:12: error: expected expression
1 warning and 1 error generated.A line with 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 it finds nothing that makes sense after printf(: expected expression means that it expected an expression, something to print.
A program that has only warnings and no errors runs, and this site doesn't show its warnings. That's why the site turns the most important warnings into errors, such as the %d without a value from the exercise Text and Numbers. You'll recognize them by -Werror at the end of the line: [-Werror,-Wformat-insufficient-args].
One mistake sometimes causes several messages, and sometimes it hides another mistake, which the compiler finds only after you fix the first one. 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.
Other common mistakes and their messages:
printfis misspelled, for exampleprinftorPrintf:call to undeclared function 'prinft'; ISO C99 and later do not support implicit function declarations. The compiler doesn't know a function of that name. In a note below, it even suggests what you probably meant:did you mean 'printf'?- The line
#include <stdio.h>is missing: the message starts withcall to undeclared library function 'printf', and the note below it says what to do:include the header <stdio.h>. - The
}at the end ofmainis missing:expected '}', with the noteto match this '{', which points to the brace that has no partner.
Lines with note: are just extra notes. Some point into files of the standard library, such as stdio.h, and you don't need to read those.
Instructions
The program in the editor has two mistakes. Run it and read the first error message: on which line and in which column is the mistake, and what's wrong? Fix it and run the program again. A new message appears: the first mistake was hiding the second one. Fix that one too, so that the program prints:
Tic-tac-toe tournament
When: Saturday at 10:00
Where: classroom 2BStart programming for free
6/7