C Comments

In this tutorial you will learn about the C Comments and its application with practical example.

C Comments

In c programming, comments are a set of statements that are not executed by the compiler. The use of comments makes it easy for humans to understand the source code. Usually, comments give you an inside or explanation about the variable, method, class, or any statement that exists in the source code. The comment statements are ignored during the execution of the program. In this tutorial I’ll explain you how to use comments properly with syntax and examples.

Types of Comments In C

In C programming, there are 2 types of comments.

  • Single Line Comment
  • Multi-Line Comment

types-of-comments-in-c

It is recommended to choose a commenting style and use it consistently throughout your source code. Doing this makes your code more readable.

Single line Comment

A ‘//’ (double forward slash) is used to specify a single line comment, which extends up to the newline character. This can be used to comment out everything until a line break. Let’s see an example of a single line comment in C.

Syntax:-

General syntax of single line comments is as follows:

Example:-

Simple example to demonstrate the use of single line comments is as follows:

Output:-

Now when we run the above program we see the following output, here the comment is completely ignored by compiler.

Multi-line Comment

You can also create a comment that spans multiple lines. If you want to comment multiple lines then you can do it using /* and */. The compiler ignores everything from /* to */. Let’s see an example of a multi-line comment in C.

Syntax:-

General syntax of multi-line comments is as follows:

Example:-

Simple example to demonstrate the use of multi-line comments is as follows:

Output:-

Now when we run the above program we see the following output, here the comment is completely ignored by compiler.

Uses of multi-line comments are as follows:

  • Useful for commenting out a section of code
  • Cannot be nested within other multi-line comments

In this tutorial we have learn about the C Comments and its application with practical example. I hope you will like this tutorial.