0

#include<stdio.h> #include<conio.h> int main() { int x,y=2,z,a; x=(y*=2)+(z=a=y); printf("%d",x); getch(); }

Why output is 8 not 6 ? helppppp

21st Jun 2018, 6:24 AM
ALOK GOLDY
ALOK GOLDY - avatar
7 ответов
+ 3
y = 2 (y*=2) so y = 4 (z=a=y) so z = 4 and a = 4 x = (4) + (4) so x = 8 c calculates expressions with same precedence from left to right
21st Jun 2018, 6:43 AM
🇮🇷 M N
+ 2
y*=2 is executed first which updates the value of y to 4 z=a=y assigns 4 to z and a this is because of the order of code answer would have been 6 if x=(z=a=y) + (y*=2);
21st Jun 2018, 6:41 AM
‎ ‏‏‎Anonymous Guy
+ 1
Because expression is parsed from left where y will have double the value (2*2=4) and add to same value (z=a=y=4 then z,a,y will have all value 4 ) then x assignement expression make x= 4+4 thats equal 8
21st Jun 2018, 6:40 AM
KrOW
KrOW - avatar
+ 1
Thanks to all of you
21st Jun 2018, 6:44 AM
ALOK GOLDY
ALOK GOLDY - avatar
0
🇮🇷 M N Not always C calculates operators with same precedence from left to right. Different operators have different associativity like in case of = associativity is right to left.
21st Jun 2018, 7:26 AM
Nikhil Sharma
Nikhil Sharma - avatar
0
Nikhil Sharma assignment operator has less precedence than math operators
21st Jun 2018, 7:38 AM
🇮🇷 M N
0
🇮🇷 M N There are two = operators and second one will be executed first, that's associativity.
21st Jun 2018, 8:07 AM
Nikhil Sharma
Nikhil Sharma - avatar