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
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Sequence Points between printf function args; does the sequence point between conversions matter?
(3 answers)
Closed 3 years ago.
Consider code below:
#include <stdio.h>
int main()
{
int x=0,y=5;
printf("x=%d,x_1=%d,sum=%d",x++,x,y+x);
return 0;
}
My assumption on this code was that, x would be printed as 0 and later on postincrement x_1 would be 1 and y+x be 5+1=6
Actual result is x as 0 as expected , x_1 as 1 as expected. But y+x be 5. I am unsure why x retains its previous value though an postincrement had occured. Could you please help clarify this?
I used gcc compiler for the same.
In
printf("x=%d,x_1=%d,sum=%d", x++, x, y+x);
// (a) (b) (b)
you are both updating x (a) and using its value (b) in the same expression (with no intervening sequence point).
That's Undefined Behaviour.
Try
printf("x=%d,x_1=%d,sum=%d", x, x + 1, y + x + 1);
x++;
This is standard undefined behaviour, order of evalution of function arguments is non deterministic. Read [Why are these constructs using pre and post-increment undefined behavior?
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:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
Closed 7 years ago.
What exactly is this called in C?
If I have int x = 3 * (4, 5);
I end up with 15.
Can someone give explanation?
Is this a list where the result is just the first number times the last one?
Thanks
In the C and C++ programming languages, the comma operator
(represented by the token ,) is a binary operator that evaluates its
first operand and discards the result, and then evaluates the second
operand and returns this value (and type).
https://en.wikipedia.org/wiki/Comma_operator
So, the result of the (4, 5) expression will be 5.
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
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
int main()
{
int a=10;
if (a == a--)
printf("true1\t\n");
a=10;
if(a == --a)
{
printf("true2");
}
return 0;}
as in the second if condition a=10 and --a is 9 i.e 10 != 9 so how come the second condition is evaluated ?
The value of --a is the previous value of a minus 1.
Furthermore that expression has the side-effect of changing the value of a.
The left part of the comparison is the value of a ... but is it the value of a before or after the side-effect has been applied?
The C Standard does not force the sequence of checking the value and applying the side-effect; and says that reading the value of a variable and changing its value without an intervening sequence point is Undefined Behaviour.
Basically there is a sequence point at every ; in the program (it's not as straightforward); your expression (a == --a) does not have sequence points.
I believe the next rule is also valid in C as in Java :
The --x has a higher priority than x--
a=10;
printf(a--) // should be showing 10;
a=10;
printf(--a) //should be showing 9