Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > short circuit evaluation

Reply
Thread Tools

short circuit evaluation

 
 
Keith Thompson
Guest
Posts: n/a
 
      09-16-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> (E-Mail Removed) wrote:
>> On Sep 16, 8:55�pm, "Lassie" <(E-Mail Removed)> wrote:
>> > bool finished = is_done();
>> >
>> > if (finished && try_again()) { }
>> >
>> > Is my understanding of short circuit evaluation correct that if
>> > finished is false that try_again() will never be executed?
>> > Or does it get evaluated sometimes?

>>
>> no it will not be evaluated ever,
>> but are you trying it in "C".
>> In "C" there is no "bool" built in data type .

>
> No, but it is a standard typedef. This is just a code fragment, which
> doesn't even provide declarations for is_done() and try_again(). I
> would assume that the missing code also contains a line which says
>
> #include <stdbool.h>


Either that, or it was compiled with a C++ compiler.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-16-2008
(E-Mail Removed) (Kenny McCormack) writes:

> && means "evaluate while true[*]; return last evaluated expression"
> || means "evaluate until true[*]; return last evaluated expression"


That is not completely correct. The result of && is always 0 or
1. It is not, say, 2, even if the last evaluated subexpression
evaluates to 2.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      09-16-2008
In article <(E-Mail Removed)>,
Ben Pfaff <(E-Mail Removed)> wrote:
>(E-Mail Removed) (Kenny McCormack) writes:
>
>> && means "evaluate while true[*]; return last evaluated expression"
>> || means "evaluate until true[*]; return last evaluated expression"

>
>That is not completely correct. The result of && is always 0 or
>1. It is not, say, 2, even if the last evaluated subexpression
>evaluates to 2.


That's true. I could have said "return the truth value of the last
evaluated expression" - but I wanted to fit it into an 80 character
line.

Of course, the phrase "truth value" probably doesn't occur in the holy
standards documents, so, no doubt, somebody will come along and nit pick
that.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-16-2008
(E-Mail Removed) (Kenny McCormack) writes:

> In article <(E-Mail Removed)>,
> Ben Pfaff <(E-Mail Removed)> wrote:
>>(E-Mail Removed) (Kenny McCormack) writes:
>>
>>> && means "evaluate while true[*]; return last evaluated expression"
>>> || means "evaluate until true[*]; return last evaluated expression"

>>
>>That is not completely correct. The result of && is always 0 or
>>1. It is not, say, 2, even if the last evaluated subexpression
>>evaluates to 2.

>
> That's true. I could have said "return the truth value of the last
> evaluated expression" - but I wanted to fit it into an 80 character
> line.


I suppose you could say "return !!(last evaluated expression)" or
"return (last evaluated expression) != 0". Either way, I guess
that it is not necessarily clear to anyone who doesn't already
understand what's going on.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-16-2008
(E-Mail Removed) wrote:
> "Lassie" <(E-Mail Removed)> wrote:
>
>> bool finished = is_done();
>>
>> if (finished && try_again()) { }
>>
>> Is my understanding of short circuit evaluation correct that if
>> finished is false that try_again() will never be executed?
>> Or does it get evaluated sometimes?

>
> no it will not be evaluated ever, but are you trying it in "C".
> In "C" there is no "bool" built in data type. If you type casted
> "bool" as some other built in data type then please post as much
> relevant code as possible in queries in future


Wrong. It is available in C99, provided you #include <stdbool.h>.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      09-16-2008
CBFalconer <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > In "C" there is no "bool" built in data type. ...

>
> Wrong. *It is available in C99, provided you #include
> <stdbool.h>.


Wrong. With or without <stdbool.h>, bool is not a built
in data type in C99.

#include <stdio.h>
#include <stdbool.h>

#undef bool

int main(void)
{
int bool = 42;
printf("%d\n", bool);
return 0;
}

--
Peter
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-16-2008
Peter Nilsson <(E-Mail Removed)> writes:

> CBFalconer <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > In "C" there is no "bool" built in data type. ...

>>
>> Wrong. *It is available in C99, provided you #include
>> <stdbool.h>.

>
> Wrong. With or without <stdbool.h>, bool is not a built
> in data type in C99.
>
> #include <stdio.h>
> #include <stdbool.h>
>
> #undef bool
>
> int main(void)
> {
> int bool = 42;
> printf("%d\n", bool);
> return 0;
> }


I'm not quite sure what your example is supposed to prove but my gcc
stdbool includes this comment:

/*
* ISO C Standard: 7.16 Boolean type and values <stdbool.h>
*/

And it goes on to define bool.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-17-2008
blargg <(E-Mail Removed)> writes:

> In article <gaopl6$ek$(E-Mail Removed)>,
> (E-Mail Removed) (Kenny McCormack) wrote:
>
>> In article <(E-Mail Removed)>,
>> Ben Pfaff <(E-Mail Removed)> wrote:
>> >(E-Mail Removed) (Kenny McCormack) writes:
>> >
>> >> && means "evaluate while true[*]; return last evaluated expression"
>> >> || means "evaluate until true[*]; return last evaluated expression"
>> >
>> >That is not completely correct. The result of && is always 0 or
>> >1. It is not, say, 2, even if the last evaluated subexpression
>> >evaluates to 2.

>>
>> That's true. I could have said "return the truth value of the last
>> evaluated expression" - but I wanted to fit it into an 80 character
>> line.

>
> How about this?
>
> a && b means a ? !!b : 0
> a || b means a ? 1 : !!b


Yes, and it even has the sequence points in the right places. But (it
would not be clc without a "but" would it?) it involves even more to
learn, so it is not ideal as an explanation.

The standard gets knocked (by some) as wordy and obscure but, it does
very well here:

The && operator shall yield 1 if both of its operands compare
unequal to 0; otherwise, it yields 0. The result has type int.

Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation; there is a sequence point after the
evaluation of the first operand. If the first operand compares equal
to 0, the second operand is not evaluated.

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-17-2008
pete <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> && is not a statement, it's an operator, and it can be part of an
>> expression. Since it's not a function it doesn't "return"
>> anything; expressions *yield* values (though it's common to say that
>> expressions return values, and it usually doesn't cause too much
>> confusion).

>
> You can find this phrase: "These operators return values",
> in the standard.


Yes, in C99 6.5p4.

This is changed to "These operators yield values" in both n1124
(C99+TC1+TC2) and n1256 (C99+TC1+TC2+TC3).

n1336.pdf, the very early first draft of C201x, also uses "yield".

Oddly, I can't find this change either in any of the Technical
Corrigenda or in any of the Defect Reports.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-17-2008
Richard wrote:
> Peter Nilsson <(E-Mail Removed)> writes:
>
>> CBFalconer <(E-Mail Removed)> wrote:
>>> (E-Mail Removed) wrote:
>>>> In "C" there is no "bool" built in data type. ...
>>>
>>> Wrong. It is available in C99, provided you #include
>>> <stdbool.h>.

>>
>> Wrong. With or without <stdbool.h>, bool is not a built
>> in data type in C99.
>>
>> #include <stdio.h>
>> #include <stdbool.h>
>>
>> #undef bool
>>
>> int main(void)
>> {
>> int bool = 42;
>> printf("%d\n", bool);
>> return 0;
>> }

>
> I'm not quite sure what your example is supposed to prove but my gcc
> stdbool includes this comment:
>
> /*
> * ISO C Standard: 7.16 Boolean type and values <stdbool.h>
> */
>
> And it goes on to define bool.


Like this:
#define bool _Bool

So it is true: C does not have a builtin type bool, C99 does have a builtin
type _Bool. C99 does have a macro bool in <stdbool.h>.

From n1256
6.2.5.2:
An onject declared as type _Bool is large enough to store the values 0 and
1.

6.3.1.1:
The rank of _Bool shall be less than the rank of all other standard integer
types.

6.7.2.1 and .2, "type specifiers" mentions _Bool, but not bool

7.16.2:
The macro bool expands to _Bool.

Bye, Jojo


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
logical expressions simplification with short circuit evaluation mingze zhang C Programming 4 10-22-2011 09:51 AM
logic expressions & short circuit evaluation mingze zhang C++ 2 07-15-2010 01:56 PM
short-circuit evaluation and assignment operators Anthony Paul C Programming 5 06-06-2009 06:49 PM
Short-Circuit Evaluation of Boolean Expressions webposter C Programming 2 09-14-2004 03:43 AM
short-circuit operators - part of the spec? Phil... Java 10 10-23-2003 08:42 PM



Advertisments