### Thread: Precedence/Assositivity between ! and !=

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
4
Rep Power
0

#### Precedence/Assositivity between ! and !=

Hi All,

I want to know how the following expression will be resolved:

int j=4;
(!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

Thanks and regards.
2. It evaluates as though you typed:
(!j)!=1
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.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
4
Rep Power
0

Ok would like to ask one more question on precedence.

how the following expression would be resolved:

int a = 500;
if(!a>400)

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

or by
Method 2 !(a>400)
Step 1: 500>400 which is true
Step and !500 is 0 which is false and returns 0