C++ Comments

In C++, comments are special annotations in the code that are ignored by the compiler. They are used to provide explanations, descriptions, or notes about the code to make it more understandable and maintainable. Comments are essential for documenting code, explaining complex logic, and reminding programmers of areas that need further attention.

Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code

There are two types of comments

1. Single-line comments

These comments start with // and continue to the end of the line. They are used for brief explanations.

Syntax:


// This is a single-line comment

Example:


 // Declare a variable a and initialize it to 20
int a= 20; 

Multi-line Comments

These comments start with /* and end with */. They can span across multiple lines.

Syntax:


/* This is a multi-line
   Comments */

Example:


/* This is a 
   multi-line comment
   that spans multiple lines */
int b = 50;

C++ Comments – Interview Questions

Q 1: What are comments?

Ans: Comments explain code and are ignored by the compiler.

Q 2: Single-line comment syntax?

Ans: //

Q 3: Multi-line comment syntax?

Ans: /* */

Q 4: Do comments affect performance?

Ans: No.

Q 5: Why use comments?

Ans: To improve code readability.

C++ Comments – Objective Questions (MCQs)

Q1. Which symbol is used to write a single-line comment in C++?






Q2. How do you write a multi-line comment in C++?






Q3. What will happen if you forget to close a multi-line comment (/*) in C++?






Q4. Which of the following is the correct way to write a comment on the same line as code?






Q5. What is the purpose of comments in a C++ program?






Related C++ Comments Topics