+ 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
43 Respuestas
+ 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.
+ 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.
+ 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
+ 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?
+ 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
+ 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
+ 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...
+ 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
+ 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
+ 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
+ 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
+ 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.
+ 2
these operation is in c programing
that are the program in exicuted
when the answers is output is.
12 12 10
+ 2
looks like increment😌😌😌
+ 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
+ 2
it should be 10 11 11
+ 2
output is different for different compilers
+ 2
10 11 11
+ 1
These are unary operators of Java used for increment & decrement