This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Undefined Behavior and Sequence Points
What will the value of x in following line of code:-
x = a++ + ++a + a++
actually compiler is giving x = 3..but how????
This is undefined behavior. Any answer is valid, up to and including crashing. It is unlikely that any particular answer will be consistent between platforms. Don't do this.
Undefined behavior, as the value of a is modified multiple time in the same statement.
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
int a = 5;
if(a==a++){
printf("true 1");
}
if(a==++a){
printf("true 2");
}
When I run this code, it prints "true 2". I do not understand how. Please help.
Also, how is logical equivalence computed in precedence with increment operators?
The order of evaluation in a==++a is not defined by the standard. Thus the ++ can be performed before the comparison or after comparison. With another compiler you can get different results. This is called 'UB', or 'Undefined Behavior'.
This code will give Undefined Behavior in many ways. But if you initialize a, the difference is that ++a will return the incremented value, while a++ will return the new value.
Also, in for loops, you should use ++a and you will not go wrong.
Let's evaluate your problem.
In the first case, when you compare a with the incremented value of a (as a++ return the incremented value), so it is false. Example: a has 5, and the incremented value is 6. So, as they do not match, it will be a false.
In the second case, when you compare a with the old value of a (as ++a returns the original value), you get true. Example: a has 5 and when you increment it using ++a, you get the old/original value, which is also 5. Thus, you get a true.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
Here is the C program which is giving different output depending on Compiler used:
#include<stdio.h>
int main()
{
int i = 5,j;
j = ++i + i++ + ++i + i++;
printf("%d",j);
return 0;
}
Check the output on the following link.
http://imgur.com/z9aMSwj,Vwx3P9S
http://imgur.com/z9aMSwj,Vwx3P9S#1
My question is what is the technical reason that output is different?
The technical reason is that there is no defined behaviour for such an operation, allowing compilers to handle this kind of an order as they wish. Such cases are generally referred as Undefined Behaviour.
According to C language, expressions like ++i + i++ + ++i + i++ have undefined behavior.
In a i++ expression, two things happen. First, the expression is evaluated to i, then i is incremented later. The behaviour of 'later' is undefined. If there are 4 such expressions in a statement, there are countless possible orders to evaluate the increments and decrements.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
the answer is coming out to be 45.I cant understand how this thing is working.
main()
{
int a =10;
int i = a++ + ++a + a++ + ++a;
printf("%d , %d ", i,a);
}
Actually the output is an undefined behavior which is fine.
From the C99 standard are 6.5 Expressions, §2
Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be read only to determine the value
to be stored.
The order of evaluation of the operands is unspecified. If an attempt
is made to modify the result of an assignment operator or to access it
after the next sequence point, the behavior is undefined.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
int a[]={10,20,30,40};
int x=0;
int v=a[++x]+ ++x + a[--x];
printf("%d",v);
What will be the output of this program??
Completely confused with the output. No way it is going to be done according to my operator precedence knowledge.
According to me, in this expression Array subscripting [] has highest precendence and should be executed first. so both [] should be executed first from left to right. In this case value of x will increment first, then decrement and finally come back to 0. so expression will become int v=a[0] + ++x + a[0]. Then the pre increment is having highest precedence and it will be incremented to 1. so our expression will become int v=a[0]+1+a[0]. so final output will be 21.
But this is not the case. I have checked on different compiler implementations and no one prints 21.
I am much surprised because the value printed is 43, which is no where understandable to me. That's why I want someone to help me understand and come to the result 43.
The link which others have suggested is using only increment and same rvalue and lvalue cases. But this is somewhat different and not clear. I tried to contruct expression tree for this and solve but 43 is no where in scope.
Output of this code:
int v=a[++x]+ ++x + a[--x];
is undefined and it depends on the compiler implementation.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
main()
{
int a=5;
a= a++ + ++a + ++a + a++ + a++;
printf("%d",a);
}
This is not defined.
You can find the Committee Draft from May 6, 2005 of the C-standard here (pdf)
See section 6.5 Expressions:
2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
and the example:
71) This paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;
The answer is actually undefined.
Answer in undefined because you've got some situations in which the parser doesn't know how to parse the code..
is a+++b: a + ++b or a++ + b?
Think the fact that usually white space is just ignored when lexing the source code. It may depends upon implementation of the compiler (and some other languages with same ++ operators may choose to give priority to one instead of another) but in general this is not safe.
For example in Java your code line gives 37 as the answer, because it chooses to bind ++ operators in a specific way according to precedence, but it's just a choice..