Skip to content

Commit aefc307

Browse files
feat: migrate c/comment from cppref [#44]
1 parent bec9b54 commit aefc307

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

src/content/docs/c/comment.mdx

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
title: "Comments"
3+
description: Auto‑generated from cppreference
4+
---
5+
6+
import { Desc, DescList, DocLink, Revision } from '@components/index';
7+
8+
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.
9+
10+
### Syntax
11+
12+
(1) `/*` <span class="t-spar">comment</span> `*/`
13+
(2) `//` <span class="t-spar">comment</span> <Revision since="C99">(since C99)</Revision>
14+
15+
1) Often known as "C-style" or "multi-line" comments.
16+
2) Often known as "C++-style" or "single-line" comments.
17+
18+
All comments are removed from the program at <DocLink src="/c/language/translation_phases">translation phase 3</DocLink> by replacing each comment with a single whitespace character.
19+
20+
### C-style
21+
22+
C-style comments are usually used to comment large blocks of text or small fragments of code; however, they can be used to comment single lines. To insert text as a C-style comment, simply surround the text with `/*` and `*/`. C-style comments tell the compiler to ignore all content between `/*` and `*/`. 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.
23+
24+
Except within a <DocLink src="/c/language/character_constant">character constant</DocLink>, a <DocLink src="/c/language/string_literal">string literal</DocLink>, or a comment, the characters `/*` introduce a comment. The contents of such a comment are examined only to identify multibyte characters and to find the characters `*/` that terminate the comment. C-style comments cannot be nested.
25+
26+
<Revision since="C99">
27+
28+
### C++-style
29+
30+
C++-style comments are usually used to comment single lines of text or code; however, they can be placed together to form multi-line comments. To insert text as a C++-style comment, simply precede the text with `//` and follow the text with the new line character. C++-style comments tell the compiler to ignore all content between `//` and a new line.
31+
32+
Except within a <DocLink src="/c/language/character_constant">character constant</DocLink>, a <DocLink src="/c/language/string_literal">string literal</DocLink>, or a comment, the characters `//` introduce a comment that includes all multibyte characters up to, but not including, the next new-line character. The contents of such a comment are examined only to identify multibyte characters and to find the new-line character that terminates the comment. C++-style comments can be nested:
33+
34+
```c
35+
// y = f(x); // invoke algorithm
36+
```
37+
38+
A C-style comment may appear within a C++-style comment:
39+
40+
```c
41+
// y = f(x); /* invoke algorithm */
42+
```
43+
44+
A C++-style comment may appear within a C-style comment; this is a mechanism for excluding a small block of source code:
45+
46+
```c
47+
/*
48+
y = f(x); // invoke algorithms
49+
z = g(x);
50+
*/
51+
```
52+
53+
</Revision>
54+
55+
### Notes
56+
57+
Because comments <DocLink src="/c/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.
58+
59+
```c
60+
/* An attempt to use a macro to form a comment. */
61+
/* But, a space replaces characters "//". */
62+
#ifndef DEBUG
63+
#define PRINTF //
64+
#else
65+
#define PRINTF printf
66+
#endif
67+
...
68+
PRINTF("Error in file%s at line%i\n", __FILE__, __LINE__);
69+
```
70+
71+
Besides commenting out, other mechanisms used for source code exclusion are:
72+
73+
```c
74+
#if 0
75+
puts("this will not be compiled");
76+
/* no conflict with C-style comments */
77+
// no conflict with C++-style comments
78+
#endif
79+
```
80+
81+
and
82+
83+
```c
84+
if(0) {
85+
puts("this will be compiled but not be executed");
86+
/* no conflict with C-style comments */
87+
// no conflict with C++-style comments
88+
}
89+
```
90+
91+
The introduction of // comments in C99 was a breaking change in some rare circumstances:
92+
93+
```c
94+
a = b //*divisor:*/ c
95+
+ d; /* C89 compiles a = b / c + d;
96+
C99 compiles a = b + d; */
97+
```
98+
99+
### Example
100+
101+
```cpp
102+
#include <stdio.h>
103+
/*
104+
C-style comments can contain
105+
multiple lines.
106+
*/
107+
108+
/* Or, just one line. */
109+
110+
// C++-style comments can comment one line.
111+
112+
// Or, they can
113+
// be strung together.
114+
115+
int main(void)
116+
{
117+
// The below code won't be run
118+
// puts("Hello");
119+
120+
// The below code will be run
121+
puts("World");
122+
123+
// A note regarding backslash + newline.
124+
// Despite belonging to translation phase 2 (vs phase 3 for comments),
125+
// '\' still determines which portion of the source code is considered
126+
// as 'comments':
127+
// This comment will be promoted to the next line \
128+
puts("Won't be run"); // may issue a warning "multi-line comment"
129+
puts("Hello, again");
130+
}
131+
```
132+
133+
Output:
134+
135+
```text
136+
World
137+
Hello, again
138+
```
139+
140+
### References
141+
142+
* C17 standard (ISO/IEC 9899:2018):
143+
* 6.4.9 Comments (p: 54)
144+
* C11 standard (ISO/IEC 9899:2011):
145+
* 6.4.9 Comments (p: 75)
146+
* C99 standard (ISO/IEC 9899:1999):
147+
* 6.4.9 Comments (p: 66)
148+
* C89/C90 standard (ISO/IEC 9899:1990):
149+
* 3.1.9 Comments
150+
151+
### See also
152+
153+
<DescList>
154+
<Desc>
155+
<DocLink slot="item" src="/cpp/comments">C++ documentation</DocLink> for <span>Comments</span>
156+
</Desc>
157+
</DescList>

0 commit comments

Comments
 (0)