Can anyone help me to understand this c problem? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone help me to understand this c problem?

What is the output of this code? int x= 2,y = 0; int z = (y++) ?2: y== 1 && x; printf("%d", z);

3rd Jun 2021, 7:28 AM
jayraj kharadkar
jayraj kharadkar - avatar
1 Answer
+ 5
Error as is, 1 if you put a space between the "int" keyword and the 'x' identifier. In C, a value of zero translates to false, while all values not equal to zero are true in a boolean sense. The post-increment y++ returns the value zero or false, and increments the value of 'y' after the evaluation. This means the condition is wrong, but 'y' equals one afterwards. This in turn means the comparison y == 1 is now true when evaluating the third operand, and 'x' is also true for the reason mentioned earlier. If both operands are true, the && operator returns one, otherwise zero, hence the final value of 'z'.
3rd Jun 2021, 7:38 AM
Shadow
Shadow - avatar