C Comments With Examples

This guide will teach you how to use comments in the C programming language. This tutorial will go over different types of comments with examples.

What exactly are Comments in C?

A comment is a piece of text that has no effect on the program’s output and is only present for documentation purposes.
The C compilers completely ignore comments.
When working in an organization, you should include comments in your code so that the other programmer understands what the code is doing.
This also aids in the maintenance of an old project, where comments can be quite useful in understanding the operation of code written a long time ago.

Example of C Comments

In this example, we’re adding a comment before the printf() statement to explain why we’re doing it. This has no effect on the program’s outcome.

#include <stdio.h>
int main() {

  // Welcome message to BeginnersBook readers
  printf("Hi, Welcome to BeginnersBook.com!");
  return 0;


Hi, Welcome to BeginnersBook.com!

C Commentary Types

1. Single-line comment – begins with / and continues until the end of the line
2. Multi-line comment – This type of comment begins with /* and ends with */ and can have any number of lines.

1. C comments on single lines

We have three single-line comments in this example. These comments explain the purpose of the C statements that follow.

#include <stdio.h>
int main() {

  // declaring and initializing two integer variables
  int num1 = 100, num2 = 50;

  // declaring another variable to store the sum
  int sum = num1+num2;

  // displaying the sum of two numbers



The single line comment can also be written as follows: The compiler ignores the comment after / and executes the rest of the statement before /.

int sum = num1+num2;// declaring another variable to store the sum

2. C comments on multiple lines

If you want to write a long comment that spans multiple lines, begin the comment with /* and end it with */. The compiler ignores the entire block.

#include <stdio.h>
int main() {

  int num=3;

  /* Here we are finding out the square
   * of the given number "num" by multiplying
   * the number by itself and storing the result in
   * another int variable numSquare
  int numSquare = num*num;



In C, comments are used.

1. Increases readability: It makes the code more understandable. By reading the comments, any programmer other than the one who wrote the code can understand it.
2. Look up a piece of code: Use the comment as a search term to find a piece of code. For example, if you have code for some task “xyz” somewhere in a large C file, you can search the file using “xyz” to find that code.
3. Documentation: Comments assist in code documentation, which is a good programming practice. All organizations always encourage code documentation.
4. Debugging: When debugging a code, rather than removing a piece of code to check for errors, simply comment out the code that you suspect is the source of the error and check again. If that doesn’t work, uncomment the code and comment the other block of code before trying again. This will allow you to debug the code more quickly without having to make changes to the actual code.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button