• closed by
7,034 views
2 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

2 Answers

3 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 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 

Position:
Show:

Related questions

0 0 votes
0 0 answers
450
450 views
balaganesh asked Aug 31, 2018
450 views
the output of the following problem?main(){ int a=1,b=2,c=4; printf("%d",a+=(a+=4,10,a));} Explain?
3 3 votes
3 3 answers
256
256 views
GO Classes asked Jun 1
256 views
What value will $\texttt{x}$ store after the execution of the code below?x = 4 x = 2Value of $\texttt{x}$ multiplied by $\texttt{2}$Square of $\texttt{x}$Value of $\text...
12 12 votes
2 2 answers
465
465 views
GO Classes asked Jun 2
465 views
What is the output of the following code?#include <stdio.h int main() { int a = 5, b = 10; printf("%d", a b || b 0); return 0; }
3 3 votes
2 2 answers
148
148 views
GO Classes asked Jun 9
148 views
Consider the following Python code:x = 10 y = 6 result = (x & y) + (x > 1) + (y << 1) + ~1 print(result)What is the output of the code above?171915-17