Different Types of Comments in C

In C programming, comments are the portion of the program intended for you and your fellow programmers to understand the code. In simple words, a description of a basic programming element is called comments.

Comments are totally ignored by compilers. The Compiler does not generate executable code for these comments. C supports three types of comments:-

  • Single line comment (//)
  • Multiline comment (/* */)
  • Document Comment (/** */)
Comments in C Quotes

Single line Comment (//)

Most of the time single-line comments are used at the end of the line or before the line. The Compiler ignores everything from // to the end of the line. For example:-

// “Hello World!” program in C
#include<stdio.h>
int main()
{
   printf(“Hello World!\n”);
   return 0;
}

Multiline Comment ( /* _ _ _ _ _ _ _ */ )

This is a traditional comment. The compiler ignores everything from /* to */. For example:-

#include<stdio.h>
int main()
{
     printf(“Hello World!\n”);
     /* This is a multiline comment.
        These line will be ignored by compilers.*/

     return 0;
}

Document Comments in C

These C comments are used for Documentation purposes. The statements placed inside these special characters are ignored by the compiler while compiling. Example:-

//Addition of two number
/**
  * This Program Add two numbers.
  * author: Vaibhav
  * company: Know Program
  */
#include<stdio.h>
int main()
{
  /*
   * This program add two integer numbers.
   */
  int a = 5, b = 10, c;
  c = a+b;
  printf(“Sum = %d\n”, c);
  return 0;
}

Use Comments in the right way

Comments should not be used as a substitute for a way to explain poorly written code in English. Write well structured and readable code, and then use comments.

Comments are used for a better understanding of the code. A person reading a large code will be bemused if comments are not provided about the details of the program. Comments are a way to make code more readable by providing more descriptions. Comments can include a description of an algorithm to make code understandable. After a long time gap if you see your own code without comments, then it will take time to understand the code. So, Comments can be helpful for one’s own self too if code is to be reused after a long gap.

Q) Find out the valid comments in the below list. If the C compiler throws a compilation time error then those comments are invalid.

  1. //
  2. /////////////////////
  3. // /*
  4. /*
  5. /* */
  6. /* // */
  7. // /* // */
  8. /* /**/
  9. /* */ */
  10. // /* */ */
  11. /*/ */ */
View Answer Valid Comments are :- 1,2,3,5,6,7,8,10

Invalid Comments are:- 4, 9 and 11

Also See:- Identifiers and Data Types Quiz in C

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

2 thoughts on “Different Types of Comments in C”

  1. Your Way Of Explanation was good. You add some valuable points. Here I am sharing some information about java. C Programing – If you are new to programming then it is better to start with ‘C’. Most of the programmers take it as a fundamental for programming. It covers all the basics of programming. You can utilize this knowledge in almost every language. After you learn how to program in C, you can easily switch to any other language. You need to understand how it works. For learning this you can prefer online tutorials or video tutorials or you can register in any good c language training institutes in Hyderabad. C Language Training Institutes in Hyderabad

Leave a Comment

Your email address will not be published. Required fields are marked *