CPP Comments

C++ Tutorial

💻 C++ Comments (CPP Comments)

Comments in C++ are used to explain code.
They are ignored by the compiler and do not affect program execution.
Comments improve readability, debugging, and maintenance.


1️⃣ Single-Line Comments ⭐

Syntax

// This is a single-line comment

Example


 

Output

Hello C++

📌 The comment line is ignored.


2️⃣ Multi-Line Comments ⭐

Syntax

/*
This is
a multi-line
comment
*/

Example


 

Output

Welcome

3️⃣ Inline Comments ⭐


4️⃣ Commenting Out Code (Debugging) ⭐⭐

📌 Very useful for testing & debugging.


5️⃣ Nested Comments ❌ (Not Allowed)


❌ Causes compile-time error

📌 C++ does not support nested comments


6️⃣ Using Comments for Documentation ⭐⭐



7️⃣ Good Commenting Practices ✔️

✔ Explain why, not what
✔ Keep comments short and clear
✔ Update comments with code changes
✔ Avoid unnecessary comments


8️⃣ Common Comment Mistakes ❌

❌ Forgetting // or /* */
❌ Over-commenting simple code
❌ Writing misleading comments
❌ Expecting comments to execute


📌 Interview Questions (CPP Comments)

Q1. What are comments in C++?
👉 Non-executable statements ignored by compiler

Q2. Types of comments in C++?
👉 Single-line and Multi-line

Q3. Can comments be nested?
👉 No

Q4. Do comments affect program speed?
👉 No


✅ Summary

✔ Comments explain code
✔ Ignored by compiler
// → single-line
/* */ → multi-line
✔ Useful for debugging & documentation

You may also like...