C printf and arguments | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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);

28th Dec 2018, 2:04 PM
Venkat
Venkat - avatar
13 Answers
+ 8
Sebastian The answer is an absolute UB. HonFu I still wanna do that crime! 8P https://www.sololearn.com/discuss/1609437/?ref=app
29th Dec 2018, 11:01 AM
Babak
Babak - avatar
+ 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!
29th Dec 2018, 11:47 AM
Babak
Babak - avatar
+ 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
29th Dec 2018, 11:22 AM
Babak
Babak - avatar
+ 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;}
30th Dec 2018, 8:24 PM
Микола Федосєєв
Микола Федосєєв - avatar
+ 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.
29th Dec 2018, 9:02 AM
Sebastian Keßler
Sebastian Keßler - avatar
+ 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! ;-)
29th Dec 2018, 11:12 AM
HonFu
HonFu - avatar
+ 3
That might become a document like 'sololearn guidelines', copypasted to every second thread here for all eternity. 😂
29th Dec 2018, 11:35 AM
HonFu
HonFu - avatar
+ 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.
30th Dec 2018, 8:14 AM
HonFu
HonFu - avatar
+ 2
Awais Ahmad Thanks. That's clear. But the Question is why the first printed Number is 4?
30th Dec 2018, 8:10 AM
Sebastian Keßler
Sebastian Keßler - avatar
+ 2
SHAME!
30th Dec 2018, 5:15 PM
Babak
Babak - avatar
+ 2
Gordie Good surgery, I love it! +2
31st Dec 2018, 2:55 PM
Babak
Babak - avatar
+ 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:-)
30th Dec 2018, 12:22 PM
ISAAC•FOSTER
ISAAC•FOSTER - avatar
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 - -.
30th Dec 2018, 7:40 AM
Awais Ahmad
Awais Ahmad - avatar