+ 4

Please anyone explain me...Why Output is different? Can't I write first code in another way which I used? Please Help me.

int main() { int n=7; printf("%d",n++); printf("%d",n--); return 0; } Output: 78 int main() { int n=7; n++; printf("%d",n); n--; printf("%d",n); return 0; } Output: 87 https://code.sololearn.com/cJyDaMRH3b6t/?ref=app https://code.sololearn.com/caU2YOoYqYsd/?ref=app

30th Oct 2019, 6:50 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
21 Answers
+ 6
excellent. so yeah, I was right in my first comment. in code 1 (the link) you get the two outputs: 8 and 7 (together outputted as 87 because you didnt use a \n newline) this is because you first do n++ which makes n=8 this is then printed, then you do n- - which makes n=7 and then print it. in code 2 you put everything in the printf n++ works as follows 1. return n 2. increment n by 1 because step 1 is return n (and n = 7 here because it hasn’t reached step 2 yet) you get a print of 7 but in the background n is now 8 so when you do another printf you get 1. return n (which is now 8 because you incremented it in the last printf) 2. decrement n (n = 7) if you would write a printf(n) after the last statement you would see 787
30th Oct 2019, 9:57 AM
Brave Tea
Brave Tea - avatar
+ 6
Tushar kumar use ++n,--n as they are pre-increament/decreament statements and n++,n-- are post
30th Oct 2019, 1:17 PM
Utkarsh Yadav
Utkarsh Yadav - avatar
+ 1
I think you had tagged wrong language, you should've tagged C instead : ) Having fixed the missing comma in the two calls to `printf` in the first code, the first code outputs 78, and the second code outputs 87. I didn't really understand what you're asking by "Can't I write first code in another way which I used?"
30th Oct 2019, 8:40 AM
Ipang
+ 1
Thank you so much...you explaination helped me a lot...and Sorry I just type the code again here instead of copying paste from playground...so I mistakenly swapped up the Output . Thanks again.
30th Oct 2019, 10:09 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Ipang Ah.. thanks for correcting me..I will edit my tag and code. And "Can't I write first code in another way which I used?" Means... Isn't both codes are meant to give same Output..I have just changed the way of writing code. Or the concept of both code is totally different.. that's why it giving different answer?
30th Oct 2019, 8:49 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Brave Tea Yes Sure sir...have a look..I linked up the code
30th Oct 2019, 9:51 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
mind you, your original post is still incorrect. it has the wrong outputs :)
30th Oct 2019, 9:59 AM
Brave Tea
Brave Tea - avatar
0
Brave Tea Sir One more Question..is there any Priority level...that I will first choose to return then...Increment the value?
30th Oct 2019, 10:51 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Okay..Thank you^^
30th Oct 2019, 11:33 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Utkarsh Yadav Did not get you Sir what you want to say?
30th Oct 2019, 1:20 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
he basically said the same thing I did, but then shorter. also when you do ++n it does +1 first and then returns
30th Oct 2019, 1:51 PM
Brave Tea
Brave Tea - avatar
0
Oh Okay :)
30th Oct 2019, 2:06 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Cek
31st Oct 2019, 1:00 AM
semut Music
semut Music - avatar
0
In the second sample, the increment and decrement operations on n took place before printing them both the times, while in the case of the first sample, they took place after printing n both the times. To get 87 as the output to the first code, you should change the post-increment to pre-increment and do the same for the decrement: —×—×— int main() { int n=7; printf("%d",++n); printf("%d",--n); return 0; } —×—×— To get 78 as the output to the second code, you just need to do the increment and decrement after printing both the times. Pre or post updation won't change the result, because the value isn't being used when being updated: —×—×— int main() { int n=7; printf("%d",n); n++; printf("%d",n); n--; return 0; } —×—×— Hope it helps!
31st Oct 2019, 7:03 AM
Param Siddharth
Param Siddharth - avatar
0
it's the order is different whether your print before or after incrementing, you printed before incrementation in the first code and you did the opposite in the second one
31st Oct 2019, 2:31 PM
Celine PHAM
Celine PHAM - avatar
0
Celine PHAM Thankyou Dear.. it's help me
31st Oct 2019, 8:59 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Привет
31st Oct 2019, 11:52 PM
Anime Lovers
0
Offcourse it will give different outputs. The increment and decrement used here are postfix i.e, in first code first it will print then increment or decrement. While in 2nd code its already increment/ decrement then we are printing the values of n.
1st Nov 2019, 12:24 AM
Himanshu baghel
- 1
I am not 100% sure here, but probably because n++ gives the output first and then increments the variable. so in the first example I think n - - the decrement is never printed because it prints first in the statement and then decrements. where in the second example the decrement happens outside of/before the print so it is decremented before it is printed try playing around with ++n and - - n to see if that makes a difference
30th Oct 2019, 8:40 AM
Brave Tea
Brave Tea - avatar
- 1
btw I epxected the results to be the other way around... are you sure your post is correct? please upload the code by using the plus to the left of the type screen
30th Oct 2019, 9:41 AM
Brave Tea
Brave Tea - avatar