1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Comments (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Comments (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Comments (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Lexical-Syntax.html" rel="up" title="Lexical Syntax">
|
---|
33 | <link href="Identifiers.html" rel="next" title="Identifiers">
|
---|
34 | <link href="Whitespace.html" rel="prev" title="Whitespace">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Comments"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Identifiers.html" accesskey="n" rel="next">Identifiers</a>, Previous: <a href="Whitespace.html" accesskey="p" rel="prev">Whitespace</a>, Up: <a href="Lexical-Syntax.html" accesskey="u" rel="up">Lexical Syntax</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Comments-1"></span><h3 class="section">5.4 Comments</h3>
|
---|
64 | <span id="index-comments"></span>
|
---|
65 |
|
---|
66 | <p>A comment encapsulates text that has no effect on the program’s
|
---|
67 | execution or meaning.
|
---|
68 | </p>
|
---|
69 | <p>The purpose of comments is to explain the code to people that read it.
|
---|
70 | Writing good comments for your code is tremendously important—they
|
---|
71 | should provide background information that helps programmers
|
---|
72 | understand the reasons why the code is written the way it is. You,
|
---|
73 | returning to the code six months from now, will need the help of these
|
---|
74 | comments to remember why you wrote it this way.
|
---|
75 | </p>
|
---|
76 | <p>Outdated comments that become incorrect are counterproductive, so part
|
---|
77 | of the software developer’s responsibility is to update comments as
|
---|
78 | needed to correspond with changes to the program code.
|
---|
79 | </p>
|
---|
80 | <p>C allows two kinds of comment syntax, the traditional style and the
|
---|
81 | C<tt>++</tt> style. A traditional C comment starts with ‘<samp>/*</samp>’ and ends
|
---|
82 | with ‘<samp>*/</samp>’. For instance,
|
---|
83 | </p>
|
---|
84 | <div class="example">
|
---|
85 | <pre class="example">/* <span class="roman">This is a comment in traditional C syntax.</span> */
|
---|
86 | </pre></div>
|
---|
87 |
|
---|
88 | <p>A traditional comment can contain ‘<samp>/*</samp>’, but these delimiters do
|
---|
89 | not nest as pairs. The first ‘<samp>*/</samp>’ ends the comment regardless of
|
---|
90 | whether it contains ‘<samp>/*</samp>’ sequences.
|
---|
91 | </p>
|
---|
92 | <div class="example">
|
---|
93 | <pre class="example">/* <span class="roman">This</span> /* <span class="roman">is a comment</span> */ But this is not! */
|
---|
94 | </pre></div>
|
---|
95 |
|
---|
96 | <p>A <em>line comment</em> starts with ‘<samp>//</samp>’ and ends at the end of the line.
|
---|
97 | For instance,
|
---|
98 | </p>
|
---|
99 | <div class="example">
|
---|
100 | <pre class="example">// <span class="roman">This is a comment in C<tt>++</tt> style.</span>
|
---|
101 | </pre></div>
|
---|
102 |
|
---|
103 | <p>Line comments do nest, in effect, because ‘<samp>//</samp>’ inside a line
|
---|
104 | comment is part of that comment:
|
---|
105 | </p>
|
---|
106 | <div class="example">
|
---|
107 | <pre class="example">// <span class="roman">this whole line is</span> // <span class="roman">one comment</span>
|
---|
108 | This is code, not comment.
|
---|
109 | </pre></div>
|
---|
110 |
|
---|
111 | <p>It is safe to put line comments inside block comments, or vice versa.
|
---|
112 | </p>
|
---|
113 | <div class="example">
|
---|
114 | <pre class="example">/* <span class="roman">traditional comment</span>
|
---|
115 | // <span class="roman">contains line comment</span>
|
---|
116 | <span class="roman">more traditional comment</span>
|
---|
117 | */ text here is not a comment
|
---|
118 |
|
---|
119 | // <span class="roman">line comment</span> /* <span class="roman">contains traditional comment</span> */
|
---|
120 | </pre></div>
|
---|
121 |
|
---|
122 | <p>But beware of commenting out one end of a traditional comment with a line
|
---|
123 | comment. The delimiter ‘<samp>/*</samp>’ doesn’t start a comment if it occurs
|
---|
124 | inside an already-started comment.
|
---|
125 | </p>
|
---|
126 | <div class="example">
|
---|
127 | <pre class="example"> // <span class="roman">line comment</span> /* <span class="roman">That would ordinarily begin a block comment.</span>
|
---|
128 | Oops! The line comment has ended;
|
---|
129 | this isn't a comment any more. */
|
---|
130 | </pre></div>
|
---|
131 |
|
---|
132 | <p>Comments are not recognized within string constants. <tt>"/* blah */"<!-- /@w --></tt> is the string constant ‘<samp>/* blah */<!-- /@w --></samp>’, not an empty
|
---|
133 | string.
|
---|
134 | </p>
|
---|
135 | <p>In this manual we show the text in comments in a variable-width font,
|
---|
136 | for readability, but this font distinction does not exist in source
|
---|
137 | files.
|
---|
138 | </p>
|
---|
139 | <p>A comment is syntactically equivalent to whitespace, so it always
|
---|
140 | separates tokens. Thus,
|
---|
141 | </p>
|
---|
142 | <div class="example">
|
---|
143 | <pre class="example"> int/* <span class="roman">comment</span> */foo;
|
---|
144 | <span class="roman">is equivalent to</span>
|
---|
145 | int foo;
|
---|
146 | </pre></div>
|
---|
147 |
|
---|
148 | <p>but clean code always uses real whitespace to separate the comment
|
---|
149 | visually from surrounding code.
|
---|
150 | </p>
|
---|
151 | <hr>
|
---|
152 | <div class="header">
|
---|
153 | <p>
|
---|
154 | Next: <a href="Identifiers.html" accesskey="n" rel="next">Identifiers</a>, Previous: <a href="Whitespace.html" accesskey="p" rel="prev">Whitespace</a>, Up: <a href="Lexical-Syntax.html" accesskey="u" rel="up">Lexical Syntax</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
155 | </div>
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 | </body>
|
---|
160 | </html>
|
---|