×

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

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

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

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

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.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

Timsort Implementation Using C++

Timsort Implementation Using C++ The Timsort is a stable sorting algorithm that uses the idea of merge sort and insertion sort. It can also be called a hybrid algorithm of insertion...

3 minutes read.

SET Data Structure in C++

Generally, in our home, we store food items in a fridge or kitchen efficiently to find them easily and use them. Similarly, in programming, we need to store the data...

6 minutes read.