+ 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
21 Réponses
+ 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
+ 6
Tushar kumar use ++n,--n as they are pre-increament/decreament statements and n++,n-- are post
+ 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?"
+ 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.
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?
0
Brave Tea Yes Sure sir...have a look..I linked up the code
0
mind you, your original post is still incorrect. it has the wrong outputs :)
0
Brave Tea Sir One more Question..is there any Priority level...that I will first choose to return then...Increment the value?
0
Okay..Thank you^^
0
Utkarsh Yadav Did not get you Sir what you want to say?
0
he basically said the same thing I did, but then shorter. also when you do ++n it does +1 first and then returns
0
Oh Okay :)
0
Cek
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!
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
0
Celine PHAM Thankyou Dear.. it's help me
0
Привет
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.
- 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
- 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