What will be the output of the C code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What will be the output of the C code

#include <stdio.h> void main() { int x = 1, y = 0, z = 5; int a = x && y || z++; printf("%d", z); }

8th Nov 2019, 3:36 PM
Ibrahim Zakeeyu
3 Answers
+ 10
Try it on Sololearn Playground.
8th Nov 2019, 3:45 PM
A͢J
A͢J - avatar
+ 6
In C, order of evaluation of operands is defined for && and || operator(it is from Left to Right), also this expression will be evaluated from L to R with no short-circuit. You need to evaluate z++ to get value of a so z get incremented by 1. ((x&&y)||(z++)) shows the precedence -> (x&&y) : x 1st, y 2nd (order of evaluation) -> since its 0, we need 2nd expression z++ to get final value -> z++
8th Nov 2019, 3:55 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
~ swim ~ Correct, && have more precedence than || I will correct it, thanks for pointing out this mistake.
8th Nov 2019, 5:53 PM
Gaurav Agrawal
Gaurav Agrawal - avatar