October 17th, 2013, 07:40 PM
Precedence/Assositivity between ! and !=
I want to know how the following expression will be resolved:
(!j!=1 ? printf(“\nWelcome”) : printf(“\nGood Bye”));
I thought it should do following way but it failed:
Step 1: ! (j!=1) It should compare the value of j (which is 4) to 1 which are not equal and it returns true i.e. 1
Step 2: tham it should check !(1) whch should be true i.e. 0
and it should print Good Bye.
But programs return Welcome.
So that means it should go from left to right. but than the question is
Step 1: (!j)!=1 :: what will be returned for !j as j is an integer as has a value of 1
Could you please help tounderstand this.
Thanks and regards.
October 17th, 2013, 07:46 PM
It evaluates as though you typed:
Look at C's operator precedence for details as to why this is so.
Since j is non-zero, !j evaluates to 0. Then it evaluates 0 != 1, which is true and hence, it prints "Welcome".
As an aside, if someone uses this in actual code (or even to teach code), feel free to slap them for me.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
October 19th, 2013, 12:16 AM
Thanks for your reply.
Ok would like to ask one more question on precedence.
how the following expression would be resolved:
int a = 500;
Method 1: Will it solve by this rule ((!a)>400) i.e.
Step 1: a=500 and !a will be 0
Step 2: than 0>500 which is false and returns 0
Method 2 !(a>400)
Step 1: 500>400 which is true
Step and !500 is 0 which is false and returns 0
Could you please help me to understand..
October 19th, 2013, 01:29 AM
From a table of precedence, look up ! and look up >. Which one has precedence over the other? That will give you your answer.