C++ Comments

Comments are used to explain the code in C++ to make the code more readable and easier to understand. Comments can also be used to stop execution while we test another code.

There are two types of comments: single-lined and multi-lined.

Single-line comments start with two forward slashes (//).
Text after two forward slashes is ignored by the compiler.

This example uses a single-line comment before a line of code:

Example :

// This is a comment
cout << "Hello World!";


The below example uses a single-line comment at the end of a line of code:



Example :

cout << "Hello World!"; // This is a comment

C++ Multi-line Comments

Multi-line comments start with /* and ends with */.
Text between /* and */ is ignored by the compiler:

Example :

/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";

Exercise:-

1. Single-line comments represent by

A. \
B. \\
C. /
D. //

View Answer


2. Start of Multi-line comments represent by

A. /*
B. */
C. */
D. //

View Answer



Program

C++ Program to Swap Two Numbers

#include <iostream>
using namespace std;
int main()
{
int a = 7, b = 15, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}


Output:
Before swapping.
a = 7, b = 15
After swapping.
a = 15, b = 7





Visit :


Discussion



* You must be logged in to add comment.