+ 48

Can anyone explain the output of this code?

#include <stdio.h> int main() { int a=10; printf("%d %d %d",a, ++a, a++); } //Output: 12 12 10

3rd Apr 2018, 6:05 PM
Md. Asiful Hoque Prodhan
Md. Asiful Hoque Prodhan - avatar
44 Respostas
+ 69
Any time you use any two of a, ++a, and a++ in the same statement, your result is undefined behavor. You could get 10 11 10, 11 11 11, etc. You output suggests a++ was processed first, follow by ++a, and finally a. But, you can NOT count on any value using that statement as it can process those values in any order.
3rd Apr 2018, 6:49 PM
John Wells
John Wells - avatar
+ 58
Different compilers will behave differently: For example, your code results are the following in these compilers: gcc: 12 12 10 clang: 10 11 11 vc: 11 11 10 Avoid these type of operations in a single line. You can store them in variables or just do the steps one by one. It's worth noting that these type of issues (compiler dependency) are unique to C/C++. You can easily do such stuff in Java, C# and the results will be always consistent.
4th Apr 2018, 1:51 PM
Aaron Sarkissian
Aaron Sarkissian - avatar
+ 35
It is simple stacking behaviour of compiler for single line short hand expressions. Stacking here is happening as : 1) a 2) ++a 3) a++ Now value of a is 10, and for stacks its LIFO: hence order of output will be 3) a++ (value 10 printed, then incremented to 11) 2) ++a ( value incremented to 12, then printed 12) 1) a (value 12 printed) Hence ordered output is: 1) 12 2) 12 3) 10
9th Apr 2018, 6:34 PM
Shashank Bafna
Shashank Bafna - avatar
+ 15
As mentioned above, C++ standard doesn't guarantee the exact behavior of sequential increment/decrement if they are in a single "line" (as in your example). As far as I know there is some specified parameter evaluation order before function call (in this case, the order of evaluation of "a", "++a" and "a++" before calling printf. Can anyone tell if there is an exact order of parameter evaluation (e.g. from left to right vs from right to left) and if yes, would it affect this code?
9th Apr 2018, 2:42 PM
vardanator
vardanator - avatar
+ 11
1--> start solving right to left -->a++ come across. do that.. increment in next statement -->++a increment at same point... now look at your problem print("%d %d %d",a, ++a, a++); . 12 12 10 <-- hope you understand very well.. I make me simple to understand
3rd Apr 2018, 7:29 PM
Arun Tomar
Arun Tomar - avatar
+ 8
https://code.sololearn.com/c2vnLDuLod5g/?ref=app 👆this is in C https://code.sololearn.com/ceD9Zw1LSar9/?ref=app 👆this is what I did same in Java But, output is not same in both languages Just check out those codes
10th Apr 2018, 1:10 AM
Nashat
Nashat - avatar
+ 6
the obvious answer is there is no adequate answer - As you can read many of the answers are telling you over and over the "best" answer...
9th Apr 2018, 7:16 PM
BroFar
BroFar - avatar
+ 5
Print starts from right to left.. Rightmost is post increment so right most will be printed as 10.. Now a value is 11.. Middle one is pre increment.. So first value of a is updated to 12 and then printed.. Now leftmost a is printed simply as 12.. However in C documentation order of argument parsing is undefined.. So results may vary according to compiler
9th Apr 2018, 5:19 PM
aakash kumar
aakash kumar - avatar
+ 3
performed by stack operation so a++ remains 10 and printed 10 then incremented to 11 and by ++a increments to 12 and printed then a is printed whose current value is 12 that is for a++=10 but a value becomes 11 for ++a =11+1=12 for a=12 and then printed according to stack operation last in first out
10th Apr 2018, 6:27 AM
Pooja Singh
Pooja Singh - avatar
+ 3
print() will always start its execution from right to left(stack) and post increment(a++) will give increment after evaluating pre increment(++a) before evaluating.. 1st increments and then evaluates with newly incremented value .. so when u start from right a =10 which means post inc(a) will give 11. now a=11 pre inc(a) = 12 a=12
10th Apr 2018, 10:50 AM
Shaik Ruha Tabasum
Shaik Ruha Tabasum - avatar
+ 2
In this kind of problems you gotta solve them from right hand side...such as in this question.. a=10;//given a++=10; //Post increment first uses the value and then increment it. Now a=11. Thus next is ++a which results in a=12. And in next you ave to print "a" ....which will print 12. Thus final output will be: 12 12 10
9th Apr 2018, 5:05 PM
NIKHIL kaushik
NIKHIL kaushik - avatar
+ 2
when we use printf() or cout for output it starts execution from right to left so first a++ would be executed (as it's postincrementer so first value would be assign ) then ++a would be executed which would increment a by 2 (10+2) then the 'a' on would be executed , as we have final value of a=12 so result is 12.
9th Apr 2018, 5:34 PM
Uzair
Uzair - avatar
+ 2
these operation is in c programing that are the program in exicuted when the answers is output is. 12 12 10
9th Apr 2018, 5:46 PM
Rahul Maurya
+ 2
looks like increment😌😌😌
9th Apr 2018, 7:39 PM
Asaølu Elïjah
Asaølu Elïjah - avatar
+ 2
in printf the elements go on stack in right to left order that means execution takes place from right to left whereas it is printed from left to right or if we talk about stack then lifo(last in first out). So a++ will be 10 then get incremented then ++a will be 12 as preincrement then a which is now 12. So output is 12 12 10
9th Apr 2018, 7:54 PM
Sumeet Kumar Rai
Sumeet Kumar Rai - avatar
+ 2
it should be 10 11 11
10th Apr 2018, 11:47 AM
APURVA SONAVANE
+ 2
output is different for different compilers
12th Apr 2018, 3:22 PM
Sushant
+ 2
10 11 11
13th Apr 2018, 1:05 AM
Ivanosky Sebastian Selva Ramos.
Ivanosky Sebastian Selva Ramos. - avatar
16th Apr 2018, 5:50 PM
Ivanosky Sebastian Selva Ramos.
Ivanosky Sebastian Selva Ramos. - avatar
+ 1
These are unary operators of Java used for increment & decrement
10th Apr 2018, 1:50 PM
Swastik Bhattacharya
Swastik Bhattacharya - avatar