📄 faqcataae2.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file style.sgml dated Sat Jul 3 17:22:13 2004 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=style by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:22 GMT --><head><base ><meta name=GENERATOR content="faqproc"><title>Style</title></head><body bgcolor="#ffffff"><H1>17. Style</H1><a name="layout"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../style/layout.html"><!-- qtag -->Question 17.1</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the best style for code layout in C?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Kernighan and Ritchie,while providing the example most often copied,also supply a good excuse fordisregardingit:<blockquote>The position of braces is less important,although people hold passionate beliefs.We have chosen one of several popular styles.Pick a style that suits you,then use it consistently.</blockquote></p><p>It is more important that the layout chosen be consistent(with itself, and with nearby or common code)than that it be ``perfect.''If your coding environment (i.e. local custom or company policy)does not suggest a style,and you don't feel like inventingyour own,just copy K&R.</p><p>Each of the various popular styles has itsgoodand badpoints.Putting theopen braceon a line by itself wastesvertical space;combining it with the following line makes it hard to edit; combining itwith the previous line prevents it from lining up with the close braceand may make it harder to see.</p><p>Indenting by eight columns per level is most common,but often gets youuncomfortably close to the right margin(which may be a hint that you shouldbreak up the function).If you indent by one tab but settabstops atsomething otherthan eight columns,you're requiring otherpeople to read your code with the same software setup that you used.(See also the Indian Hill Style Guide.)</p><p>The elusive quality of ``good style'' involves much morethan mere code layout details;don't spend time on formatting to the exclusion ofmore substantive code quality issues.</p><p>See also question<a href="faqcataae2.html?sec=style#srcfiles">17.2</a>.</p><p>References:K&R1 Sec. 1.2 p. 10<br>K&R2 Sec. 1.2 p. 10<hr><hr><hr><a name="srcfiles"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../style/srcfiles.html"><!-- qtag -->Question 17.2</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How should functions be apportioned among source files?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Usually, related functions are put together in one file.Sometimes(as when developing libraries)it is appropriate to haveexactly one source file(and, consequently, oneobject module)per independent function.Other times,and especially forsome programmers,numerous source filescan becumbersome,and it may be tempting(or even appropriate)to putmost or all of a program ina few big source files.When it is desired to limit the scope ofcertainfunctions orglobal variablesby using the <TT>static</TT> keyword,source file layout becomesmoreconstrained:thestatic functions and variablesand the functions sharing access to themmust allbe in the same file.</p><p>In other words, there are a number of tradeoffs,so it is difficult to give general rules.See also questions<a href="faqcatd3c2.html?sec=decl#decldef">1.7</a>,<a href="faqcatd3c2.html?sec=decl#semiglobal">1.9</a>,<a href="faqcat204f.html?sec=cpp#hfiles">10.6</a>,and<a href="faqcat204f.html?sec=cpp#nestincl">10.7</a>.<hr><hr><hr><a name="strcmp"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../style/strcmp.html"><!-- qtag -->Question 17.3</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Here's a neat trick for checking whether two strings are equal:<pre>if(!strcmp(s1, s2))</pre>Is thisgood style?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It is not particularly good style,although it is a popular idiom.The test succeeds if the two strings are equal,butthe use of <TT>!</TT> (``not'')suggests that it tests forinequality.</p><p>Anotheroptionis todefinea macro:<pre> #define Streq(s1, s2) (strcmp((s1), (s2)) == 0)</pre>which you can then use like this:<pre> if(Streq(s1, s2))</pre></p><p>Another option(which borders on preprocessor abuse;see question <a href="faqcat204f.html?sec=cpp#slm">10.2</a>)is to define<pre> #define StrRel(s1, op, s2) (strcmp(s1, s2) op 0)</pre>after which you can say things like<pre> if(StrRel(s1, ==, s2)) ... if(StrRel(s1, !=, s2)) ... if(StrRel(s1, >=, s2)) ...</pre></p><p>See also question <a href="faqcataae2.html?sec=style#stylewars">17.10</a>.<hr><hr><hr><a name="revtest"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../style/revtest.html"><!-- qtag -->Question 17.4</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why do some people write<TT>if(0 == x)</TT>instead of<TT>if(x == 0)</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It's a trick to guard against the common error of writing<pre>if(x = 0)</pre>If you're in the habit of writing the constant before the<TT>==</TT>,the compiler will complain if you accidentally type<pre> if(0 = x)</pre>Evidentlyit can be easierfor some peopleto remember to reverse the testthanto remember to typethe doubled <TT>=</TT> sign.(To be sure,accidentally using<TT>=</TT> instead of <TT>==</TT>is a typo which even the most experienced C programmer can make.)</p><p>On the other hand,somepeople find these reversed tests ugly or distracting,and argue that a compiler should warn about <TT>if(x = 0)</TT>.(In fact, many compilers do warn aboutassignments in conditionals,though you can alwayswrite<TT>if((x = expression))</TT>or<TT>if((x = expression) != 0)</TT>ifyou really mean it.)</p><p>References:H&S Sec. 7.6.5 pp. 209-10<hr><hr><hr><a name="protomacro"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../style/protomacro.html"><!-- qtag -->Question 17.4b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I've seen function declarations that look like this:<pre>extern int func __((int, int));</pre>What are thoseextra parentheses and underscoresfor?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>They'repart ofa trick which allowsthe prototype part of the function declarationto be turned off for a pre-ANSI compiler.Somewhere else is a conditional definitionof the <TT>__</TT> macrolike this:<pre> #ifdef __STDC__ #define __(proto) proto #else #define __(proto) () #endif</pre>The extra parentheses in the invocation<pre> extern int func __((int, int));</pre>are required so that the entire prototype list(perhaps containing many commas)is treated asthe single argumentexpected by the macro.</p><p>Additional links:<a href="../../style/protomac.20000601.html">further reading</a><hr><hr><hr><a name="voidcasts"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../style/voidcasts.html"><!-- qtag -->Question 17.5</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I came across some code thatputsa <TT>(void)</TT> cast beforeeachcall to <TT>printf</TT>.Why?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font><TT>printf</TT>does return a value(the number of characters printed,or an error code),though few programsbother tocheck the return values from each call.Since some compilers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -