Comments in C/C++

Last Updated :

A comment is text that the compiler ignores but that is useful for programmers. Comments are normally used to annotate code for future reference. Comments are statements that are not executed by the compiler and interpreter. The compiler treats them as white space.

  • It makes a program more readable and error finding become easier.
  • One important part of good documentation is Comments.
  • All programming languages allow for some form of comments.
  • Comments are used to give insight into the design or method of a program. These explain the purpose and functionality of code.
  • Using these comments inside the code, we can place of copyright/licenses, project notes, special thanks, contributor credits, etc. directly in the source code.
  • We can disable the code (or any line of code) using these Comment marker. This is according to property of comment and is called “comment out” of the code.

There are two types of comments in C++ : Single line comment, and Multi-line comment.

1. Single line Comment

These denote single line comment using by // double forward slash

// single line comment 

2. Multi line Comment

These denote multi line comment using by /* … */ (slash, asterisk)

/* This is multi line comment 
in a CPP program to describe some comment by developer  
/* This is 
mult line comment */
#include <iostream>
using namespace std;

// this is single line comment
main() {
   cout << "Hello World"; // this is also a single line comment
   
   /* this is another 
    multi line comment */
   return 0;
}

Note –

  1. Normally, we use // for short comments, and /* */ for longer. But, it is up to you which you want to use.
  2. Comments using the first syntax, therefore, cannot be nested.
  3. They must be maintained to reflect any changes in the code
  4. Excessive comments tend to make the code less readable



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next Article
CPP_Programmer Published 15 Aug, 2020 · 2 min read
Article Tags :

Similar Reads

  • Four Pillars of Object-Oriented Programming (OOPS)

    Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage🛠️. Tools are scattered everywhere,…

    4 min read
  • What is Overriding in C++

    Write a program in C++ to depict the concept of overriding or Dynamic polymorphism. Overriding is a type of polymorphism. Polymorphism in OOP languages means to take more…

    2 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read