C++ Comments

C++ Comments

Comments are text in your code that the C++ compiler completely ignores. They are used to explain what the code does, making it easier for you and others to read and maintain.

Single-line Comments

Single-line comments start with two forward slashes //. Any text between // and the end of the line is ignored.

Example

// This is a single-line comment
cout << "Hello World!"; // This prints Hello World to the screen

Multi-line Comments

Multi-line comments start with /* and end with */. Any text between these markers will be ignored by the compiler.

Example

/*
This is a multi-line comment.
It can span multiple lines.
Very useful for long explanations.
*/
cout << "Hello World!";

Using comments effectively is a sign of a good programmer. Always write clear comments to describe complex logic!


Exercise

?

How do you start a single-line comment in C++?