Namespaces
Variants
Views
Actions

Comments

From cppreference.com
< cpp
 
 
C++ language
General topics
Preprocessor
Comments
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++20)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static
Special member functions
Templates
Miscellaneous
 
 

Comments serve as a sort of in-code documentation. When inserted into a program, they are effectively ignored by the compiler; they are solely intended to be used as notes by the humans that read source code. Although specific documentation is not part of the C++ standard, several utilities exist that parse comments with different documentation formats.

Contents

[edit] Syntax

/* comment */ (1)
// comment (2)
1) Often known as "C-style" or "multi-line" comments.
2) Often known as "C++-style" or "single-line" comments.

All comments are removed from the program at translation phase 3 by replacing each comment with a single whitespace character.

[edit] C-style

C-style comments are usually used to comment large blocks of text, however, they can be used to comment single lines. To insert a C-style comment, simply surround text with /* and */; this will cause the contents of the comment to be ignored by the compiler. Although it is not part of the C++ standard, /** and */ are often used to indicate documentation blocks; this is legal because the second asterisk is simply treated as part of the comment. C-style comments cannot be nested.

[edit] C++-style

C++-style comments are usually used to comment single lines, however, multiple C++-style comments can be placed together to form multi-line comments. C++-style comments tell the compiler to ignore all content between // and a new line.

[edit] Notes

Because comments are removed before the preprocessor stage, a macro cannot be used to form a comment and an unterminated C-style comment doesn't spill over from an #include'd file.

Besides commenting out, other mechanisms used for source code exclusion are

#if 0
    std::cout << "this will not be executed or even compiled\n";
#endif

and

if(false) {
    std::cout << "this will not be executed\n";
}

[edit] Example

#include <iostream>
/* C-style comments can contain
multiple lines */
/* or just one */
 
/**************
 *  you can insert any *, but
 *  you can't make comments nested
 */
 
// C++-style comments can comment one line
 
// or, they can
// be strung together
 
int main()
{
  // comments are removed before preprocessing,
  // so ABC is "1", not "1//2134", and "1 hello world"
  // will be printed
#define ABC 1//2134
  std::cout << ABC << " hello world\n";
 
  // The below code won't be run
  // return 1;
 
  // The below code will be run
  return 0;
}

Output:

1 hello world

[edit] See also

C documentation for comment