C/C++ Arena

Step 5 of 5

Comments and reading errors

Comments

Comments are notes for people reading the code. The compiler throws them away, so they never change what the program does.

// A line comment runs from the two slashes to the end of the line.
/* A block comment can
   span several lines. */
printf("hi\n"); // comments can also follow code

Good comments explain why the code does something ("prices are in cents so we never round"), not what it obviously does.

Reading compiler errors

When your code isn't valid C, the compiler stops and reports an error. Learning to read these messages is one of the most useful skills in programming. A typical error looks like this:

main.c:4:25: error: expected ';' after expression

One important detail: the compiler reports where it noticed the problem, which isn't always where the mistake is. For a missing semicolon, modern compilers point right at the spot where the ; belongs. But for other mistakes, such as a missing closing quote or }, the error can appear a line or more later, because that's where the compiler realized something was wrong. If the line it names looks fine, look at the lines just above it.

When there are several errors, fix the first one first. One mistake can confuse the compiler into reporting extra errors that disappear once the first is fixed.

On this site, errors also appear in the editor next to the line, with a plain-English explanation below the code.

Your task

This program has two mistakes. Press Check first to see what the compiler says, then fix both so it prints Fixed it on one line.

Challenge 2

This program has two different mistakes: something missing at the top and a misspelled name. Press Check to read the errors, then fix both so it prints Bug squashed.

Challenge 3

Two harder mistakes this time: a string that never ends, and a block that never closes. The errors may point to a later line than the real mistake. Fix both so the program prints:

Line one
Line two

Previous: Escape sequences