Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!eecae!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: value of TRUE??? Keywords: TRUE, if(), another nonzero value Message-ID: <16248@mimsy.UUCP> Date: 7 Mar 89 04:30:19 GMT References: <987@infmx.UUCP> Distribution: usa Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 63 In article <987@infmx.UUCP> kevinf@infmx.UUCP (Kevin Franden) writes: >Hi, I hope someone out there in netland can decide a bet >I have with a colleague. I am kinda new to this newsgroup >and know that there are alot of C gurus (guri?) out there that >can handle this. A good C tutorial can also handle it, and is likely to provide you with a single correct answer, rather than several conflicting ones. It would take more work on your part to locate and read one; but that would have the side effect of giving you quite a bit more information (although I would like to think that my presentation is generally more accurate and/or entertaining than theirs :-) ). Anyway: >given: :The if() statement will evaluate to true provided that > the argument does not evaluate to 0. (ie a=3; if (a)...) The *statement* does not produce a value. The *expression* used as an argument to `if' will produce the value (assuming `a' is an int), and control will move to the `true' section of the `if'. >Is then the value of true any nonzero integer? >If it's not, is it equivelent to a nonzero integer? These questions are ill-formed. C's flow-control statements (if, while, do/while, and for) require as their conditional expression any expression returning an arithmetic or pointer type. The value produced by that expression is then compared against zero (the zero having been converted to the appropriate type), and if not equal to zero, the `true' branch is taken (in the loops, iteration continues), otherwise the `false' branch is taken (in the loops, iteration stops). No value is returned (not even one of type void). C's boolean negation operator `!' requires as its operand the same sort of expression. The value is compared (with conversion as above) against zero; if not zero, the result is (`false'); otherwise the result is (`true'). (Thus, ! converts not-0 to 0 and 0 to 1.) C's logical operators <, <=, >, >=, ==, !=, &&, and || work in the obvious fashion and return either (representing `true') or (representing `false'). && and || further guarantee that the left operand is fully evaluated before the right operand is begun, and that the evaluation then stops if the result is known (0 for &&, 1 for ||). Thus, in one respect, the value of `true' is . No other value is produced for true conditional expressions. From another point of view, however, the value of `true' is any non-zero arithmetic expression or any non-nil pointer expression. In as few words as possible: any nonzero value is taken as true; true is given as 1. >What does if (a=3) evaluate to? It assigns 3 to a and executes the `true' half of the control flow. It does not produce a value. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris