×

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

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

Vector Size in C++

What are Vectors?  In the C++ programming language, vectors are run-time sequence containers representing arrays with variable sizes, which are contained within STL (Standard Template Library). They utilize contiguous storage spaces...

6 minutes read.

How to create a stack in C++

A stack is a data structure, which is of linear type. A specific order has to be followed while inserting and deleting the elements from the stack. Generally, stack follows...

5 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

Palindrome Using While Loop in C++

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++ also allows...

6 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 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.

C++ Iterators

What are iterators ? Iterators are among the four foundations of the C++ Standard Template Library, also known as the STL. The memory address of the STL container classes is pointed...

15 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

C++ Functions

In other programming languages, a function is referred to as a process or a subroutine. We can design functions to execute any task. A function can be used repeatedly. It...

5 minutes read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.