×

C++ Comments

Comment/Remark

A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated as white space by the compiler. In testing, you may use comments to make particular lines of code inactive; however, the #if / #endif preprocessor directives are superior for this since you can surround code with comments but not nest them. Comments are a crucial aspect of excellent documentation.

  • A remark in computer programming is a programmer-readable explanation or annotation in a computer program's source code.
  • Comments are statements that the compiler and interpreter do not execute.

There are two types of Comments in C++:

1. Single Line Comment

Any sequence of characters after the / (two slashes) characters. This type of comment is terminated by a new line that is not immediately beyond the backslash. As a result, it is known as "single-line comment / remark".

Example of a program containing single line comment:

#include <iostream>
#include <bits/stdc++.h>  
using namespace std;  
int main()  
{  
 int a = 5; // a is a variable      
 cout<<”Value of a is: “<<a<<endl;
 return 0;         
} 

OUTPUT:

Value of a is : 5

Explanation:

In the above example of a code printing value of a variable having a single line comment “// a is a variable” in the sixth line of code.

2. Multiple Line Comment

The */ characters, followed by any sequence of characters (including new lines), then the /* (slash, asterisk) characters. This syntax is identical to that of ANSI C.

Example of a program containing multiple line comment:

#include <iostream>
#include <bits/stdc++.h>  
using namespace std;  
int main()  
{  
 /* declaring and   
 print the vale of the variable a. */   
 int a = 14;     
 cout<<"Value of a is: <<a<<endl;
 return 0;         
} 

OUTPUT:

Value of a is: 14

Explanation:

In the above example of a code printing value of a variable having multiple line comment ”/* declaring and print the value of the variable a. */” in  6th and 7th line of code.

NOTE: Comments can also be used to deactivate code, making it unavailable for execution. As an example:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
   cout << "JAVATPOINT ";
   // cout << "Hello world !";
   cout << "for C++"<<endl;
   return 0;
}

OUTPUT:

JAVATPOINT for C++

Explanation:

In the above example of code printing “JAVATPOINT for C++” in which 6th line containing single line comment which is deactivated, making it unavailable for execution.

Tip: Remember the comment shortcut; it can be really useful. It's Ctrl + / for Windows and Cmd + / for Mac in most coding editors.

What are the benefits of using comments?

  • If there are no comments on the program's intricacies, a person reading a huge code will be perplexed.
  • Comments are a means to provide extra description to a code to make it more understandable.
  • To make code more intelligible, comments might provide a description of an algorithm.
  • If code is to be reused over a lengthy period of time, comments might be useful for one-self as well.

NOTE: Comments should not be used to clarify badly written code written in English. Always write code that is well-structured and self-explanatory. Then there's the utilization of comments.


Related Topics

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

How to implement map in C++

Part of the C++ STL is maps (Standard Template Library). Maps are associative containers that hold sorted key-value pairs, where each key is distinct and may only be added or...

4 minutes read.

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.

Single level Inheritance

Inheritance is a fundamental element of C++’s Object-Oriented Programming (OOP). It allows a class (called the derived class) to inherit characteristics and attributes from another class (called the base class)....

5 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute read.

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions. What are Keywords in C++? In C++, a keyword is a reserved word that has a predefined...

4 minutes read.

Reserved Keywords in C++

What are reserved keywords in C++? There are a few keywords that cannot be used as identifiers as those words are reserved for some other purposes, such keywords are called reserved...

15 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

C++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.