source: public/doc/gnu-c/Comments.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.9 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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&rsquo;s
67execution or meaning.
68</p>
69<p>The purpose of comments is to explain the code to people that read it.
70Writing good comments for your code is tremendously important&mdash;they
71should provide background information that helps programmers
72understand the reasons why the code is written the way it is. You,
73returning to the code six months from now, will need the help of these
74comments to remember why you wrote it this way.
75</p>
76<p>Outdated comments that become incorrect are counterproductive, so part
77of the software developer&rsquo;s responsibility is to update comments as
78needed to correspond with changes to the program code.
79</p>
80<p>C allows two kinds of comment syntax, the traditional style and the
81C<tt>++</tt> style. A traditional C comment starts with &lsquo;<samp>/*</samp>&rsquo; and ends
82with &lsquo;<samp>*/</samp>&rsquo;. 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 &lsquo;<samp>/*</samp>&rsquo;, but these delimiters do
89not nest as pairs. The first &lsquo;<samp>*/</samp>&rsquo; ends the comment regardless of
90whether it contains &lsquo;<samp>/*</samp>&rsquo; 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 &lsquo;<samp>//</samp>&rsquo; and ends at the end of the line.
97For 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 &lsquo;<samp>//</samp>&rsquo; inside a line
104comment 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>
108This 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
123comment. The delimiter &lsquo;<samp>/*</samp>&rsquo; doesn&rsquo;t start a comment if it occurs
124inside 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>&quot;/*&nbsp;blah&nbsp;*/&quot;<!-- /@w --></tt> is the string constant &lsquo;<samp>/*&nbsp;blah&nbsp;*/<!-- /@w --></samp>&rsquo;, not an empty
133string.
134</p>
135<p>In this manual we show the text in comments in a variable-width font,
136for readability, but this font distinction does not exist in source
137files.
138</p>
139<p>A comment is syntactically equivalent to whitespace, so it always
140separates 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
149visually from surrounding code.
150</p>
151<hr>
152<div class="header">
153<p>
154Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.