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
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
+ 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);
+ 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
+ 1
Thanks to all of you
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.
0
Nikhil Sharma assignment operator has less precedence than math operators
0
🇮🇷 M N There are two = operators and second one will be executed first, that's associativity.