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

📄 bool2.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/bool/bool2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:53 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 9.2</title><link href="booltype.html" rev=precedes><link href="ifptr.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="booltype.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="ifptr.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a>&nbsp;<a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><!-- qtag -->Question 9.2</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="../style/stylewars.html">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="../null/ptrtest.html">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''<br></p><!-- aend --><p><hr><a href="booltype.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="ifptr.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a>&nbsp;<a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><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="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/bool/bool2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:53 GMT --></html>

⌨️ 快捷键说明

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