+ 3

Why it gives 12 12 10 ?

#include <stdio.h> int main() { int i=10; printf("%d\n%d\n%d",i,++i,i++); return 0; }

13th Oct 2021, 2:58 AM
Shubham Maske
Shubham Maske - avatar
11 Respuestas
+ 2
Shubham Maske As I said before, increment operators have right to left precedence so, When pre increment is in use, then it will evaluate I and reserved this value for a temporary time, bcz it will be printed according to last value of I. by pre increment the value of I will 21. Now again pre increment so the value of I is 22 Bcz it's post increment then it will be printing 22 then increment the value of I by 1 and now it's 23 In the last one it's only I and I=23 Now it's time for printing the value of pre increment those we are having, Put last value of I (23) in each of pre increment. Final output: 23 22 23 23 (In case of GCC)
13th Oct 2021, 3:52 AM
Rupali Haldiya
Rupali Haldiya - avatar
+ 11
While it gives 12 12 10 on sololearn code playground, it gives 10 10 11 in Cxxdroid. The documentation says that C/C++ doesn't guarantee the order in which the parameters are passed to a function(the parameter copying order). So it means that, maybe the ++i is passed first to the printf function, then i and then i++. Or the other way, we can't know which order the parameters are passed(copied to the function parameters). Different compilers give different results for this. The is called undefined behaviour. So it's advisable to say "don't make your program dependent on the order in which any function argument is passed". All the best =) Edit:Tnx for the upvotes, I almost thought my answer is wrong =)
13th Oct 2021, 3:08 AM
Rishi
Rishi - avatar
+ 5
Rellot's screwdriver I think you're not getting my point. I'll explain my point considering your example. You're calling foobar(2+3,1*2) What I'm saying here is that it's not guaranteed that wheather 2+3 will be assigned to foo first and 1*2 assigned to bar second, or 1*2 to bar first or 2+3 to foo second. The order of assignment is unpredictable by the user. That's what I'm trying to say
13th Oct 2021, 10:32 AM
Rishi
Rishi - avatar
+ 4
Rishi agree with you but according to GCC compiler it's 12 12 10 and i really don't know why it's 10 10 11 in cxxdroid.
13th Oct 2021, 3:50 AM
Rupali Haldiya
Rupali Haldiya - avatar
+ 3
Rupali explain the output I got in Cxxdroid (mentioned in my answer above)
13th Oct 2021, 3:15 AM
Rishi
Rishi - avatar
+ 3
Ok, then why below code give answer 23 22 23 23 #include <stdio.h> int main() { int i=20; printf("%d\n%d\n%d\n%d",i,i++,++i,++i); return 0; }
13th Oct 2021, 3:34 AM
Shubham Maske
Shubham Maske - avatar
+ 3
Rellot's screwdriver what do you think about this question? Is my answer wrong here?
13th Oct 2021, 7:03 AM
Rishi
Rishi - avatar
+ 2
In C, precedence of increment operator is right to left so, from right it will print 10and then increment the value of I by 1 bcz it's post increment,then now the value of I is 11 and in next it will first increment the value of i by 1 and then it will be printed (11+1=12) and in last one once again 12 will be printed bcz you're not performing any operation here, the value of I is 12 now. Final output 12 12 10
13th Oct 2021, 3:10 AM
Rupali Haldiya
Rupali Haldiya - avatar
+ 2
Rupali Yes got it ,thank you
13th Oct 2021, 4:37 AM
Shubham Maske
Shubham Maske - avatar
+ 1
Compiler starts from right to left (turbo c/cpp compiler) So 12 12 10
14th Oct 2021, 5:38 PM
Praween Gupta
Praween Gupta - avatar