In C++ development, comments play a crucial role in enhancing code readability and comprehension. Developers annotate their code by explaining variables, functions, classes, or specific code sections. These comments are disregarded by the compiler, meaning they do not affect the program's functionality. Comments are typically denoted using double slashes (//).
Simple Example
Let's consider a basic example to demonstrate the use of comments in C++ programming.
Example
#include <iostream> //header file
using namespace std; //allows standard library
int main() //main body of the program
{
// A comment to provide information about below line
// printing a message to the console
cout << "Hello! Welcome to the Cpp Tutorial World"; //output statement
return 0; //return statement
}
Output:
Hello! Welcome to the Cpp Tutorial World
Explanation:
In this instance, we have included multiple explanations to simplify the program. A line beginning with (//) is considered a comment line, which is disregarded during compilation by the compiler.
Types of C++ Comments
There are two classifications of comments in C++:
- Single Line notation
- Multi-Line commentary
Single Line Comment:
C++ utilizes double forward slashes (//) for comments, which are intended for single-line annotations. Comments do not impact program execution as the compiler disregards content following //. These types of comments are frequently employed for testing, debugging, annotating code segments, or temporarily deactivating specific lines of code.
Example:
Let's explore an illustration in C++ showcasing a Single Line comment.
Example
#include <iostream>
using namespace std;
int main()
{
// Declaring two variables to store user input.
int j, q;
// Requesting the user to enter the first number.
cout << "Enter the first number: ";
cin >> j;
// Taking input from the user.
// Requesting the user to enter the second number.
cout << "Enter the second number: ";
cin >> q;
// Taking input from the user.
// Adding the two numbers.
int add = j + q;
// Displaying the result.
cout << "The addition of two numbers is: " << add << endl;
return 0;
// Program execution ends successfully
}
Output:
Enter the first number: 20
Enter the second number: 30
The addition of two numbers is: 50
Explanation:
This C++ software computes the total of a pair of numbers provided by the user. To store the user's input, two integer variables, j and q, are initially defined. Subsequently, the software prompts the user to enter the initial and subsequent values, which are then captured using the cin function.
It calculates the total after saving the input values, then saves the outcome in the variable add. Subsequently, it presents the sum using cout to ensure a clear output for the user. Each part of the code is explained with a brief comment using // for improved readability and maintenance.
Multiple Line Comment
Multiple lines of code can be elucidated using block comments in C++. These comments begin with / and end with /. The compiler ignores any text enclosed within these delimiters, so it does not affect the program's functionality. Block comments are beneficial for providing documentation, clarifying intricate logic, or deactivating multiple lines of code temporarily for debugging purposes.
Example:
Let's explore a C++ illustration of a Multi Line comment.
Example
#include <iostream>
using namespace std;
int main()
{
/*
The area of a rectangle is computed using this program.
It requires the user to enter the width and length.
Area = Length × Width was the formula used.
*/
// Declaring two variables to store the length and width.
double l, w;
// Requesting the user to enter the length.
cout << "Enter the length of the rectangle: ";
cin >> l;
// Requesting the user to enter the width.
cout << "Enter the width of the rectangle: ";
cin >> w;
/*
Calculating the area of the rectangle using the formula:
Area = length * width
The result is stored in the variable 'area'.
*/
double a = l * w;
// Displaying the calculated area of rectangle.
cout << "Area of the rectangle is: " << a << endl;
return 0;
}
Output:
Enter the length of the rectangle: 5
Enter the width of the rectangle: 4
Area of the rectangle is: 20
Explanation:
In this instance, the program computes the area of a rectangle based on the length and width provided by the user. It starts with a detailed explanation displayed over multiple lines stating the formula, Area = Length × Width, and outlining the program's objective. The cout function is utilized to show prompts, followed by the declaration of two double variables, 'l' and 'w', to contain user inputs, which are then obtained through cin. Subsequently, the area is determined using the multiplication formula and saved in the variable 'a'. Lastly, the program showcases the calculated area using cout, while employing single-line comments to enhance the readability of each step.
Why are Comments used in C++?
- In C++, comments improve code readability by providing explanations on variables, algorithms, and complex code segments.
- They facilitate debugging by enabling developers to disable specific parts of code without deleting them.
- Code maintenance is facilitated by well-placed comments, which guarantee that a program will stay understandable even after extended use.
- Additionally, they facilitate easier developer collaboration by describing the objectives of various sections.
- Writing clear and organized code requires the use of comments, whether for documentation or debugging.
C++ Comments MCQS
- In C++, what symbol is used for single-line comments?
- / /
- Which C++ syntax is suitable for multi-line comments?
- // This is a multi-line comment //
- / This is a multi-line comment /
- # This is a multi-line comment
- This is a multi-line comment --
- What happens when comments are included in C++ code?
- They increase execution time
- They are ignored by the compiler
- They cause syntax errors
- They slow down compilation
- Can multi-line comments be nested in C++?
- Yes, multiple / / comments can be nested
- No, nesting / / comments causes compilation errors
- Nesting is allowed only inside functions
- // comments must be used to nest / / comments
- Which of the following describes the correct C++ comment syntax?
- / Comment starts // Nested /
- // This is a comment
- # This is a comment
- / Comment starts / Nested / End /