Skip to content

Commit d6e09cb

Browse files
1 parent 0477071 commit d6e09cb

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

src/content/docs/cpp/comments.mdx

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
```
15+
/* comment */
16+
```
17+
18+
(1)
19+
```
20+
// comment
21+
```
22+
23+
(2)
24+
25+
1) Often known as "C-style" or "multi-line" comments.
26+
2) Often known as "C++-style" or "single-line" comments.
27+
28+
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.
29+
30+
## C-style
31+
32+
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.
33+
34+
## C++-style
35+
36+
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.
37+
38+
## Notes
39+
40+
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.
41+
42+
Besides commenting out, other mechanisms used for source code exclusion are
43+
44+
```cpp
45+
#if 0
46+
std::cout << "this will not be executed or even compiled\n";
47+
#endif
48+
```
49+
50+
and
51+
52+
```cpp
53+
if (false)
54+
{
55+
std::cout << "this will not be executed\n";
56+
}
57+
```
58+
59+
## Example
60+
61+
```cpp
62+
#include <iostream>
63+
64+
/* C-style comments can contain
65+
multiple lines */
66+
/* or just one */
67+
68+
/**************
69+
* you can insert any *, but
70+
* you can't make comments nested
71+
*/
72+
73+
// C++-style comments can comment one line
74+
75+
// or, they can
76+
// be strung together
77+
78+
int main()
79+
{
80+
// comments are removed before preprocessing,
81+
// so ABC is "1", not "1//2134", and "1 hello world"
82+
// will be printed
83+
#define ABC 1//2134
84+
std::cout << ABC << " hello world\n";
85+
86+
// The below code won't be run
87+
// return 1;
88+
89+
// The below code will be run
90+
return 0;
91+
}
92+
```
93+
94+
Output:
95+
96+
```text
97+
1 hello world
98+
```
99+
100+
## See also
101+
102+
<DescList>
103+
<Desc>
104+
<DocLink slot="item" src="/c/comment">C documentation</DocLink> for <span>comment</span>
105+
</Desc>
106+
</DescList>

0 commit comments

Comments
 (0)