Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Comments

In this tutorial, we will learn about C++ comments, types of Comments, why we use them, and how to use them with the help of examples.


Comments

  • A well-documented program is a good practice as a programmer. It makes a program more readable and error finding became easier.
  • C++ comments are hints that a programmer can add to make their code easier to read and understand.
  • Comments are statements that are not executed by the compiler and interpreter.
  • Comments are One of the important part of good documentation.

Types of Comments

There are two ways to add comments to code:

1. Single Line Comments – //
2. Multi-line Comments – /* */


1. Single Line Comments

  • It is used to denote a single line comment.
  • In C++, any line that starts with // is a Single-line comment.
  • It is referred to as C++ style comments as it is originally part of C++ Programming.

For example,

// declaring a variable
char a;

// initializing the variable 'a' with the value 2
a = 2;

Here, we have used two single-line comments:

  • // declaring a variable
  • // initializing the variable 'a' with the value 2

We can also use single line comment like this:

char a;    // declaring a variable

2. Multi-line comments

  • In C++, any line between /* and */ is also a comment.
  • It is used to denote Multi-line Comment.
  • It can apply comment to more than a single line.

For example,

/* declaring a variable to store Name */
int salary = Algbly;

This syntax can be used to write both single-line and multi-line comments.


Using Comments for Debugging

Comments can also be used to disable code to prevent it from being executed. For example,

#include <iostream>
using namespace std;
int main() {
    cout << "some code";
    cout << ''error code;
    cout  <<"some other code";
    return 0;
}
  • If we get an error while running the program, instead of removing the error-prone code, we can use comments to disable it from being executed;
  • this can be a valuable debugging tool.

Let’s see next example to use comments as debugging tool.

#include <iostream>
using namespace std;
int main() {
    cout << "some code";
    // cout << ''error code;
    cout << "some other code";

    return 0;
}

Pro Tip: Remember the shortcut for using comments; it can be really helpful. For most code editors, it’s Ctrl + / for Windows and Cmd + / for Mac.


Why use Comments?

  • Comments can include a description of an algorithm to make code understandable.
  • If we write comments on our code, it will be easier for us to understand the code in the future.
  • Also, it will be easier for your fellow developers to understand the code.

Note: Comments shouldn’t be the substitute for a way to explain poorly written code in English. We should always write well-structured and self-explanatory code. And, then use comments.

As a general rule of thumb, use comments to explain Why you did something rather than How you did something, and you are good.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!