ipkiss
ipkiss

Reputation: 13651

Question regarding Relational Operator in C?

I am learning C language and I have some question as follows (sorry if these are silly ones)

I am using Dev-C++ 4.9.9.2 to run some examples:

int m=3, n=4, k = 2;
(1) printf("%d", k<m<n); => this one prints 1
(2) printf("%d", k>m>n); => this one prints 0
(3) printf("%d", m<n>k); => this one prints 0

As the book says "A zero value stands for false and any other value stands for true." So, why the statement (3) prints 0 (false). I thought it should be 1, or what am i missing here?

Can anyone give me a clear explanation, please?

Thanks a lot.

Upvotes: 1

Views: 131

Answers (1)

yan
yan

Reputation: 20982

According to C's precedence rules, m<n>k gets interpreted as (m<n)>k (your other examples follow the same form). m<n is true, so that evaluates to 1. Then the statement is actually 1>k which is false, thus 0.

Upvotes: 8

Related Questions