-2

In C programming, the following code block return False

int a=15, b=10,c=5;
    if(a>b>c)
    {
        printf("True");
    }
    else
    {
        printf("False");
    }

But in Python, the following block return True.

a = 15
b = 10
c = 5
if a > b > c:
    print("True")
else:
    print("False")

Edit: Found a solution from link, it explain very well in python perspective. And in C programming perspective this answer explain my question very well.

shafik
  • 6,098
  • 5
  • 32
  • 50
  • 7
    C and Python books can explain. These are two different languages with different syntax and rules. – Eugene Sh. Sep 26 '18 at 18:01
  • 1
    Another format of the same question: why this snippet does not compile in assembly? – Sourav Ghosh Sep 26 '18 at 18:05
  • 1
    C and Python are *very* different languages, with very different rules. Do not assume that *any* operators behave the same way between the two languages. – John Bode Sep 26 '18 at 21:35

1 Answers1

6

They behave differently because that's how the languages are implemented.

With C, a > b > c is evaluated left to right. There is no concept of "operator chaining" (I'll get to this shortly), so the expression is evaluated as (a > b) > c which is 1 > c which is false (in C, logical expressions either return 1 or 0).

In python, OTOH, a > b > c evaluates to a > b and b > c (as specified in the documentation on comparisons), which turns out to be True in this case.

zvone
  • 18,045
  • 3
  • 49
  • 77
cs95
  • 379,657
  • 97
  • 704
  • 746