MCG
MCG

Reputation: 1031

How below expression will be evaluated in C?

I was debugging a C code and came across below expression. I want to know, how below expression will be evaluated in c?

x += y*2 != z;

Upvotes: 1

Views: 51

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

To figure out expressions like that start with the precedence table:

  • Multiplication has precedence of 3, so it will be evaluated first
  • != has precedence of 7, so it will be evaluated next
  • += has precedence of 14, so it will be evaluated last.

Therefore, x will be incremented by 1 if y*2 is not equal to z.

Upvotes: 2

ergonaut
ergonaut

Reputation: 7057

It should be

x += ((y*2) != z);

Upvotes: 1

Related Questions