Why this program is printing 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12
3rd May 2019, 7:36 AM
Yamin Mansuri
Yamin Mansuri - avatar
12 Answers
+ 8
int x=10, y=20, z=5, i; i=x<y<z = i=(x<y)<z; 👇 1. 10<20 = true, (true = 1); 2. 1<5 = true; 3. i = true, i = 1;
3rd May 2019, 7:48 AM
Solo
Solo - avatar
+ 10
Tanay Bhatt but bro if y<z is false then it should print 0. Because 0 is false and 1 is true. I think so...
3rd May 2019, 7:41 AM
Yamin Mansuri
Yamin Mansuri - avatar
+ 7
Given condition is true In C program for boolean conditions If true it will return the value as 1 Else false it will return 0 /*This will vary on different languages*/ So if you print the i in %d formatter it shows 1
3rd May 2019, 1:40 PM
Arvind Raj
Arvind Raj - avatar
+ 7
The precedence of the operators and associativity plays a role here
3rd May 2019, 1:44 PM
Arvind Raj
Arvind Raj - avatar
+ 6
Vasiliy Thank you very much😄
3rd May 2019, 9:17 AM
Yamin Mansuri
Yamin Mansuri - avatar
+ 5
x<y = true = 1: 10< 20 1<5=true=1 i=1 😎
3rd May 2019, 2:50 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 5
x< y = 10 < 20 ? Yes , true = 1 1 < 5 ? Yes , i = true = 1
4th May 2019, 8:56 AM
Yousef Eabitan
Yousef Eabitan - avatar
+ 4
Bcz it a condition so check and ans is true so 1
3rd May 2019, 7:59 PM
Abhishek Bakhai
Abhishek Bakhai - avatar
+ 3
You are comparing nos. int x =10,y=20,z=5,i i=(x<y)<z //(10<20) it is true and returning 1, in the next part 1<5//result of (x<y)<z =1 so that's why you got 1
4th May 2019, 3:01 AM
vijay
vijay - avatar
+ 2
It is a condition and 1 is true
3rd May 2019, 10:11 PM
Alonge Renfred
Alonge Renfred - avatar
+ 1
Theres no bracket in x<y<z, and what Tanay Bhatt is saying is absolutely correct.. but the compiler reads value from left to right so x<y is computed first then the resultant with z so output is 1(also operation is same so no precedence issue)..
3rd May 2019, 12:59 PM
Min_desu_ga
Min_desu_ga - avatar
+ 1
sorry not tanay, Yamin Mansuri .. Am new to this app so mistake .. gomenasai
3rd May 2019, 1:00 PM
Min_desu_ga
Min_desu_ga - avatar