0

int x=1; x=x+2*x++; printf ("%d",x); Output is 4 int x=1; x=x++; printf ("%d",x); output is 1

Anyone explain me why there is no increment in 2nd program while it is incrementing in 1st one

10th Jul 2019, 2:20 PM
Rohit Ahuja
Rohit Ahuja - avatar
6 Réponses
0
I think what it is doing is that , in the first code Initially x is 1 Then when it comes to second line for assigning to LHS it must first compute RHS soo here it sees there is an post increment in the statement soo it says "first use then increment" so it multiplies the value of 2 to 1 and then as x has been updated after this use it makes x to 2 soo now the concept is . X=2+2*1 . In the second code x in LHS is given value of 1 and not 2 since its post increment. And hence 1 is printed . The concept is about incrementation and usage , hope this helps.
10th Jul 2019, 2:43 PM
Saket Bhatt
Saket Bhatt - avatar
0
Saket how u r solving the code 1st I am also trying to solve that but in youtube video one person explain that x=1+2*1=3 and then in the next line it print 4 by incr....
11th Jul 2019, 8:48 AM
Rohit Ahuja
Rohit Ahuja - avatar
0
Brother I told you what I have experienced and learnt.I could have mistaken in understanding ,but I need a proper,more logical explanation to agree with you.
11th Jul 2019, 8:54 AM
Saket Bhatt
Saket Bhatt - avatar
0
Ok bro don't worry just chill
11th Jul 2019, 8:55 AM
Rohit Ahuja
Rohit Ahuja - avatar
0
If you can share that video ,it will be helpful..thankyou!
11th Jul 2019, 8:56 AM
Saket Bhatt
Saket Bhatt - avatar
0
Int x=1; [ ] { Printf("%d", x); X++; } [ ] ( x<= 3) [ ]
15th Jul 2024, 3:13 AM
Nithin Kumar Varma
Nithin Kumar Varma - avatar