0
#include<stdio.h> int main() { int a=1; printf("%d%d%d",a++,++a,a); return 0; }
output should be :221 but it is coming 233 how ??
14 Antworten
+ 6
@Sreejith This question should not appear in any exam. If it does, you can write any answer and appeal if you are marked wrong for that specific question.
+ 5
The operation does not go blindly from left to right. ++a is evaluated prior to statement execution, a becomes 2. Printing a++ will return 2, and then a gets incremented to 3.
Note: prefix and postfix operators increment the value of a variable and store it back to the variable. The value is not incremented just for that instance.
+ 5
@Sreejith Did you even compile the code.
Your statements would be correct if the code was separated on single lines:
int a = 1;
cout << a++;
cout << ++a;
cout << a;
will return 133, but not for our case here in original post. In fact, the behaviour of multiple prefix/postfix operators on a single continuous line of output code (either through cout or printf) is undefined and is up to the compiler.
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15
+ 5
@Seeejith I compiled the code as it is both on Code Playground and desktop compiler. It returned 233 in both environments.
The site I tried to link to may have been down for some time now. This is the original source:
https://stackoverflow.com/questions/5204805/explanation-of-call-ordering-in-a-cout-statement-postfix-vs-prefix
Also:
https://stackoverflow.com/questions/18174498/how-is-the-postfix-and-prefix-increment-operator-evaluated-in-an-expression
This isn't just about 'the concept of postfix'.
+ 5
@Sreejith
I don't know. :D
We can deduce the logic behind the compiler's decisions, but cannot predict the decision of a compiler. Hence, all our answers are correct for each of our own specific compiler.
+ 3
Sreejith, this is called "undefined behaviour" and is compiler dependent.
https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
+ 3
@hatsy @karl T. So if this question appears in any exam which answer should be given
+ 2
because
a++ is increments value of a after
++a is increments value of a before
a is the new value printed
a=1 at start of the program
then a++ increments it's value by 1 so now it's 2
then again it is Pre increment ++a so now it's value is 3 then a will be printed it's present value is 3
so output is 233
+ 2
I'm surprised the preprocessor does not even complain about the lack of space between #include and <stdio.h>
+ 2
@hatsy I compiled it on 2 apps both gave me 133 but playground gave me 233
So according to the post you mentioned
133, 231, 221, 233
are all legal answers on different compilers based on their settings and priority
now
what is the correct answer to this question ??
or
is there any one correct standard answer to this question??
+ 1
both of your answers are wrong
it is 133
not 233 or 221
because a++ is post increment ,so value of a will be incremented next time you use the value.so it displays "1".
++a is pre increment, its value will be increased and displayed.so its displays "3". (2 from previous increment)
finally a, after all the increments, the value of a has changed to "3".
0
@hatsy did you compile it??
I dont think you understand the concept of post fix
and your reference site not working
0
answer can be very
depends upon the compiler
due to unspecified behaviour
- 6
@hatsy Rei
operation will go from right to left