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
main.cis the file,4is the line and25the column where the compiler noticed the problem.error:means it can't continue. Awarning:means the code compiles but is suspicious; take warnings seriously too.- The message describes what it expected.
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 turn: 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.