+ 5
C printf and arguments
Can any one explain that how come the output is 45555 for int i=5; printf("%d%d%d%d%d",i++,i--,++i,--i,i);
14 Réponses
+ 8
Sebastian The answer is an absolute UB.
HonFu I still wanna do that crime! 8P
https://www.sololearn.com/discuss/1609437/?ref=app
+ 5
Yeah, at least in that situation we know where to looking for the link to copy/paste! AND again probably people won't give a darn to look through it and as a result, it reminds us that there was an easy solution in the first place! H.A.N.G.
LOOOL!
+ 4
I don't know man! Maybe we should write a pdf book or make a blog for the top 10 mistakes of Sololearners for all ages! LOL
+ 4
It is interesting that printf("%d%d%d%d%d",d(i++),d(i--),d(++i),d(--i),d(i)); returns 45545.
d() is declared as int d(i) {return i;}
+ 3
C++ Soldier (Babak) Do you know the Answer? I can follow the Logic of increments and decrements but don't understand why the first printed Number is 4.
+ 3
C++ Soldier (Babak)
I keep telling people that xp are foremostly earned by doing challenges. People continue getting confused, believing that other ways to earn them would even come close. That is my burden to carry.
And your burden is to stick all of our noob noses into the UB matter over and over.
Carry it with pride! ;-)
+ 3
That might become a document like 'sololearn guidelines', copypasted to every second thread here for all eternity. 😂
+ 3
The answer to why that is PLUS the reasoning why that doesn't even matter has been linked up there by C++ Soldier (Babak).
EDIT: To my dear downvoter.
If you actually clicked at the link the Coldier provided, read the procedure how to find the 'solution' I described there and applied it to the given problem, you would see: Oh, nice, that seems to explain it!
If you then read Cs comments about why we can not rely on such a result anyway, you would have gotten the whole deal.
Of course hitting that down arrow is a lot easier than playing it out yourself.
+ 2
Awais Ahmad Thanks. That's clear. But the Question is why the first printed Number is 4?
+ 2
SHAME!
+ 2
Gordie Good surgery, I love it! +2
+ 1
if it’s i and then ++. it will use the val of i first. then add 1 to it. else it will add first
hope to help you:-)
0
Let me simplify this code by examples.
int i=5;
printf("%d",i);
It prints 5 on the screen.
int i=5;
printf("%d", ++i);
This statement first increment the value of "i" by one and then print it which will be 6. An the same case is with - - i.
int i=5;
printf("%d",i ++);
This statement first prints the value of "i" and then increment the value of "i" by one. And the same case is with i - -.