'Why the output of the following code is "No"? [duplicate]
I have done this code in my VS-code using Mingw GCC compiler and I got the output "No" instead of "Yes"? It is clear that 5>4>3, then what is the reason behind it?
#include <stdio.h>
int main()
{
int a = 4;
if (5 > a > 3)
printf("Yes");
else
printf("No");
return 0;
}
Expected Output: Yes
Original Output: No
Solution 1:[1]
(5 > a > 3) does not mean what you think it means. It translates to (5>a) > 3, and (5>a) evaluates to 1. Then, 1>3 evaluates to false, so you get "No".
To do what you want, you do if ( (5 > a) && (a > 3) ).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andy Lester |
