+ 2

i++ and ++i I'm not getting how is this is working in c

#include<stdio.h> int main () { int i=10; printf("%d%d%d",i,++i,i++); return o; } output : 10 12 12 how????????

29th Apr 2017, 9:58 AM
Zameer shaikh
Zameer shaikh - avatar
21 Antworten
29th Apr 2017, 10:35 AM
Sachin Artani
Sachin Artani - avatar
+ 18
30th Apr 2017, 10:48 AM
Sachin Artani
Sachin Artani - avatar
+ 16
Zameer, wait for a while...
30th Apr 2017, 10:40 AM
Sachin Artani
Sachin Artani - avatar
+ 6
I tried it and was shocked, output is 121210. https://code.sololearn.com/c4r1k93b5GkA/?ref=app
30th Apr 2017, 8:59 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 5
30th Apr 2017, 9:04 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 5
That always show result like that surely..... Must be... Integer i = 10; String a = String.format("%d%d%d",i,++i,i++); System.out.print(a); To make more sense.....
30th Apr 2017, 10:03 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
No... It's 12 12 10..... Because of......Dunno. we ever talk about this and not get answer yet :P
29th Apr 2017, 10:09 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
#include <iostream> using namespace std; int main(){ int i=10; cout << i << ++i << i++; //This one too -_-.... }
29th Apr 2017, 10:12 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
10 12 12? Where did you get that result?
29th Apr 2017, 10:18 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
From http://stackoverflow.com/questions/33445796/increment-and-decrement-with-cout-in-c "Please notice that cout << x++ << ++x; is just another notation for: operator<<( operator<< (cout, x++), ++x); The order in which your x++ and ++x statements are evaluated is undefined, so is the effect of your code. Even if it seems to happens from right to left in your particular examples, you should not rely on that by any means. Just make another experiment by using multiple statements as: cout << ++x << " "; cout << x++ << " "; cout << x++ << endl; The solution to your problem is: Never write code which results in undefined behaviour! :)"
29th Apr 2017, 10:41 AM
Calviղ
Calviղ - avatar
+ 2
it was question from one of the best company during interview
29th Apr 2017, 10:42 AM
Zameer shaikh
Zameer shaikh - avatar
+ 2
in c++ also #include <iostream> using namespace std; int main() { int i=10; cout<<i; cout<<++i; cout<<i++; return 0; } output is 10 11 11
30th Apr 2017, 10:39 AM
Zameer shaikh
Zameer shaikh - avatar
+ 1
Output is 10 11 11
29th Apr 2017, 10:05 AM
Calviղ
Calviղ - avatar
+ 1
Maybe due to the way printf handles arguments.
29th Apr 2017, 10:15 AM
Calviղ
Calviղ - avatar
+ 1
ok
30th Apr 2017, 10:40 AM
Zameer shaikh
Zameer shaikh - avatar
0
no its 10 12 12
29th Apr 2017, 10:09 AM
Zameer shaikh
Zameer shaikh - avatar
0
I'm getting 10 12 12 as op
29th Apr 2017, 10:09 AM
Zameer shaikh
Zameer shaikh - avatar
0
yes 10 12 12 or 12 12 10 as output how?????
29th Apr 2017, 10:10 AM
Zameer shaikh
Zameer shaikh - avatar
0
I also checked in c,c++ and java getting same ans as 12 12 10
29th Apr 2017, 10:13 AM
Zameer shaikh
Zameer shaikh - avatar
0
I assumed that ll like this 10 12 12 but the op is 12 12 12 10 I'm getting confused
29th Apr 2017, 10:24 AM
Zameer shaikh
Zameer shaikh - avatar