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
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:
1 |
//This is single line comment |
Example:-
Simple example to demonstrate the use of single line comments is as follows:
1 2 3 4 5 6 7 8 |
#include <stdio.h> int main() { // It prints Hello World printf("Hello world"); return 0; } |
Output:-
Now when we run the above program we see the following output, here the comment is completely ignored by compiler.
1 |
Hello World |
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:
1 2 3 4 5 6 |
/* This is multi line comment */ |
Example:-
Simple example to demonstrate the use of multi-line comments is as follows:
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> int main() { /* This is a multiline comment * It prints Hello World */ printf("Hello world"); return 0; } |
Output:-
Now when we run the above program we see the following output, here the comment is completely ignored by compiler.
1 |
Hello World |
Uses of multi-line comments are as follows:
- Useful for commenting out a section of code
- Cannot be nested within other multi-line comments