C Comments

In this lesson, you will learn about comments in C, their importance, and implementation, along with examples to better understand the topic.

What is a Comment?

A comment is simply a text ignored by the C compiler, and it is a programmer-readable description in the source code of the C program. The objective of inserting the comments in the code is to make debugging easy and code comprehending. Appropriate use of the comments can make code maintenance easier and find bugs faster. Further, writing comments helps so other people will understand the code easily.

Why Comments are Important?

For example, we write a code and try to access it after a long time. Maybe some other developers want to read it. Before starting to work, they will probably spend time just understanding it. With comments, it will be more effortless to understand the code.

Type of Comments in C

C supports two types of comments as given below:

  • Single Line Comment //
  • Multi-line Comment /*…*/

1. Single-line Comments

A single-line comment starts with a double slash //  in the same line. Mostly used for a short description in C.

Example of Single Line Comment:

#include <stdio.h>
int main() {
// create mathGrade character variable
char mathGrade = 'A';
// print the mathGrade
printf("I got %c in my Maths exams", mathGrade);
return 0;
}

2. Multi-line Comments in C

Multi-line comments let us comment many lines at a time. Multi-Line comments start with a slash asterisk \* and end with an asterisk slash *\. Any text that comes in between multi-line comments “\*. *\” ignores by the C compiler.

Example of Multi-line Comments:

/* This program creates the student1 and student2 math marks and math grades variables. Print the
math marks and math grades using printf */
#include <stdio.h>

int main() {
  char std1Grade = 'A';
  char std2Grade = 'F';
  int std1Marks = 95;
  int std2Marks = 40;
  printf("If you get %d, You get %c grade in Maths exams.\n", std1Marks, std1Grade);
  printf("If you get %d, You will get %c grade in Maths exams.", std2Marks, std2Grade);
  return 0;
}

In the next lesson, you will learn about C Variables and their usage!