closed by
6,125 views
2 votes
2 votes
closed with the note: answered
#include <stdio.h>

int main(){

int a=1 , b=3;

a += b -= a-=b;

printf("%d %d",a,b);

}

what is the output ?

a.  3 4

b.  3 5

c.  2 4

d. compilation fails
closed by

2 Answers

3 votes
3 votes
ans b) 3,5

expand the expression in this way for clarity :

a=a+b=b=b-a=a=a-b;

execute from right to left.

3= -2+5= 5 = 3 - (-2)  = -2 = 1-3

note: this kind of expression has unspecified behavior for different compilers. I executed in gcc 5.3.0
3 votes
3 votes

Answer is : (b)  3  5

a+=b-=a-=b;

This contains three logics:

a+=b;              =>     a=a+b;

b-=a;               =>     b=b-a;

a-=b;              =>      a=a-b;

in the main logic these three logics are equal:

a=a+b     =    b=b-a     =     a=a-b;

                                         -2 =1-3

                     5=3-(-2)  

3=-2+5

so, a=3,b=5 

Related questions

0 votes
0 votes
0 answers
1
balaganesh asked Aug 31, 2018
227 views
the output of the following problem?main(){ int a=1,b=2,c=4; printf("%d",a+=(a+=4,10,a));} Explain?