⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faqcate034.html

📁 this is a mirrored site c-faq. thought might need offline
💻 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 bool.sgml dated Thu Jul 15 19:41:07 2004 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=bool by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:57:58 GMT --><head><base ><meta name=GENERATOR content="faqproc"><title>Boolean Expressions and Variables</title></head><body bgcolor="#ffffff"><H1>9. Boolean Expressions and Variables</H1><a name="booltype"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../bool/booltype.html"><!-- qtag -->Question 9.1</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What is the right type to use for Boolean values in C?Is there a standard type?ShouldI use<TT>#define</TT>s or enumsfor the true and false values?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Traditionally,C did not provide a standard Boolean type,partlyand partly to allow the programmer to make the appropriatespace/time tradeoff.(Using an <TT>int</TT>may be faster,while using <TT>char</TT>maysave data space.<a href="../../bool/bitfield.html" rel=subdocument>[footnote]</a>Smaller types may make thegeneratedcode bigger or slower,though,if they require lots of conversions to and from <TT>int</TT>.)</p><p>However, C99 <em>does</em> define a standard Boolean type,as long as you include <TT>&lt;stdbool.h&gt;</TT>.</p><p>If you decide to define them yourself,the choice between <TT>#define</TT>sand enumeration constantsfor the true/false valuesis arbitrary and notterribly interesting(see also questions<a href="faqcat6b6b.html?sec=struct#enumvsdefine">2.22</a>and<a href="faqcataae2.html?sec=style#stylewars">17.10</a>).Use any of<br><br><TT><pre>	#define TRUE  1	#define YES 1	#define FALSE 0	#define NO  0	enum bool {false, true};	enum bool {no, yes};</pre></TT><br><br>or use raw 1 and 0,as long as you are consistent within one program or project.(An enumeration may be preferable if your debuggershows the names of enumeration constantswhen examining variables.)</p><p>You may also want to use a <TT>typedef</TT>:<pre>	typedef int bool;or	typedef char bool;or	typedef enum {false, true} bool;</pre></p><p>Some people prefer variants like<pre>	#define TRUE (1==1)	#define FALSE (!TRUE)</pre>or define ``helper'' macros such as<pre>	#define Istrue(e) ((e) != 0)</pre>These don't buy anything (see question<a href="faqcate034.html?sec=bool#bool2">9.2</a>;see also questions<a href="faqcat1f1a.html?sec=null#nullptrmacro">5.12</a>and<a href="faqcat204f.html?sec=cpp#slm">10.2</a>).<hr><hr><hr><a name="bool2"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../bool/bool2.html"><!-- qtag -->Question 9.2</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Isn't #defining <TT>TRUE</TT> to be 1 dangerous, since any nonzero valueis considered ``true'' in C?What if a built-in logical or relational operator ``returns''something other than 1?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It istrue (sic)that any nonzero value is considered true in C,but this applies only ``on input'', i.e. where a Boolean value isexpected.When a Booleanvalue is generated by a built-inoperatorsuch as <TT>==</TT>, <TT>!=</TT>, and <TT>&lt;=</TT>,it is guaranteed to be 1 or 0.Therefore, the test<pre>	if((a == b) == TRUE)</pre>would work as expected (as long as <TT>TRUE</TT> is 1),but it is obviously silly.In fact,explicit tests against <TT>TRUE</TT>and<TT>FALSE</TT> aregenerallyinappropriate.In particular,andunlike the built-in operators,some library functions(notably <TT>isupper</TT>, <TT>isalpha</TT>, etc.)return,on success,a nonzero value which is<I>not</I>necessarily 1,so comparing their return valuesagainst a single value such as <TT>TRUE</TT>is quite riskyand likely not to work.</p><p>(Besides, if you believe that<pre>if((a&nbsp;==&nbsp;b)&nbsp;==&nbsp;TRUE)</pre>is an improvement over<pre>if(a&nbsp;==&nbsp;b)</pre>why stop there?Why not use:<pre>if(((a&nbsp;==&nbsp;b)&nbsp;==&nbsp;TRUE)&nbsp;==&nbsp;TRUE)</pre>or even:<pre>if((((a == b) == TRUE) == TRUE) == TRUE)</pre>See also Lewis Carroll's essay ``What the Tortoise Said to Achilles.'')</p><p>Given that<pre>	if(a == b)</pre>is a perfectly legitimate conditional,so is<pre>	#include &lt;ctype.h&gt;	...	if(isupper(c))		{ ... }</pre>since <TT>isupper</TT> is known to return zero/nonzero for false/true.Similarly,there should not be any reluctance to use code like<pre>	int is_vegetable;		/* really a bool */	...	if(is_vegetable)		{ ... }</pre>or<pre>	extern int fileexists(char *);	/* returns true/false */	...	if(fileexists(outfile))		{ ... }</pre>as long as <TT>is_vegetable</TT> and <TT>fileexists()</TT>are of ``conceptual Boolean type.''Alternatives like<pre>	if(is_vegetable == TRUE)or	if(fileexists(outfile) == YES)</pre>are not really any improvement.(They can be thought of as ``safer''or ``better style,''but they can also be thought of as risky or poor style.They certainly don't read as smoothly.See question <a href="faqcataae2.html?sec=style#stylewars">17.10</a>.)</p><p>A good rule of thumb isto use <TT>TRUE</TT>and <TT>FALSE</TT>(or the like)only for assignmentto a Boolean variableor function parameter,or as the return value from a Boolean function,butnever in a comparison.</p><p>See also question <a href="faqcat1f1a.html?sec=null#ptrtest">5.3</a>.</p><p>References:K&amp;R1 Sec. 2.6 p. 39, Sec. 2.7 p. 41<br>K&amp;R2 Sec. 2.6 p. 42, Sec. 2.7 p. 44, Sec. A7.4.7 p. 204, Sec. A7.9 p. 206<br>ISO Sec. 6.3.3.3, Sec. 6.3.8, Sec. 6.3.9, Sec. 6.3.13, Sec. 6.3.14, Sec. 6.3.15, Sec. 6.6.4.1, Sec. 6.6.5<br>H&amp;S Sec. 7.5.4 pp. 196-7, Sec. 7.6.4 pp. 207-8, Sec. 7.6.5 pp. 208-9, Sec. 7.7 pp. 217-8, Sec. 7.8 pp. 218-9, Sec. 8.5 pp. 238-9, Sec. 8.6 pp. 241-4<br>Lewis Carroll, ``What the Tortoise Said to Achilles''<hr><hr><hr><a name="ifptr"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../bool/ifptr.html"><!-- qtag -->Question 9.3</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Is <TT>if(p)</TT>,where <TT>p</TT> is a pointer,avalidand portable test?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Yes.See question <a href="faqcat1f1a.html?sec=null#ptrtest">5.3</a>.<hr><hr><hr><a name="macros"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../bool/macros.html"><!-- qtag -->Question 9.4</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Should I use symbolic names like <TT>TRUE</TT>and <TT>FALSE</TT> for Boolean constants,or plain 1 and 0?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It's your choice.Preprocessor macros like<TT>TRUE</TT> and <TT>FALSE</TT>(and, of course, <TT>NULL</TT>)areused for code readability, not because the underlying valuesmight ever change.It's a matter of style,not correctness,whether to use symbolic namesor raw 1/0 values.(See also questions<a href="faqcat1f1a.html?sec=null#macsochange">5.10</a>and <a href="faqcataae2.html?sec=style#stylewars">17.10</a>.)</p><p>On the one hand,using a symbolic name like <TT>TRUE</TT>or <TT>FALSE</TT> reminds the reader that a Boolean value is involved.On the other hand,Boolean values and definitions canevidentlybeconfusing,andsome programmers feel that <TT>TRUE</TT>and <TT>FALSE</TT> macros only compound the confusion.(See also question<a href="faqcat1f1a.html?sec=null#nullor0">5.9</a>.)<hr><hr><hr><a name="thirdparty"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../bool/thirdparty.html"><!-- qtag -->Question 9.5</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Athird-partyheader fileI just started usingis defining its own <TT>TRUE</TT>and <TT>FALSE</TT> valuesincompatibly withthe code I've already developed.What can I do?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat204f.html?sec=cpp#def3rdparty">10.10</a>.<hr><hr><hr><hr><p>Read sequentially:<a href="faqcate107.html?sec=charstring" rev=precedes>prev</a><a href="faqcat204f.html?sec=cpp" rel=precedes>next</a><a href="faqcat.html" rev=subdocument>up</a></p><hr><p><br><!-- lastfooter --><a href="../../about.html">about this FAQ list</a>&nbsp;<a href="../../eskimo.html">about eskimo</a>&nbsp;<a href="../../search.html">search</a>&nbsp;<a href="../../feedback.html">feedback</a>&nbsp;<a href="../../bool/copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=bool by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:57:58 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -