|
| 1 | +--- |
| 2 | +title: Comments |
| 3 | +description: Auto‑generated from cppreference |
| 4 | +--- |
| 5 | + |
| 6 | +import { Desc, DescList, DocLink } from '@components/index'; |
| 7 | + |
| 8 | +# Comments |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +#### Block Comment |
| 15 | +Often known as "C-style" or "multi-line" comments. |
| 16 | + |
| 17 | +``` |
| 18 | +/* comment */ |
| 19 | +``` |
| 20 | +#### Single-line Comment |
| 21 | +Often known as "C++-style" or "single-line" comments. |
| 22 | +``` |
| 23 | +// comment |
| 24 | +``` |
| 25 | + |
| 26 | +All comments are removed from the program at <DocLink src="/cpp/language/translation_phases">translation phase 3</DocLink> by replacing each comment with a single whitespace character. |
| 27 | + |
| 28 | +## C-style |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +## C++-style |
| 33 | + |
| 34 | +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. |
| 35 | + |
| 36 | +## Notes |
| 37 | + |
| 38 | +Because comments <DocLink src="/cpp/language/translation_phases">are removed</DocLink> 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. |
| 39 | + |
| 40 | +Besides commenting out, other mechanisms used for source code exclusion are |
| 41 | + |
| 42 | +```cpp |
| 43 | +#if 0 |
| 44 | + std::cout << "this will not be executed or even compiled\n"; |
| 45 | +#endif |
| 46 | +``` |
| 47 | + |
| 48 | +and |
| 49 | + |
| 50 | +```cpp |
| 51 | +if (false) |
| 52 | +{ |
| 53 | + std::cout << "this will not be executed\n"; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Example |
| 58 | + |
| 59 | +```cpp |
| 60 | +#include <iostream> |
| 61 | + |
| 62 | +/* C-style comments can contain |
| 63 | +multiple lines */ |
| 64 | +/* or just one */ |
| 65 | + |
| 66 | +/************** |
| 67 | + * you can insert any *, but |
| 68 | + * you can't make comments nested |
| 69 | + */ |
| 70 | + |
| 71 | +// C++-style comments can comment one line |
| 72 | + |
| 73 | +// or, they can |
| 74 | +// be strung together |
| 75 | + |
| 76 | +int main() |
| 77 | +{ |
| 78 | + // comments are removed before preprocessing, |
| 79 | + // so ABC is "1", not "1//2134", and "1 hello world" |
| 80 | + // will be printed |
| 81 | +#define ABC 1//2134 |
| 82 | + std::cout << ABC << " hello world\n"; |
| 83 | + |
| 84 | + // The below code won't be run |
| 85 | + // return 1; |
| 86 | + |
| 87 | + // The below code will be run |
| 88 | + return 0; |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +Output: |
| 93 | + |
| 94 | +```text |
| 95 | +1 hello world |
| 96 | +``` |
| 97 | + |
| 98 | +## See also |
| 99 | + |
| 100 | +<DescList> |
| 101 | +<Desc> |
| 102 | +<DocLink slot="item" src="/c/comment">C documentation</DocLink> for <span>comment</span> |
| 103 | +</Desc> |
| 104 | +</DescList> |
0 commit comments