Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what does a sequence of comparison operations in c represents/do?

i have been playing around with what would be considered valid C syntax and what wouldnt,
i tried 1<2<1 and to my surprise it is a valid C syntax, does anyone know what that would do or represent ?

i tried figuring it out by testing the value this kind of expressions take and changing the integer values but found no pattern.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In C, when using a relational operator such as <, the result will always be 1 if the condition is true, or 0 if the condition is false.

Due to the rules on operator precedence and associativity, the expression

1<2<1

is equivalent to:

(1<2)<1

The sub-expression (1<2) is true, so it will evaluate to 1, so the entire expression is equivalent to:

1<1

Since this expression is false, it will evaluate to 0.

Therefore, writing 1<2<1 is equivalent to writing 0.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading