+ 2
Plz explain me....how this output comes: -9,3,0,1-9,3,1,1
Code :#include<stdio.h> int main() { int a=-10, b=3,c=0,d; d=a++||b++&&c++; printf("%d,%d,%d,%d",a,b,c,d); a=-10, b=3,c=0; d=c++&&b++||a++; printf("%d,%d,%d,%d",a,b,c,d); return 0; }
2 Respostas
+ 6
I will try to break it down as easy as I can So :
d=a++
a=-10+1
a=-9
d=a++ || b++
next we have
d=a++ || b++ && c++
c= 0 ; b= 3 because a is false a<0 stop the next ones
So b++ && c++ = true
Wich equals to 1
a++ || 1 = true = 1
d=1
printf("%d,%d,%d,%d",a,b,c,d);
%d to print it in decimal
So the output for this first half is -9,3,0,1
That's the first half now the second one
d= c++ && b++ || a++
c= 0+1 =1
b= 3 because c is true c>0 pass to the next one
a=-10+1=-9
d= 1 || a++ = true = 1
The output is -9,3,1,1
The result will appear:
-9,3,0,1
-9,3,1,1
I hope I was helpful enough as I know it this way, hope I didn't miss anything
+ 1
a++ || b++ && c++;
here it is a conditional operation statement and first a++ is return -10,
since it is not 0 and, hence statement result return as true and you know, that or ( || ) opearator results true if atleast 1 statement is true.. So d is assigned to 1 (equalenrmt of true as integer value).
And a++ is evaluated next, so a = - 9
And first statement a++ returned true then it won't execute b++, c++ (no need, since no affect result).
So
a= -9, b=3, c=0, d=1.
Same way. :
C++ && b++ || a++
First
(c++ && b++ )=> 0&&3 = 0, (c++ is post increment opearator, so 1st it uses value, then after incremented) so Next c++ => c=0+1=1;
b++ not evaluated since c returned 0 and
0&&0 or 0&&1 both result 0 => so b=3.
And next
0 || a++ => 0 || -10 =>true, a++ => a=-9;
a = -9, b=3, c=1,d=1..
1) anything otherthan 0, is treated as true.
2) 0 || X=> 1
3) 0 && X => 0
X is expression, not evaluated in both cases.
4) read about post increment
Hope it clears, reply if any..
(note: considering no sequence point issue)