Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ut-sally!im4u!milano!mcc-pp!tiemann From: tiemann@mcc-pp.UUCP (DZ-015 @ Information Retrieval) Newsgroups: comp.lang.c Subject: Re: conditional expression evaluation question Message-ID: <2360@mcc-pp.UUCP> Date: Tue, 13-Jan-87 11:58:43 EST Article-I.D.: mcc-pp.2360 Posted: Tue Jan 13 11:58:43 1987 Date-Received: Wed, 14-Jan-87 00:38:02 EST References: <207@rebel.UUCP> Organization: MCC, Austin, TX Lines: 42 Summary: I wouldn't have answered, but... In article <207@rebel.UUCP>, george@rebel.UUCP (George M. Sipe) writes: > I need to check a string, composed of byte triples, for a null area no > less than MINSKIP triples in length. A pointer, cp, is initialized to > a triplet boundary. After the test, it must remain on a triplet > boundary. Initially, I wrote the following: > > while (cp < end && triples < MINSKIP) > if ((*cp++ | *cp++ | *cp++) == 0) ++triples; > else triples = 0; > > After looking at it, I wasn't absolutely sure that it would perform as > expected. My question is "Does C guarantee execution of portions of a > conditional expression, even when the result is known after partial > evaluation?". In my example, if the first byte is non-null, then the > result is known to be false and there is no need to evaluate the > remaining subexpressions. However, I am counting on the whole > expression being evaluated. Assuming that C does guarantee full > execution of the conditional test, would I be taking a significant > chance that an optimizing compiler might be too tricky and > (incorrectly) fail to do the full evaluation? > > For now, I am using the following ugly code: > > while (cp < end && triples < MINSKIP) { > if ((*cp | *(cp+1) | *(cp+2)) == 0) ++triples; > else triples = 0; > cp += 3; > } I wouldn't have answered, but there was a wrong answer to the net, so I thought I'd balance it out. First of all, the bit-wise OR operator is not the same thing as the logical disjuntion operator. The former does evaluate all its arguments (though it may permute them as it sees fit (just like + or *)), while the latter does not. The answer to your question is "No, C does not execute portions of a conditional expression which are not needed", but you have not specified a conditional expression, rather a logical one, which will be fully executed. Michael Tiemann tiemann@mcc.com