Introduction to Programming

Comments


Comments help other people understand your code.
This is useful if you want someone's help fixing your program, or if more than one person is going to work on your program.

It also helps you in case you forget what some of your code does.

// Put anything you want here!

This kind of comment is very cool and easy to use. Just put two slashes, and everything else on that line will be ignored by the compiler. So after the two slashes, you can say anything you want!
Generally you want to say something helpful about the nearby code. Like:

// The following code compares two numbers together:

Note that the comment ends at the end of the line. The next line must be valid code again, unless that line also has two slashes.

There's another kind of comment if you want to have comments on more than one line:

/* Put anything you want here.
   You can also put whatever here.
   And here.
   Finally, close the comment with: */

Start it with a slash and a star, end it with a star and a slash. Anything can go between, and any number of lines. The only thing you can't have in between is star-slash (because that would end the comment earlier).

Use the multi-line comments if you have to do a lot of explaining, or maybe at the very beginning of your code to explain what the program does. Like:

/* happy.c

   This program is supposed to make people happy.
   The code is very complicated, and took me three days to write.
   I hope you like it!
*/


Next: Makefiles

Previous: Functions


Introduction to Programming

Home