Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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

  1. 1

    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.

  2. 2

    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
Back

6/9

  1. 1. Your First Program
  2. 2. More Lines
  3. 3. Quiz: New Lines
  4. 4. Quotes in Text
  5. 5. Text and Numbers
  6. 6. Comments
  7. 7. Reading Errors
  8. 8. Quiz: Spot the Mistake
  9. 9. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.