Types of comments in c++ – Single line
comments in c++
In Types of comments in c++, you can include comments in your program in two different ways. First, you can use the standard, Types of comments in c++-like comment mechanism. That is, begin a comment with /* and end it with */. As with C, this type of comment cannot be nested in C++Single line comments in c++.
The second way that you can add a remark to your C++ program is to use the single-line comment. A single-line comment begins with a // and stops at the end of the line. Other than the physical end of the line (that is, a carriage-return/linefeed combination), a single-line comment uses no comment terminator symbol.
Typically, C++ programmers like Single line comments in c++ comments for multiline commentaries and reserve Single line comments in c++ for short remarks.
Here is a program that contains both C and C++-style comments:
This is a C-like comment.
This program determines whether an integer is odd or even
#include <iostream> using namespace std;
int num; // this is a C++ single-line comment
// read the number cout << “Enter number to be tested: “; cin >> num;
// see if even or odd
if((num%2)==0) cout << “Number is even\n”;
else cout << “Number is odd\n”;
return 0 ;
While multiline comments cannot be nested, it is possible to nest a single-line comment within a multiline comment. For example, this is perfectly valid:
|This is||a multiline||comment|
|inside||of which //||is nested a single-line||comment.|
|the end of||the multiline comment.|
As an experiment, determine whether this comment (which nests a C-like comment within a C++-style, single-line comment) is valid:
Read My Other Posts