Open In App

C Comments

The comments in C are human-readable explanations or notes in the source code of a C program.  A comment makes the program easier to read and understand. These are the statements that are not executed by the compiler or an interpreter.

It is considered to be a good practice to document our code using comments.

When and Why to use Comments in C programming?

  1. A person reading a large code will be bemused if comments are not provided about details of the program.
  2. C Comments are a way to make a code more readable by providing more descriptions.
  3. C Comments can include a description of an algorithm to make code understandable.
  4. C Comments can be used to prevent the execution of some parts of the code.

Types of comments in C

In C there are two types of comments in C language:

Types of Comments in C

1. Single-line Comment in C

A single-line comment in C starts with ( // ) double forward slash. It extends till the end of the line and we don’t need to specify its end.

Syntax of Single Line C Comment

// This is a single line comment

Example 1: C Program to illustrate single-line comment




// C program to illustrate
// use of single-line comment
#include <stdio.h>
 
int main(void)
{
    // This is a single-line comment
    printf("Welcome to GeeksforGeeks");
    return 0;
}

Output: 
Welcome to GeeksforGeeks

 

Comment at End of Code Line

We can also create a comment that displays at the end of a line of code using a single-line comment. But generally, it’s better to practice putting the comment before the line of code.

Example: 




// C program to demonstrate commenting after line of code
#include <stdio.h>
 
int main() {
    // single line comment here
   
      printf("Welcome to GeeksforGeeks"); // comment here
    return 0;
}

Output
Welcome to GeeksforGeeks

2. Multi-line Comment in C

The Multi-line comment in C starts with a forward slash and asterisk ( /* ) and ends with an asterisk and forward slash ( */ ). Any text between /* and */ is treated as a comment and is ignored by the compiler.

It can apply comments to multiple lines in the program.

Syntax of Multi-Line C Comment

/*Comment starts
    continues
    continues
    .
    .
    .
Comment ends*/

Example 2:  C Program to illustrate the multi-line comment




/* C program to illustrate
use of
multi-line comment */
#include <stdio.h>
int main(void)
{
    /*
    This is a
    multi-line comment
    */
   
      /*
    This comment contains some code which
    will not be executed.
    printf("Code enclosed in Comment");
    */
    printf("Welcome to GeeksforGeeks");
    return 0;
}

Output: 
Welcome to GeeksforGeeks

 


Article Tags :