Hi Folks,
This question may well have been asked and answered before. But, sorry
that I couldn't find one from the archives.
I typed up this program and compiled it with gcc 3.3.2
main() {
int i = -3,j= 2,k=0,m;
m = ++i || ++j && ++k;
printf("\n%d %d %d %d\n",i,j,k,m);
}
and got the output to be
-2 2 0 1
I presumed that the answer should have well been
-3 3 1 1
as ++j and ++k would first be evaluated and && operator applied to
obtain the result to be TRUE (3 && 1). Hence, ++i wouldn't be evaluated
as one part of || goes TRUE.
The ANSI C specification clearly states that && has precedence over ||.
Could someone explain why this strange "-2 2 0 1" output for the program
is obtained?
Cheers
Vivek 20 8155
Vivek N wrote: Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives.
I typed up this program and compiled it with gcc 3.3.2 main() { int i = -3,j= 2,k=0,m;
m = ++i || ++j && ++k; printf("\n%d %d %d %d\n",i,j,k,m); } and got the output to be -2 2 0 1
I presumed that the answer should have well been -3 3 1 1
as ++j and ++k would first be evaluated and && operator applied to obtain the result to be TRUE (3 && 1). Hence, ++i wouldn't be evaluated as one part of || goes TRUE.
The ANSI C specification clearly states that && has precedence over ||.
(a || b && c), is the same as (a || (b && c)).
Could someone explain why this strange "-2 2 0 1" output for the program is obtained?
j and k are not evaluated at all.
The left operand of || is evaluted before the right.
The right operand of || is evaluated conditionally only if
the left operand is equal to zero.
Your left operand (++i), is not equal to zero.
Your right operand (++j && ++k) is not evaluated.
--
pete
Vivek N <ke***********@ yahoo.com> spoke thus: main() {
Allowing the return type of a function to default to int is illegal in
C99 and dubious otherwise.
int i = -3,j= 2,k=0,m;
m = ++i || ++j && ++k; printf("\n%d %d %d %d\n",i,j,k,m);
Where is <stdio.h>? Turn up gcc's warnings (-Wall -pedantic -ansi).
Could someone explain why this strange "-2 2 0 1" output for the program is obtained?
Not strange at all, thanks to short-circuit evaluation. The fact that
&& has precedence over || only means that the assignment to m is
equivalent to
m=++i || (++j && ++k);
++i is computed first, and if it is nonzero (true), then the entire
statement is guaranteed to be true regardless of what (++j && ++k) is.
In C, this means that (++j && ++k) will not be evaluated. That is
what short-circuit evaluation is, and that is why only i is
incremented in the code you posted.
--
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. ke***********@y ahoo.com (Vivek N) wrote: as ++j and ++k would first be evaluated and && operator applied to obtain the result to be TRUE (3 && 1). Hence, ++i wouldn't be evaluated as one part of || goes TRUE.
The ANSI C specification clearly states that && has precedence over ||.
The ISO C specification states no such thing, since "precedence " is
ambiguous.
What it _does_ state is this:
* To begin with, all logical operators are evaluated left-to-right, with
short-circuit evaluation. This means that it's possible to do this:
if (ptr!=NULL && *ptr!='A') ...
and be certain that your pointer is tested before being dereferenced.
* The logical and (&&) operator binds more closely than the logical or
(||). That is, x || y && z means x || (y && z), not (x || y) && z. The
order of evaluation remains the same, however.
Richard
Richard Bos wrote:
<snip>
Richard,
I think your clock is incorrectly set.
The original message is dated 14:34:18 GMT (1) and your reply is dated
13:56:05 GMT (2).
(1) Date: 19 Jan 2004 06:34:18 -0800
(2) Date: Mon, 19 Jan 2004 13:56:05 GMT
Grumble <in*****@kma.eu .org> wrote: Richard Bos wrote:
I think your clock is incorrectly set.
This is true (I've been looking for a gizmo that keeps it set to an
atomic clock, but I'm not paying $39.95 to some vague shareware firm for
it, so in the mean time I'm correcting it by hand every now and then)...
The original message is dated 14:34:18 GMT (1) and your reply is dated 13:56:05 GMT (2).
....but this doesn't necessarily indicate that. Usenet is weird.
Richard
"Vivek N" <ke***********@ yahoo.com> wrote in message
news:e7******** *************** *@posting.googl e.com... Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives.
I typed up this program and compiled it with gcc 3.3.2 main() { int i = -3,j= 2,k=0,m;
m = ++i || ++j && ++k; printf("\n%d %d %d %d\n",i,j,k,m); } and got the output to be -2 2 0 1
I presumed that the answer should have well been -3 3 1 1
as ++j and ++k would first be evaluated and && operator applied to obtain the result to be TRUE (3 && 1). Hence, ++i wouldn't be evaluated as one part of || goes TRUE.
The ANSI C specification clearly states that && has precedence over ||.
The precedence of an operator only defines where brackets are implicitely
placed to avoid abiguity. Thus a || b && c evaluates to a || (b && c)
rather than (a || b) && c. But after that the conditional statement is
executed left to right and will abort as soon as it is know that the
condition is false, which is why the ++j and ++k are not evaluated. I've
seen some compilers with an option to force all expressions in a conditional
statement to be evaluated so check your compiler if you really need this ..
Sean
In <bu**********@n ews-rocq.inria.fr> Grumble <in*****@kma.eu .org> writes: Richard,
I think your clock is incorrectly set.
The original message is dated 14:34:18 GMT (1) and your reply is dated 13:56:05 GMT (2).
(1) Date: 19 Jan 2004 06:34:18 -0800 (2) Date: Mon, 19 Jan 2004 13:56:05 GMT
How did you decide that the original message was correctly time stamped?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Dan Pop wrote: Grumble wrote:
I think your clock is incorrectly set.
The original message is dated 14:34:18 GMT (1) and your reply is dated 13:56:05 GMT (2).
(1) Date: 19 Jan 2004 06:34:18 -0800 (2) Date: Mon, 19 Jan 2004 13:56:05 GMT
How did you decide that the original message was correctly time stamped?
The original message showed up on my system when I downloaded new
messages at approximately 14:40:00 GMT.
Then Pete's and Christopher's messages showed up on my system when I
downloaded new messages at approximately 14:55:00 GMT.
When Richard's message showed up 5 minutes later, I figured it was
more probable that the first 3 were correctly time-stamped, and it
was Richard's clock which was incorrectly set.
Are you satisfied with my answer? :-)
On 19 Jan 2004, Vivek N wrote: m = ++i || ++j && ++k;
as ++j and ++k would first be evaluated and && operator applied to obtain the result to be TRUE (3 && 1). Hence, ++i wouldn't be evaluated as one part of || goes TRUE. The ANSI C specification clearly states that && has precedence over ||.
Your reasoning doesn't make sense as ++ has even higher precedence than &&. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: William |
last post by:
Original question:
"Give a one-line C expression to test whether a number
is a power of 2. "
Answer:
if (x && !(x & (x-1)) == 0)
My question:
Why does this expression work?
|
by: orekinbck |
last post by:
Hi There
Say I want to check if object1.Property1 is equal to a value, but
object1 could be null.
At the moment I have code like this:
if (object1 != null)
{
if (object1.Property == desiredValue)
|
by: Nathan Sokalski |
last post by:
I add a JavaScript event handler to some of my Webcontrols using the
Attributes.Add() method as follows:
Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"
TextBox2.Attributes.Add("onKeyPress", jscode)
You will notice that jscode contains the JavaScript Logical And operator
(&&). However, ASP.NET renders this as && in the code that is
|
by: marko |
last post by:
/* code start */
int a = 0;
/* expected evaluation and excution order with precedence in mind
/* False(3) , True(1), False(2) */
if ( (a=1) == 0 || 0 != 1 && (a =2) == 1)
putchar('T');
printf("%d", a);
/* code end */
2
|
by: find clausen |
last post by:
How can I use this:
if (!zxmes && self.name != "menu")
and add if (zmes == 1)
if (!zxmes && self.name != "menu" || zmes == 1)
and make it work.
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |