📄 x-coding3.html
字号:
<tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="top"><td colspan=1 rowspan=1><p class="BodyLeft"><a name="85075"> </a>INCORRECT:</p></td><td colspan=1 rowspan=1><pre class="CodeLeft"><b><a name="85081">typedef struct tcbInfo { FOO_INFO * pfooInfo; ... } TCB_INFO;</a></b></pre></td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></div><br></dl></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84467">Subroutines</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84469"> </a>There are two formats for subroutine declarations, depending on whether the subroutine takes arguments. </p></dl><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84470"> </a>For subroutines that take arguments, the subroutine return type and name appear on the first line, the opening parenthesis on the next, followed by the arguments to the routine, each on a separate line. The declaration is concluded by a line containing the closing parenthesis. For example:</li></ul></p><dl class="margin"><dd><pre class="Code2"><b><a name="84471">int lstFind ( LIST * pList, /* list in which to search */ NODE * pNode /* pointer to node to search for */ )</a></b></pre></dl><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84472"> </a>For subroutines that take no parameters, the word <i class="term">void</i> in parentheses is required and appears on the same line as the subroutine return type and name. For example:</li></ul></p><dl class="margin"><dd><pre class="Code2"><b><a name="84473">STATUS fppProbe (void)</a></b></pre></dl></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84474">I.3.4 C Code Layout</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84476"> </a>The maximum length for any line of code is 80 characters.</p><dd><p class="Body"><a name="84477"> </a>The rest of this section describes the conventions for the graphic layout of C code, and covers the following elements:</p></dl><dl class="margin"><ul class="BulletSingle" type="disc"><li><a name="84478"> </a>vertical spacing</li></ul><ul class="BulletSingle" type="disc"><li><a name="84479"> </a>horizontal spacing</li></ul><ul class="BulletSingle" type="disc"><li><a name="84480"> </a>indentation</li></ul><ul class="BulletSingle" type="disc"><li><a name="84481"> </a>comments</li></ul></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84482">Vertical Spacing</a></i></h4></font><dl class="margin"><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84483"> </a>Use blank lines to make code more readable and to group logically related sections of code together. Put a blank line before and after comment lines.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84484"> </a>Do not put more than one declaration on a line. Each variable and function argument must be declared on a separate line. Do not use comma-separated lists to declare multiple identifiers.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84485"> </a>Do not put more than one statement on a line. The only exceptions are the <b class="keyword">for</b> statement, where the initial, conditional, and loop statements can go on a single line:</li></ul></p><dl class="margin"><dd><pre class="Code2"><b><a name="84486">for (i = 0; i < count; i++)</a></b></pre></dl><dl class="margin"><dd><div class="Indent"><a name="84487"> </a>or the <b class="keyword">switch</b> statement if the actions are short and nearly identical (see the <b class="keyword">switch</b> statement format in <a href="x-coding3.html#84501"><i class="title">Indentation</i></a>).</div><br><dd><div class="Indent"><a name="84491"> </a>The <b class="keyword">if</b> statement is not an exception: the executed statement always goes on a separate line from the conditional expression:</div><br></dl><dl class="margin"><dd><pre class="Code2"><b><a name="84492">if (i > count) i = count;</a></b></pre></dl><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84493"> </a>Braces (<b class="operator">{</b> and <b class="operator">}</b>) and <b class="keyword">case</b> labels always have their own line.</li></ul></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84495">Horizontal Spacing</a></i></h4></font><dl class="margin"><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84496"> </a>Put spaces around binary operators, after commas, and before an open parenthesis. Do not put spaces around structure members and pointer operators. Put spaces before open brackets of array subscripts; however, if a subscript is only one or two characters long, the space can be omitted. For example:</li></ul></p><dl class="margin"><dd><pre class="Code2"><b><a name="84497">status = fooGet (foo, i + 3, &value); foo.index pFoo->index fooArray [(max + min) / 2] string[0]</a></b></pre></dl><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84498"> </a>Line up continuation lines with the part of the preceding line they continue:</li></ul></p><dl class="margin"><dd><pre class="Code2"><b><a name="84499">a = (b + c) * (d + e); status = fooList (foo, a, b, c, d, e); if ((a == b) && (c == d)) ...</a></b></pre></dl></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84501">Indentation</a></i></h4></font><dl class="margin"><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84502"> </a>Indentation levels are every four characters (columns 1, 5, 9, 13, ). </li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84503"> </a>The module and subroutine headings and the subroutine declarations start in column one.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84504"> </a>Indent one indentation level after:</li></ul></p><dl class="margin"><ul class="DashSingle2" type="circle"><li><a name="84505"> </a>subroutine declarations</li></ul><ul class="DashSingle2" type="circle"><li><a name="84506"> </a>conditionals (see below)</li></ul><ul class="DashSingle2" type="circle"><li><a name="84507"> </a>looping constructs</li></ul><ul class="DashSingle2" type="circle"><li><a name="84508"> </a>switch statements</li></ul><ul class="DashSingle2" type="circle"><li><a name="84509"> </a>case labels</li></ul><ul class="DashSingle2" type="circle"><li><a name="84510"> </a>structure definitions in a <b class="keyword">typedef</b> </li></ul></dl><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84511"> </a>The <b class="keyword">else</b> of a conditional has the same indentation as the corresponding <b class="keyword">if</b>. Thus the form of the conditional is:</li></ul></p><dl class="margin"><dd><pre class="Code2"><b><a name="84512">if (<i class="textVariable"> condition </i>) { <i class="textVariable">statements </i> } else { <i class="textVariable">statements </i> }</a></b></pre></dl><dl class="margin"><dd><div class="Indent"><a name="84513"> </a>The form of the conditional statement with an <b class="keyword">else if</b> is:</div><br></dl><dl class="margin"><dd><pre class="Code2"><b><a name="84514">if (<i class="textVariable"> condition </i>) { <i class="textVariable">statements </i> } else if (<i class="textVariable"> condition </i>) { <i class="textVariable">statements </i> } else { <i class="textVariable">statements </i> }</a></b></pre></dl><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84516"> </a>The general form of the <b class="keyword">switch</b> statement is:</li></ul></p><dl class="margin"><dd><pre class="Code2"><b><a name="84517">switch (<i class="textVariable"> input </i>) { case 'a': ... break; case 'b': ... break; default: ... break; }</a></b></pre></dl><dl class="margin"><dd><div class="Indent"><a name="84518"> </a>If the actions are very short and nearly identical in all cases, an alternate form of the switch statement is acceptable:</div><br></dl><dl class="margin"><dd><pre class="Code2"><b><a name="84519">switch (<i class="textVariable"> input </i>) { case 'a': x = aVar; break; case 'b': x = bVar; break; case 'c': x = cVar; break; default: x = defaultVar; break; }</a></b></pre></dl><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84520"> </a>Comments have the same indentation level as the section of code to which they refer (see <a href="x-coding3.html#84526"><i class="title">Comments</i></a>).</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84524"> </a>Section braces<b class="operator"> </b>(<b class="operator">{</b> and <b class="operator">}</b>) have the same indentation as the code they enclose.</li></ul></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84526">Comments</a></i></h4></font><dl class="margin"><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84527"> </a>Place comments within code so that they precede the section of code to which they refer and have the same level of indentation. Separate such comments from the code by a single blank line.</li></ul></p><dl class="margin"><p class="listspace"><ul class="Dash2" type="circle"><li><a name="84528"> </a>Begin single-line comments with the open-comment and end with the close-comment, as in the following:</li></ul></p><dl class="margin"><dd><pre class="Code3"><b><a name="84529">/* This is the correct format for a single-line comment */ foo = MAX_FOO;</a></b></pre></dl><p class="listspace"><ul class="Dash2" type="circle"><li><a name="84530"> </a>Begin and end multi-line comments with the open-comment and close-comment on separate lines, and precede each line of the comment with an asterisk (*), as in the following:</li></ul></p><dl class="margin"><dd><pre class="Code3"><b><a name="84531">/* * This is the correct format for a multiline comment * in a section of code. */ foo = MIN_FOO;</a></b></pre></dl></dl>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -