CodeBlanks Logo Icon CodeBlanks

Comments

Learn to write single-line (//) and multi-line (/* */) comments in C.

Comments

Comments are text that explains the code and is ignored by the compiler. C has two kinds of comments:

  • Single-line: // this is a comment
  • Multi-line: /* this is also a comment */

Comments make code readable and convey your intent to other developers. Avoid adding misleading explanations.

What are comments for?

Comments serve three main purposes:

  • Explanation: describe complex logic to the reader
  • Documentation: say what functions do and what parameters they take
  • Disabling: temporarily turn off trial code

A good comment explains why, not what. Avoid restating the obvious; well-named code often speaks for itself.

Practice Write a program with a comment

Write a C program: #include <stdio.h>, int main(void), and print Hello with printf. Also add at least one single-line comment starting with //.

Sample solution

main.c
#include <stdio.h>

// Account
int main(void) {
    printf("Hello");
    return 0;
}

Expected output

Hello

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Single-line comment marker

Question
____ this is a comment
Answer
// this is a comment

Explanation: // starts a single-line comment.

2 Multi-line comment syntax?

  • /* Comment */ Correct answer
  • # Comment
  • <!-- Comment -->
  • // Comment

Explanation: /* ... */ is a multi-line comment.

3 Match comment types

Term Meaning
// Single-line
/* */ Multi-line
# Not a comment
Explanation Intent

All lessons in Beginner