Posted inProgramming / Technology

How expression a = b == c (Multiple Comparison) evaluates in C programming?

Is a = b == c possible to write in c? – also in this Example learn to a==b==c (Multiple Comparison) evaluates in C programming.

Is a = b == c possible to write in c language?

Expression ‘A = B == C’ is calculated as ‘A = (B == C)’. Consider inspecting the expression.

  • Assignment operator (=) : It is used to assign the value to the left side variable.
  • Equal to operator (==) : It is used to compare two variables.

Since Main C programming language does not support chaining comparison like x==y==z; each equal to operator (==) operates on two operands only. Then how expression x==y==z evaluates?

According to some operators associativity equal to operator (==) operates from left to right, that means associativity of equal operator (==) is left to right.

Expression x==y==z is actually (x==y) ==z, see how expression (x==y) ==z evaluates?

  • (x==y) will be compared first as well as return either 1 (true) or 0 (false).
  • Then value of variable z will be compared with the result of (x==y).

Consider the following example

#include 
int main(){
	int x,y,z;
	x=y=z=100;
	
	if(x==y==z)
		printf("Great...\n");
	else	
		printf("Sorry...\n");
	
	return 0;	
}

Result

Sorry...

How result is “Sorry…”?

See the example, the values of x, y and z is 100 as well as you are thinking how condition is false here & why result is “False…”?

The expression is x==y==z which will evaluates like (x==y)==z now what will be the result?

  • The result of (x==y) is 1 (i.e. true).
  • And (1)==z will be 0 (i.e. false) because the value of z is 100 and 100 is equal not to 1.

Also Read : JavaScript Arrays & Objects Tips, Tricks And Examples

What does int i=(x,b,z) mean ?

==> x,y will return y 
==> y,z will return z 
==> means (x,y,z) will return z
==> and Last Assign z to variable i which will be actual result.

I hope you get an idea about a = b == c.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype