Comments

Programs are read not only by computers, but also by people: by your classmates, your colleagues, and by you yourself a few weeks later, when you no longer remember why you wrote something the way you did. That's why programming languages have comments: notes in the code that the compiler ignores.

In C, a comment can start with two slashes //. Everything after them, up to the end of the line, is ignored. A comment can be on a line of its own, or at the end of a line with code:

// Prints the price of two cinema tickets
printf("%d\n", 12 + 12); // one ticket costs 12 dollars

This prints only:

24

You can write a longer note between /* and */. Such a comment can even span several lines:

/*
Author: Anna Smith
The program prints today's to-do list.
*/

A good comment says what you can't see in the code at first sight: what a part of the program is for, or why it is written the way it is. A comment such as // prints Hello! above printf("Hello!\n"); doesn't help anyone.

Comments are also useful for switching code off for a while without deleting it. Programmers call this commenting out: put // at the start of a line, and the line no longer runs. When you want it back, just delete the //.

printf("This line runs.\n");
// printf("This one doesn't.\n");

This prints:

This line runs.

Watch out for a /* without its */: everything after it becomes a comment, up to the end of the program, including the } of main. The compiler then reports unterminated /* comment.

Instructions

At the start of the program, above the #include line, add a comment that says what the program does, for example // Packing list for the seaside.

You're going to the seaside in summer, so you don't need a winter jacket. Comment out the line that prints winter jacket, so that the program prints only:

Packing for the seaside:
swimsuit
sunscreen

Start programming for free

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

Or sign up with:

5/7