kevinf@infmx.UUCP (Kevin Franden) (03/07/89)
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. given: :The if() statement will evaluate to true provided that the argument does not evaluate to 0. (ie a=3; if (a)...) Is then the value of true any nonzero integer? If it's not, is it equivelent to a nonzero integer? What does if (a=3) evaluate to? Thanks for the reply. Kevin Franden Informix Software Inc. disclaimer("I said what I said and NOT my employer");
chris@mimsy.UUCP (Chris Torek) (03/07/89)
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 <int,3> (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 <int,0> (`false'); otherwise the result is <int,1> (`true'). (Thus, ! converts not-0 to 0 and 0 to 1.) C's logical operators <, <=, >, >=, ==, !=, &&, and || work in the obvious fashion and return either <int,1> (representing `true') or <int,0> (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 <int,1>. 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
derek@hsi.UUCP (Derek Lee-Wo) (03/08/89)
In article <987@infmx.UUCP> kevinf@infmx.UUCP (Kevin Franden) writes: : :given: :The if() statement will evaluate to true provided that : the argument does not evaluate to 0. (ie a=3; if (a)...) : :Is then the value of true any nonzero integer? :If it's not, is it equivelent to a nonzero integer? : :What does if (a=3) evaluate to? I believe that would evaluate to true. -- +-----------------------------------------------------------------------------+ |Derek Lee-Wo, Health Systems International, New Haven, CT 06511. | |E-mail address :- derek@hsi.com ...!yale!hsi!derek | +-----------------------------------------------------------------------------+
w-colinp@microsoft.UUCP (Colin Plumb) (03/08/89)
Sigh, this went around a few short weeks ago... When a C operator returns a truth value (these operators are ==, !=, <, > , <=, >=, !, &&, || and defined()), the result is of type int and has the value 0 (for false) or 1 (for true). When a C operator takes a truth value (!, &&, ||, ? :, if(), if() else, while(), do while(), and for(;;), and #if), the value must be comparable to 0 (i.e. int, float, pointer, etc.) and if it is non-zero, it is considered to be true. An if() statement does not return a value, so I don't quite understand that part of the original question. if(i=3)... is equivalent to i=3; if(i)... (assuming i isn't a macro). If i is 2 bits or wider, this is equivalent to i=3; if(3)... which is equivalent to i=3; if(1)... (or if(0.0001)... or if(&i)... or if(-1)...). -- -Colin (uunet!microsoft!w-colinp) "Don't listen to me. I never do." - The Doctor
ftw@masscomp.UUCP (Farrell Woods) (03/08/89)
In article <987@infmx.UUCP> kevinf@infmx.UUCP (Kevin Franden) writes: >given: :The if() statement will evaluate to true provided that > the argument does not evaluate to 0. (ie a=3; if (a)...) Expressions have values in C. Since there is no boolean type in C, expressions that evaluate to non-zero values are taken as "true", and zero-valued as "false". >What does if (a=3) evaluate to? The expression "a = 3" has the value of 3, which is non-zero and therefore "true". Therefore: if (a = 3) printf("true!\n"); will always execute the printf(). -- Farrell T. Woods Voice: (508) 392-2471 Concurrent Computer Corporation Domain: ftw@masscomp.com 1 Technology Way uucp: {backbones}!masscomp!ftw Westford, MA 01886 OS/2: Half an operating system
karl@haddock.ima.isc.com (Karl Heuer) (03/08/89)
In article <16248@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: >[The operators] !, <, <=, >, >=, ==, !=, &&, and || ... return either <int,1> >(representing `true') or <int,0> (representing `false'). Thus, in [this] >respect, the value of `true' is <int,1>. No other value is produced for true >conditional expressions. This is true for the operators, as you say. It should probably be mentioned that it is not necessarily true for functions, even those standard library functions that are conceptually boolean: for example, isdigit('6') returns 4 on this system. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/08/89)
In article <987@infmx.UUCP> kevinf@infmx.UUCP (Kevin Franden) writes: >given: :The if() statement will evaluate to true provided that > the argument does not evaluate to 0. (ie a=3; if (a)...) >Is then the value of true any nonzero integer? >If it's not, is it equivelent to a nonzero integer? >What does if (a=3) evaluate to? You're adding to your own confusion by trying to bundle too much together. The C language itself has no official "true" value, so trying to decide what that value "is" is inherently misleading. First of all, the "if" STATEMENT does not have a value. You cannot say a = if(x)y;else z; (Some Algol-like languages would support this, but C doesn't; it has a ?: operator instead.) Next, the value of what would appear to be a Boolean or relational expression is either 0 or 1, and it's an arithmetic expression in C. Third, the condition used for if(), while(), etc. is any arithmetic expression, not just those that appear to be Boolean. The condition is taken to be "true" if it has any nonzero value. Thus, if(a=3) assigns the value 3 to the variable a, and then, since the assignment expression has the value 3 which is nonzero, the "true" branch of the "if" statement is taken. The assignment statement i = j > 2; will give the variable i the value 0 unless the value of j exceeds 2, in which case i will be assigned the value 1. I don't recommend mixing conceptually arithmetic and Boolean operations even though the C language is designed to allow it.
evil@arcturus.UUCP (Wade Guthrie) (03/09/89)
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. Strictly no sweatski -- what kinda bux are we talking about here? > given: :The if() statement will evaluate to true provided that > the argument does not evaluate to 0. (ie a=3; if (a)...) So far, so good. . . > Is then the value of true any nonzero integer? Strictly speaking, yes. (see recent net flamewarz) > What does if (a=3) evaluate to? Well, a=3 is an expression (i.e., it evaluates to a value). In the event of 'a' being declared as an integer variable (I don't want to get into the implications if it's not), then it is an *integer* expression. Assigning a value to a variable causes the expression to evaluate to the value being assigned to the variable -- so, the expression a=3 evaluates to 3. Hence: if(a=3) { /* this will always be executed */ . . . } else { /* this will never be executed */ . . . } Wade Guthrie evil@arcturus.UUCP Rockwell International Anaheim, CA (Rockwell doesn't necessarily believe / stand by what I'm saying; how could they when *I* don't even know what I'm talking about???)
jcbst3@cisunx.UUCP (James C. Benz) (03/10/89)
In article <987@infmx.UUCP> kevinf@infmx.UUCP (Kevin Franden) writes: >What does if (a=3) evaluate to? if (a=3) will evaluate to true, since the value of (a=3) is 3, a non-zero value Now, if you had said, if (a == 3), it is highly dependent on the value of 3. The values of TRUE and FALSE are 1 and 0, and are defined as constants in I think, the stdio library. -- Jim Benz jcbst3@unix.cis.pittsburgh.edu If a modem University of Pittsburgh answers, UCIR (412) 648-5930 hang up!
bobmon@iuvax.cs.indiana.edu (RAMontante) (03/10/89)
jcbst3@unix.cis.pittsburgh.edu (James C. Benz) <16575@cisunx.UUCP> : - -if (a=3) will evaluate to true, since the value of (a=3) is 3, a non-zero value -Now, if you had said, if (a == 3), it is highly dependent on the value of 3. How TRUE, how TRUE.... I once knew a mathematician (okay, a statistician) who was quite disturbed by the limit of "pi/2" for small values of 2.
jcbst3@cisunx.UUCP (James C. Benz) (03/10/89)
In article <18427@iuvax.cs.indiana.edu> bobmon@iuvax.cs.indiana.edu (RAMontante) writes: >jcbst3@unix.cis.pittsburgh.edu (James C. Benz) <16575@cisunx.UUCP> : >- >-if (a=3) will evaluate to true, since the value of (a=3) is 3, a non-zero value >-Now, if you had said, if (a == 3), it is highly dependent on the value of 3. > >How TRUE, how TRUE.... I once knew a mathematician (okay, a statistician) >who was quite disturbed by the limit of "pi/2" for small values of 2. OOPS! How'd that get in there? Meant to say "a", not "3" - just another example of brain-damage from too much sillycon in my diet. Got to stop eating so many chips. -- Jim Benz jcbst3@unix.cis.pittsburgh.edu If a modem University of Pittsburgh answers, UCIR (412) 648-5930 hang up!