Comments in C Programming

In this article, I will explain how to add comments in C program. By the end of this article, you will learn how to add single line 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 video version which includes detailed explanation along with 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

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.

Single line comment

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 comment starting //. These single line comments are being used to explain the program code. During execution, program will ignore the text specified after // single line comment.

Multi-line comment

Now, lets 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, 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 code description within /* and */. This type of commenting is called multi-line commenting. During execution, program will ignore the text specified between /* and */.

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

You may also like...

Leave a Reply

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