Comments in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

A well-documented program or code is a good habit for a programmer or developer. It makes a program more readable, maintainable and error finding becomes easier. One important part of good documentation is to put comments in your code. So, In this article, We will see the comments in C Program.

Scope

  • This article defines comments in C Program and explains the intuitive logic of this algorithm. We also learn how to put comments in the C program.
  • The article shows the types of comments with the help of programming examples.
  • The article also explains why we need comments and their advantages.

Introduction to Comments in C

Comments are program text used to explain the program logic. They are ignored by the compiler. Comments help to make our code more readable and maintainable. The compiler and interpreter ignore comments, so they do not affect the program's behaviour or performance.

Syntax:

Types of C Comments

  • Single-Line Comments
  • Multi-Line comments or Paired Comments

Types of comments in c

Single Line Comment in C

A Single Line Comment starts with a double forward-slash (//) and ends with a new line. So, everything to the right side of these forward slashes on the current line is ignored by the compiler. A comment of this type can contain any text, it can even contain another double slash as well. They best suit when we have to give short detail about the code.

Example of Single Line Comment: This C program illustrates the Single Line Comment

Output:

In the above example, we can see that the commented line is ignored by the compiler in the program.

Multi-Line Comment or Paired Comment in C

Multi-Line comment uses two delimiters /* and */ which is a slash asterisk /* and asterisk slash */. Multi-Line comments start with a slash asterisk /* and end with the asterisk slash next */. These comments can include anything that is not a */ , including newlines. The compiler treats everything as comments, whichever comes between the /* and */. This type of comment is used when we have to give a detailed explanation of the code and require more than one line.

Example of Multi-Line or Paired Comment:

This C program illustrates the Multi-Line Comment

Output:

In the above example, we can see that everything between the delimiters is ignored by the compiler in the program.

Note: A comment that begins with /* ends with the */ means Comment Pairs do not nest. As a result, one comment pair can not appear inside another.

Comment at End of Code Line

We can add the comments at the end of the code line in a program. Comment at the end of the code line means we can comment text after the termination of a line.

Being a programmer or developer, develop a good habit to put your comment before the line of code.

Example:

Rules for writing comments in C

An incorrect comment is not a good thing because it may mislead the reader and could also give compilation errors. So, there are some rules for writing a comment.

  • When we change anything in the code, make sure to update the comments too.
  • Do not nest the comments that mean one comment pair can not appear inside another.
  • We can split the comments.
  • We can give any number of comments at any place in a single program.

Why do you need comments in C ?

  • When our program becomes too long than using comments, we can make our code or program more readable and maintainable.
  • Comments are generally used to summarize an algorithm, identify the purpose of a variable, or clarify an unclear segment of code.
  • When we know our program is going to be read by other programmers also, the use of comments to explain the working of the code is very helpful.
  • Comment helps one's own self also as whenever code or program is reused after a long time, it recaps all the information of the code quickly.

What does the compiler do with the C comments ?

Lexical Analyzer is an in-build program in the compiler that scans the characters and transforms them into tokens and these in-build programs do not pass the commented text to the parser. That means comments are simply omitted at the time of compilation as they are just there for reading purposes and do not contribute to the program's functionality.

Hence we can understand that Comments are program text that is ignored by the compiler.

Advantages of commenting on a program

  • Using comments in a program, the program seems easy to read and maintain.
  • By using comments to explain the program's working and logic made the program universally accepted as everyone finds it easy to use and understand.
  • Using comments not only others but you can also understand your code after ages.

Conclusion

  • Comments in C are used to explain the code we are writing in our program.
  • Using Comments we can make our code more readable, maintainable and error finding becomes easier.
  • C supports two types of commenting styles one is single-line comment and the other is a multi-line comment.
  • The compiler just ignores the commented text at the time of compilation.
  • A good programmer always uses comments in their code.