1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
<h1 id="firstHeading" class="firstHeading">Comments</h1> <p>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.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>/*</code> <span class="t-spar">comment</span> <code>*/</code> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
</tr> <tr class="t-sdsc"> <td> <code>//</code> <span class="t-spar">comment</span> </td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td>
</tr>
</table> <div class="t-li1">
<span class="t-li">1)</span> Often known as "C-style" or "multi-line" comments.</div> <div class="t-li1">
<span class="t-li">2)</span> Often known as "C++-style" or "single-line" comments.</div> <p>All comments are removed from the program at <a href="language/translation_phases" title="c/language/translation phases">translation phase 3</a> by replacing each comment with a single whitespace character.</p>
<h3 id="C-style"> C-style</h3> <p>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 <code>/*</code> and <code>*/</code>. C-style comments tell the compiler to ignore all content between <code>/*</code> and <code>*/</code>. Although it is not part of the C standard, <code>/**</code> and <code>**/</code> are often used to indicate documentation blocks; this is legal because the second asterisk is simply treated as part of the comment.</p>
<p>Except within a <a href="language/character_constant" title="c/language/character constant">character constant</a>, a <a href="language/string_literal" title="c/language/string literal">string literal</a>, or a comment, the characters <code>/*</code> introduce a comment. The contents of such a comment are examined only to identify multibyte characters and to find the characters <code>*/</code> that terminate the comment. C-style comments cannot be nested.</p>
<table class="t-rev-begin"> <tr class="t-rev t-since-c99">
<td> <h3 id="C.2B.2B-style"> C++-style</h3> <p>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 <code>//</code> and follow the text with the new line character. C++-style comments tell the compiler to ignore all content between <code>//</code> and a new line.</p>
<p>Except within a <a href="language/character_constant" title="c/language/character constant">character constant</a>, a <a href="language/string_literal" title="c/language/string literal">string literal</a>, or a comment, the characters <code>//</code> 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:</p>
<div class="c source-c"><pre data-language="c">// y = f(x); // invoke algorithm</pre></div> <p>A C-style comment may appear within a C++-style comment:</p>
<div class="c source-c"><pre data-language="c">// y = f(x); /* invoke algorithm */</pre></div> <p>A C++-style comment may appear within a C-style comment; this is a mechanism for excluding a small block of source code:</p>
<div class="c source-c"><pre data-language="c">/*
y = f(x); // invoke algorithms
z = g(x);
*/</pre></div> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <h3 id="Notes"> Notes</h3> <p>Because comments <a href="language/translation_phases" title="c/language/translation phases">are removed</a> 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.</p>
<div class="c source-c"><pre data-language="c">/* An attempt to use a macro to form a comment. */
/* But, a space replaces characters "//". */
#ifndef DEBUG
#define PRINTF //
#else
#define PRINTF printf
#endif
...
PRINTF("Error in file %s at line %i\n", __FILE__, __LINE__);</pre></div> <p>Besides commenting out, other mechanisms used for source code exclusion are:</p>
<div class="c source-c"><pre data-language="c">#if 0
puts("this will not be compiled");
/* no conflict with C-style comments */
// no conflict with C++-style comments
#endif</pre></div> <p>and</p>
<div class="c source-c"><pre data-language="c">if(0) {
puts("this will be compiled but not be executed");
/* no conflict with C-style comments */
// no conflict with C++-style comments
}</pre></div> <p>The introduction of // comments in C99 was a breaking change in some rare circumstances:</p>
<div class="c source-c"><pre data-language="c">a = b //*divisor:*/ c
+ d; /* C89 compiles a = b / c + d;
C99 compiles a = b + d; */</pre></div> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
/*
C-style comments can contain
multiple lines.
*/
/* Or, just one line. */
// C++-style comments can comment one line.
// Or, they can
// be strung together.
int main(void)
{
// The below code won't be run
// puts("Hello");
// The below code will be run
puts("World");
// A note regarding backslash + newline.
// Despite belonging to translation phase 2 (vs phase 3 for comments),
// '\' still determines which portion of the source code is considered
// as 'comments':
// This comment will be promoted to the next line \
puts("Won't be run"); // may issue a warning "multi-line comment"
puts("Hello, again");
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">World
Hello, again</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 6.4.9 Comments (p: 54) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 6.4.9 Comments (p: 75) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 6.4.9 Comments (p: 66) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 3.1.9 Comments </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/comments" title="cpp/comments" class="mw-redirect">C++ documentation</a></span> for <span class=""><span>Comments</span></span> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/comment" class="_attribution-link">https://en.cppreference.com/w/c/comment</a>
</p>
</div>
|