Please look at the code below
#include <stdio.h>
int expr(char str[], int i){
printf("%s \n",str);
return i;
}
int main()
{
if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1));
return 0;
}
output
-------
1st
As && has an higher precedence over ||
then it should call expr("2nd",0) or expr("3rd",0)
first then why it calls (expr("1st",1) first
Regards
Shiju 34 2250 sh**********@ho tmail.com (sam) wrote in
news:25******** *************** ***@posting.goo gle.com: Please look at the code below
#include <stdio.h>
int expr(char str[], int i){ printf("%s \n",str); return i; }
int main() { if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1)); return 0; }
output ------- 1st
As && has an higher precedence over || then it should call expr("2nd",0) or expr("3rd",0) first then why it calls (expr("1st",1) first
It's called a short-circuit. If expr("1st",1) is true, then there is no
need to bother evaluting expr("3rd",0) and then expr("2nd",0), so it does
not. What you have seen is correct.
--
- Mark ->
--
sam <sh**********@h otmail.com> spoke thus: int main() { if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1)); return 0; }
As && has an higher precedence over || then it should call expr("2nd",0) or expr("3rd",0) first then why it calls (expr("1st",1) first
Does this make sense?
* > +
A + B * C equals A + (B * C)
&& > ||
A || B && C equals A || (B && C)
As Mark stated, the last expression is subject to short-circuit
evaluation, and thus there is no need to evaluate B && C (since A is
true in this case). Notice that
B && C || A
in this case calls #2 and #1, but not #3, again because of
short-circuit evaluation.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
sam wrote: Please look at the code below
#include <stdio.h>
int expr(char str[], int i){ printf("%s \n",str); return i; }
int main() { if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1)); return 0; }
output ------- 1st
As && has an higher precedence over || then it should call expr("2nd",0) or expr("3rd",0) first then why it calls (expr("1st",1) first
"Operator precedence" and "order of evaluation" are
two different things. Precedence dictates that the
expression means
expr("1st",1) || ( expr("2nd",0) && expr("3rd",0) )
rather than
( expr("1st",1) || expr("2nd",0) ) && expr("3rd",0)
.... but precedence alone doesn't determine the order in
which the three expr() calls are made.
The evaluation order is determined not by the precedence,
but by the definitions of the || and && operators. In this
case, the rule for || says that if expr("1st",1) produces a
non-zero value, the second sub-expression is not evaluated
at all. If the expr("1st",1) yields zero, the second sub-
expression *is* evaluated -- and in that evaluation, there
is a similar rule for && that governs the order in which
expr("2nd",0) and expr("3rd",0) are evaluated.
Summary: Operator precedence governs the meaning of an
expression with multiple operators, but does not control
the order in which the operands are evaluated.
-- Er*********@sun .com
Mark A. Odell writes: Please look at the code below
#include <stdio.h>
int expr(char str[], int i){ printf("%s \n",str); return i; }
int main() { if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1)); return 0; }
output ------- 1st
As && has an higher precedence over || then it should call expr("2nd",0) or expr("3rd",0) first then why it calls (expr("1st",1) first
It's called a short-circuit. If expr("1st",1) is true, then there is no need to bother evaluting expr("3rd",0) and then expr("2nd",0), so it does not. What you have seen is correct.
This is nasty stuff! Once prompted, I remember the short circuit rule and
how I once wanted such a rule in Pascal. But digging this rule out of the
BNF for C seems like a real challenge. However I would expect it to be
noticeable in syntax charts. Does any one know of a site containing syntax
charts for C? I looked and failed to find one. I know there is a book, I
saw one several years ago, but I didn't like it.
"osmium" <r1********@com cast.net> wrote in
news:bu******** ****@ID-179017.news.uni-berlin.de: > int main() > { > if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1)); > return 0; > } > > output > ------- > 1st > > As && has an higher precedence over || > then it should call expr("2nd",0) or expr("3rd",0) > first then why it calls (expr("1st",1) first
It's called a short-circuit. If expr("1st",1) is true, then there is no need to bother evaluting expr("3rd",0) and then expr("2nd",0), so it does not. What you have seen is correct.
This is nasty stuff!
Nasty nothing. It's dog simple.
if (a || b)
Why on earth would you ever evaluate b if a is true? Very simple. I
believe we need to think about sequence points here.
--
- Mark ->
--
osmium wrote: Mark A. Odell writes:
It's called a short-circuit. If expr("1st",1) is true, then there is no need to bother evaluting expr("3rd",0) and then expr("2nd",0), so it does not. What you have seen is correct. This is nasty stuff! Once prompted, I remember the short circuit rule and how I once wanted such a rule in Pascal. But digging this rule out of the BNF for C seems like a real challenge.
A challenge, indeed, because the short-circuit rule is
not present in the BNF in the first place.
However I would expect it to be noticeable in syntax charts.
Why? The syntax will tell you what arrangements of
symbols are valid C utterances, but will say nothing about
what those utterances mean (if, indeed, they mean anything
at all).
-- Er*********@sun .com
Mark A. Odell wrote: "osmium" <r1********@com cast.net> wrote in
[...snip...] Nasty nothing. It's dog simple.
if (a || b)
Why on earth would you ever evaluate b if a is true?
If this wasn't a rhetorical question, I /could/ think of an answer. For
example: short-circuit evaluation may, under some circumstances,
negatively impact the size and/or speed of generated assembly code
(e.g., by introducing pipeline stalls).
Best regards, Sidney
Eric Sosman wrote:
(snip) if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1));
(snip)
... but precedence alone doesn't determine the order in which the three expr() calls are made.
The evaluation order is determined not by the precedence, but by the definitions of the || and && operators. In this case, the rule for || says that if expr("1st",1) produces a non-zero value, the second sub-expression is not evaluated at all. If the expr("1st",1) yields zero, the second sub- expression *is* evaluated -- and in that evaluation, there is a similar rule for && that governs the order in which expr("2nd",0) and expr("3rd",0) are evaluated.
Summary: Operator precedence governs the meaning of an expression with multiple operators, but does not control the order in which the operands are evaluated.
Funny. There is a very similar discussion in comp.lang.fortr an,
except that Fortran does not guarantee short circuit evaluation.
The discussion of precedence and evaluation order is there, though.
-- glen
Sidney Cadot wrote: Mark A. Odell wrote:
"osmium" <r1********@com cast.net> wrote in
[...snip...] Nasty nothing. It's dog simple.
if (a || b)
Why on earth would you ever evaluate b if a is true?
If this wasn't a rhetorical question, I /could/ think of an answer. For example: short-circuit evaluation may, under some circumstances, negatively impact the size and/or speed of generated assembly code (e.g., by introducing pipeline stalls).
You're missing the point, I think. Short-circuit
evaluation isn't about efficiency, but about correctness.
The || and && operators are *defined* to work this way;
an implementation that evaluated `b' when `a' was true
would not be an implementation of C.
-- Er*********@sun .com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Hari Om |
last post by:
Here are the details of my error log files:
I execute the command and get following message at console:
----------------------------------------------------------------------
../sqlldr scott/tiger@common control=/full_path/test.ctl
log=/full_path/adhoc/test.log
SQL*Loader: Release 9.2.0.1.0 - Production on Tue Sep 2 10:49:27 2003
...
|
by: Charles Crume |
last post by:
Hello all;
I have used dBASE, and other computer languages/databases, for years. They
all have a logical field type. However, the version of MySQL used by the ISP
hosting my site does not support a "logical" field type.
It does support ENUM and I have set some up in a couple of tables that
accept the values 'T' and 'F'. Sometimes they...
|
by: Christopher Benson-Manica |
last post by:
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one who
would find it useful?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I...
|
by: serge |
last post by:
How do I determine which method I should use if
I want to optimize the performance of a database.
I took Northwind's database to run my example.
My query is I want to retrieve the Employees' First
and Last Names that sold between $100,000 and
$200,000.
First let me create a function that takes the EmployeeID
|
by: serge |
last post by:
I am running a query in SQL 2000 SP4, Windows 2000 Server
that is not being shared with any other users or any sql
connections users. The db involves a lot of tables,
JOINs, LEFT JOINs, UNIONS etc... Ok it's not a pretty
code and my job is to make it better.
But for now one thing I would like to understand with your
help is why the same SP...
| |
by: blueboy |
last post by:
Hi,
I am planning to automate a nighty restore of a DB on another server
can someone point me in the right direction with the SQL script to
modify the logical file names to the correct path and not the ones
carried over with the DB??
i.e the database is to be renamed on the new server
any help much appreciated
|
by: dbtwo |
last post by:
Until today I always thought as long as you see a lot of logical reads
as compared to physical reads, then you're good. But it looks like it
isn't so. But doesn't logical read mean it's being read from memory
and no I/O involved? So why is Logical Reads = CPU Consumption ?
I ran into an exact scenario last week when our applciation were...
|
by: ags5406 |
last post by:
Hi --
I posted this in a Fortran group but thought I'd post here as well.
Any help is appreciated.
I have a IVF10 DLL that is the calculation engine for a frontend
application written in VB.NET 2005.
I've noticed that when I pass a Boolean/Logical from VB.NET to IVF10
as part of an argument list, when I try to evaluate it it only
|
by: Dominic Vella |
last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical
type fields. I start with
Dim dbsTmp As Object ' I think it's DAO.Database
Set dbsTmp = DBEngine.OpenDatabase(CurrentProject.path & "\data_be.mdb")
and then use the following to run my SQL ststement
dbsTmp.Execute...
|
by: raylopez99 |
last post by:
I have a logical drawing space much bigger than the viewport (the
screen) and I'd like to center the viewport (the screen) to be at the
center of the logical drawing space. After following the excellent
transforms specified on Bob Powell's site, I still wonder if there's
an easier way of centering it than the following procedure?
Here is...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |