Home / Programming / Types of comments in c++ – Single line comments in c++ (Explained)

Types of comments in c++ – Single line comments in c++ (Explained)

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.

Types of comments in c++ - Single line comments in c++
Types of comments in c++ – Single line comments in c++

Typically, C++ programmers like Single line comments in c++ comments for multiline commentaries and reserve Single line comments in c++ for short remarks.

 

Also, Read – Two versions of the C++ programming language? Explained in details

EXAMPLES

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 main()

{

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.
Here is

*/

the end of the multiline comment.

EXERCISES

As an experiment, determine whether this comment (which nests a C-like comment within a C++-style, single-line comment) is valid:

// This is a strange /* way to do a comment */

 

Read My Other Posts

1. Blogging For Starters – Learn How to start a successful blog in 2018

2.Learn C++ Programming Within 14 Days ( Introduction )

About rhtoufiq

Check Also

WHAT IS OBJECT ORIENTED PROGRAMMING?

WHAT IS OBJECT ORIENTED PROGRAMMING?   Object-oriented programming is a powerful way to approach the …

Leave a Reply