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?
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.
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.....
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.....
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 ??
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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;
}
|
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...
|
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.
...
|
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...
| |
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...
|
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 &...
|
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');...
|
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)
{
....
}
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |