Reputation: 1
I tried running the following code in C:
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
I got the output as d = 1
. Can someone please explain to me what happens when we use ==
like this?
Upvotes: 0
Views: 61
Reputation: 113302
==
is the equal-to operator. It returns 1
if the two sides are equal and 0
otherwise.
Upvotes: 0
Reputation: 16607
§6.5.9 (== and !=)-http://c0x.coding-guidelines.com/6.5.9.html
The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.)Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.
So here as b+c
is equal to a
as both has value 10
therefore it yields 1
.
Upvotes: 1
Reputation: 317
In c, addition has higher precedence than ==
, so it adds b and c before comparing the result to a, since it is true it results in 1, if it was false it would result in 0.
Upvotes: 0
Reputation: 3147
Because b + c is executed first, and after is evaluate comparison with == operator.
Upvotes: 0