TOP CATEGORIES

×

What are Comments in C Programming

Comments in C

Comments in C

In C programming comments are used to ignore lines of code. Commented code does not participate in the execution of the program, the compiler simply ignores commented lines from being compiled and executed.

Why Comments are Used

  • Comments are generally used to pass a message about code.
  • Comments are used to close a line/block of code from being compiled or execute.
  • Comments help to understand what the code is written for.
  • Comments are very useful in large programs for better readability.

Types of Comments in C

There are two types of comments in C programming.

  • Single Line Comment
  • Multiline Comment

1. Single Line Comment

In C programming single line comment is used to ignore only one line of code. Symbol // is used to define single-line comments. In a line, the content which is written after // symbol is get ignored by the compiler.

Single Line Comment Syntax

// single line commented statement..

Example:-

#include<stdio.h>

 int main()

  {

     int m=10, n=2, res;

     // get sum here

     res=m+n;

     printf("Sum = %d\n",res);

     // get subtraction here

     res=m-n;

     printf("Subtraction = %d\n",res);

     // get multiplication here

     res=m*n;

     printf("Multiplication = %d\n",res);

     //res=m/n;

     //printf("m/n=%d\n",res);

     res=m%n;

     printf("Remainder = %d\n",res);

     return 0;

  }

Output

Sum = 12

Subtraction = 8

Multiplication = 20

Remainder = 0

Note: In the above example, you can see, the code followed by // symbol are got ignored by the compiler

2. Multiline Comment

The multiline comment is also used to ignore the code, the only difference is that it can ignore more than one line of code at a time.

  • In this, the whole code is written between /* and */ symbols.
  • It can also be used as a single-line comment.
  • The comment scope starts with /* symbol and ends with */

MultiLine Comment Syntax

/* commented line 1

commented line 2

...........

commented line n */

Example:-

#include<stdio.h>

 int main()

  {

     int m=10, n=2, res;

     /* get sum here */

     res=m+n;

     printf("Sum = %d\n",res);

     // get subtraction here

     res=m-n;

     printf("Subtraction = %d\n",res);

     /* get multiplication here */

     res=m*n;

     printf("Multiplication = %d\n",res);

     /* res=m/n;

     printf("m/n=%d\n",res);

     res=m%n;

     printf("Remainder = %d\n",res); */

     return 0;

  }

Output

Sum = 12

Subtraction = 8

Multiplication = 20

Note: You can use many numbers of a single line / multiline or both types of comments in a C program.

Email: advertise@gdatamart.com

Donate: To Support US

© 2024 GDATAMART.COM (All Rights Reserved)