Comments in C

In this article, I will explain how to comment in the C program. By the end of this article, you will learn how to add a single-line comment and multi-line comments in your C program.

I also have a video version of this article. Scroll to the bottom of this article to view the video version which includes a detailed explanation along with the hands-on example.

Comments in C are used to explain or describe source code logic. There are 2 types of comments:

  • Single line comment
  • Multi-line comment

The program will ignore whatever text we specify using comments during execution. Mostly, comments are used to explain program logic. It is mainly used to add program logic explanation (in free text form) in between program code in order to make the code more readable.

If you want your program not to execute specific lines of code, you can use C comments in order to disable those lines of code.

How to add single line comment in C?

Single line comment is represented by //

C program ignores everything in the line that follows // during execution.

Here is an example of single line comment.

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

     //Below statement is printf statement
     //The below printf statement will display a text
     printf("This is an example of single line comment");

     return 0;

}

In the above program, I have added 2 different single-line comments starting //. These single-line comments are being used to explain the program code. During execution, the program will ignore the text specified after // single-line comment.

How to add Multi-line comment in C?

Now, let’s have a look at multi-line C comments. Similar to single-line comments, multi-line comments are also used to explain the program code. However, the only difference is, that multi-line comments are used to comment multiple lines all at once.

We can comment multiple lines of code by enclosing the text within /* and */ i.e., slash asterisk and asterisk slash. Here is an example:

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

     /*
     Below statement is printf statement
     The below printf statement will display a text
     */
     printf("This is an example of single line comment");

     return 0;

}

In the above example, I have enclosed a code description within /* and */. This type of comment is called multi-line commenting. During execution, the program will ignore the text specified between /* and */.

Here is the video tutorial on Comments in C with the hands-on session for your better understanding.

You may also like...

3 Responses

  1. vamshi myakala says:

    this is actually great is much better then the lectures we do ……

  2. Moses says:

    I have understood it now ..thanks

Leave a Reply

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

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet