How to Add Single-Line Comment in C

What is comment in C?

In C, a comment is a piece of text in code that is not executed when the program is executed.

Comments are used to provide explanations, and keep documentation, or notes within the code for future reference and code understanding.

C supports two types of comments: single-line comments and multi-line comments.

Adding single-line comments in C

Just add two forward slashes (//) before a line and the line will be treated as comment.

In the following example, two forward slashes have been added before the line 'This is an example of a single-line comment'. C compiler will ignore the line as comment and it will not have any effect on the execution of the program or on output.

Again, two forward slashes have been added before the line 'This is another example of comment' which is at the end of the printf() function. This is another way of adding comment at the end of C code.

Please take a closer look at the following example:

Example of C Single Line Comment


Output of the above example

Add two forward slashes (//) to comment a single line

The output does not contain the commented lines.

Live Code Playground

In the following C code editor, you can practice the above code by adding more comments. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output