This question already has answers here:
Why doesn't a+++++b work?
(9 answers)
Closed 8 years ago.
The following code prints 7. It increments value of a, but b remains unaffected. Post increment operator is not supposed to increment first and then use the value which it has done in this case. It should be the other way around. Why this change in behaviour? Also, and perhaps more importantly, why is b not pre-incremented?
int main() {
int a=5, b=2;
printf("%d", a+++b);
}
You have a well defined behavior. Where
a++ + b = 5 +2 = 7
If you have a pre-incrementor operator like
printf("%d\n",++a+b);
Then the output would be 8
The ++ operator has higher precendence than the + unary plus operator so the evaluation from left to right of
a+++b will happen
a++ + b which will fetch you 7
Post-incrementation is done by taking the value of your variable first which in this case is 5 so
a= 5 and b=2
In your code a is added to b and then it is incremented
Related
This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Closed 3 years ago.
I don't understand why a equals 1 and b equals 0 at the end. They should be the same in my view. Thanks in advance.
#include "stdio.h"
int main()
{
int a=0;
int b=0;
a++;
printf("a=%d,b=%d",a,b++);
return 0;
}
Before this call:
printf("a=%d,b=%d",a,b++);
the variable a was already incremented by:
a++;
However in the pointed-to call of printf, the value of the post-increment expression b++ is the value of the variable b before its increment. So 1 and 0 are output.
If you want to get the output 1 and 1, then use the pre-increment expression with the variable b like:
printf("a=%d,b=%d",a,++b);
From the C Standard (6.5.2.4 Postfix increment and decrement operators)
2 The result of the postfix ++ operator is the value of the
operand. As a side effect, the value of the operand object is
incremented (that is, the value 1 of the appropriate type is added to
it)...
The operation b++ sends b to printf before doing the increment. a is incremented before printf is called
printf("a=%d,b=%d",a,b++);
Is logically equivalent to:
printf("a=%d,b=%d",a,b);
b++;
This question already has answers here:
Why doesn't a+++++b work?
(9 answers)
Closed 5 years ago.
In C, is a+++b equal to a+b++?
They are and will be equal if you supply the same initial values of the operands.
In your case, the side effect of the first statement (post increment on a) is affecting the second one. Due to the presence of the post-increment in the first expression, a is incremented to 3 before the next statement is executed.
Re-initialize the variables with the same genesis value before calculating the second one.
You need to check the C operator precedence to understand it.
The confusing thing here is that a+++b may be read as either a + (++b) or as (a++) + b. According to the C operator precedence, it is actually looks like:
int a=2, b=3, c;
c = (a++) + b; // 2+3=5 and 'a' will be 3 after that line
printf("%d\n",c); // c = 5
c = a + (b++); // 3+3=6 and 'b' will be 4 after that line
printf("%d\n",c); // c= 6
From the link above:
++ as sufix has highest priority.
++ as prefix has lower priority.
+ has even lower priority.
int a=2, b=3, c;
c = (a++) + b; // The value for a will be 3 after that line
printf("%d\n",c); // c = 5
c = a + (b++); // So here a value is 3 (3+3) =6 after executing this line b value will be 4
printf("%d\n",c); // c= 6
To avoid this you need to reinitialize the variables
c = a+++b;
is equivalent
c = a++ + b;
a++ means post-increment, means expression takes the value of a and then increment.
c = a+b++;
is equivalent
c = a + b++;
b++ means post-increment, means expression takes the value of b and then increment.
If you provide same value in both cases, then both express variable c same.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
Here is a question I can not explain clearly.
Which option is wrong and why?
(A) a += (a++);
(B) a += (++a);
(C) (a++) += a;
(D) (++a) += (a++);
What is the difference between A and B?
My understand:
A is a UB but B is OK because the side effect of ++a will be completed before the assignment. Is that right?
Update: What is the difference between ++a and a++ within the sequence point? The side effect of pre-increment(decrement) may be completed any time before the next seq-point just like post-increment(decrement)?
Which option is wrong and why?
All of them are wrong, and that's because the first two invoke undefined behavior, and the last two don't compile. (And if they did, they would invoke UB as well.)
The first and second is UB because C does not define what is supposed to be evaluated first.
The third and fourth do not compile - the reason: lvalue required as left operand of assignment
Let me be more specific on the first two:
a += (a++); - equal to a = a + (a++);
if the a++ is evaluated first then it's a = a + 1 + a;
if the a++ is evaluated at the end then it's a = a + a;
but C does not define what should happen first, so it depends on the implementation, and it's UB.
The same goes for the second.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C comma operator
I came across a line of code which I couldn't understand. I remember seeing something similar somewhere.
int x,y,z;
x=(y=2,z=2*y,z+4);
I know that the value assigned to x is 8. Can someone explain me why?
This is equivalent to:
y = 2; // y == 2
z = 2 * y; // z == 4
x = z + 4; // x == 8
The operands of the comma operator are evaluated from left to right and the result is the value of the right operand.
the comma operator separates the previous values, and the last item in the comma is returned as the result, e.g.
a = b,c
assings the value of c to a. The parentheses here do essentially nothing, btw
So you have two assignments, then a statement, whose result is returned and assigned to x
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 years ago.
I am new to C, i have an Increment operator program in C
#include<stdio.h>
main(){
int a, b;
a = 2;
b = a + ++a + ++a;
printf("%d", b);
getchar();
}
The output is 10, can someone explain me how the output will be 10 .
a + ++a + ++a;
Behaviour for this is undefined.
The compiler might generated code that evaluated this as 2 + 4 + 4 or 3 + 3 + 4, but any combination/ordering of incrementing and accessing is a "valid" result.
This is undefined, the ++i can happen in any order.
Function call arguments are also ambigiously evaluated, e.g. foo(++i,++i).
Not all operator chains are undefined, a||b||c is guaranteed to be left-to-right, for example.
The guarantees are made in places known as sequence points although this terminology is being deprecated and clarified in C++0x.
What's odd in your example is that neigher 2+3+4 nor 4+4+3 happened, so the compiler evaluated the left side first in one step and the right side first in the other. This was probably an optimisation to flatten the depencency graph.