C++ supports two ways of commenting code:
|
|
The first of them, known as line comment, discards everything from where the pair of slash signs (
//
) are found up to the end of that same line. The second one, known as block comment, discards everything between the /*
characters and the first appearance of the */
characters, with the possibility of including multiple lines.Let's add comments to our second program:
/* my second program in C++
with more comments */
#include
int main ()
{
std::cout << "Hello World! "; // prints Hello World!
std::cout << "I'm a C++ program"; // prints I'm a C++ program
}
Output
Hello World! I'm a C++ program
If comments are included within the source code of a program without using the comment characters combinations
//
, /*
or */
, the compiler takes them as if they were C++ expressions, most likely causing the compilation to fail with one, or several, error messages.... Qweit ...