C Comments

In this tutorial, you will learn different commenting systems in the C programming language with the help of syntax and examples.


Comments are lines of sentences placed inside the program codes for a better understanding. Comments are not executed as part of the program and generally ignored by the C compiler.

In the C programming language, comments are used for various reasons.

  • Documenting code – In large C projects there may be thousands of lines of code and many programmers are working on that same project. It will be really helpful if they use proper comments to specify the purpose of the code developed by them. This is helpful among the developers as well as for future reference.
  • Debugging purpose – Sometimes C comments are used to debug the code. This is really helpful to identify the correctness of part of the code.

There are mainly two types of comments are used in the C programming language.

  1. Single-Line Comments
  2. Multi-Line Comments

Single-Line Comments

There are two ways to represent single-line comments. Below are the examples:

#include<stdio.h> 
int main(){ 
/* printing information */
printf("Welcome to C Tutorial"); 
return 0; 
}

Also, C++ has introduced a new style of doing single line comment by using double slash \\.

#include<stdio.h> 
int main(){ 
// printing information
printf("Welcome to C Tutorial"); 
return 0; 
}

The new style can be also be used in the C programming language for single-line comments as above.

Output

Welcome to C Tutorial

You can also place the single-line comment after the end of the code line.

printf("Welcome to C Tutorial"); /* printing information */

OR

printf("Welcome to C Tutorial"); // printing information

Multi-Line Comments

Multi-Line comments are created using slash asterisk /* .... */. The C compiler assumes that everything after the /* are the comments until it finds the */ even if it spans in multiple lines. */ denotes the end of the comments.

 Syntax

/*
Lines to be 
Commented
*/

Let’s see an example of a Multi-Line comment in the C programming language.

#include<stdio.h>
int main(){
/* This is a 
Multi-Line
Comment
*/
printf("Welcome to C Tutorial");
return 0;
}

Output

Welcome to C Tutorial

Please get connected & share!

Advertisement