Namespaces
Variants
Actions

Comments

From cppreference.com
Revision as of 18:02, 22 September 2013 by P12 (Talk | contribs)


Comments are text blocks that are not interpreted by the compiler. Comments are used to provide additional information that can not be easily expressed in code. They are a good way to leave notes about why the code is written as it is, since even a single programmer tends to forget things. Comments are indispensable in large programmer teams.

There are two ways to declare a comment block in C++:

  • Single-line comment. The compiler always ignores everything after // till the end of a line.
  • Multi-line comment. The compiler always ignores everything between /* and */. Be sure that the comment itself does not contain */, because this would cause the compiler to end the comment prematurely and probably emit a compilation error, since the rest of the comment is usually not proper C++.

The following example demonstrates the usage of comments in the "Hello world!" program:

/* Hello world example
 
   This comment spans several lines
*/
#include <iostream>
 
int main() /* This comment spans part of a line */
{
    std::cout << "Hello World!\n"; // till the end of the line
    return 0;
}

Output:

Hello World!