简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM