473,500 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Logical operators...

13 New Member
main( )
{
int i= -3, j=2, k=0, m;
m=++i | | ++j && ++k;
printf("\n%d%d%d%d",i,j,k,m);
}

I am expecting the output of this code as

-2,2,1,1

but the compiler is giving
-2,2,0,1
can anyone tell me Y it is so?
Aug 8 '06 #1
4 2423
Banfa
9,065 Recognized Expert Moderator Expert
Why are you expecting -2,2,1,1 ?

Anyway

m=++i | | ++j && ++k;

is equivilent to

m=++i | | (++j && ++k);

it evaluates i++ this is -2, logically this is equivilent to true

true || anything = true

therefore it doesn't bother evaluating (++j && ++k) as it already has the result of the expression.

This is a feature that is unique (and very useful) to the logical operators but does mean that you have to be careful about putting expressions that actually do something into if statements etc.
Aug 8 '06 #2
shinelakshmanan
13 New Member
Why are you expecting -2,2,1,1 ?

Anyway

m=++i | | ++j && ++k;

is equivilent to

m=++i | | (++j && ++k);

it evaluates i++ this is -2, logically this is equivilent to true

true || anything = true

therefore it doesn't bother evaluating (++j && ++k) as it already has the result of the expression.

This is a feature that is unique (and very useful) to the logical operators but does mean that you have to be careful about putting expressions that actually do something into if statements etc.

cant we treat,

m=++i | | ++j && ++k as
m=(++i | | ++j) && ++ k
as post increment operators are having left to right associativity.....
its not clear to me if u clear me that I will be grateful.....
Aug 9 '06 #3
shinelakshmanan
13 New Member
I got the point,
as && operator is having higher priority then the || operator compiler will look for first occurence of && and combine the left and right operator in the parantheses,
thats why,
m=++i | | ++j && ++k
will be treated as
m=++i || (++j&& ++k) and not as
m=(++i || ++j) && ++k
Thanks a lot.....
Aug 9 '06 #4
akshaycjoshi
153 New Member
Hello friends
We all know that () has the highest priority.consider the expression--
int x=y=z=-1;
z= ++x || ++y && ++z;
In this by writing as z= (++x || ++y) && ++z;
there is no effect on x,y,z.
Why .According to prcedence relationship (++x || ++y ) should be calculated first ??
Jan 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3772
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for...
34
2237
by: sam | last post by:
Please look at the code below #include <stdio.h> int expr(char str, int i){ printf("%s \n",str); return i; }
80
35019
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...
8
2406
by: Chua Wen Ching | last post by:
Hi, I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming? I am not sure why i need to use it? I had some samples of c# codes using it. ...
2
4099
by: ThunderMusic | last post by:
Hi, I have a value that contains flags that I must get using a bitmask. I tryied with the && operator, but the compiler outputs this error : Operator '&&' cannot be applied to operands of type...
6
2712
by: Protoman | last post by:
I'm writing a program to calc truth tables of arguments to prove that they are logically valid. Currently, I have to tell the program to print a truth table, and check it by hand to prove it's...
10
17996
by: Jonathan | last post by:
Hi all, I'm having an argument with a co-worker about the difference between the & and && operators when applied to boolean operands in C#. His point of view is that the expression (false &...
9
3725
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');...
7
1715
by: Charles Law | last post by:
This is a very basic question, but I can' turn up a statement on the subject: In C#, if I have If (x == 1 && y == 2) { .... }
19
1475
by: werdan | last post by:
G'day all I'm bitmapping integers to store combined boolean values and have hit a bit of a snag. I want to be able to turn off certain bits if they they match in a number but not turn them on...
0
7136
marktang
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,...
0
7018
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...
0
7232
jinu1996
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...
1
6906
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...
0
7397
tracyyun
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...
1
4923
isladogs
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...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.