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.
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:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 5 years ago.
int x=0, y=0, z=0;
z = (x==1) && (y=2);
printf("%d ", y);
I'm wondering the output is 0.
Why the output is not 2?
Because of how C deals with the logical operators:
Since x==1 returns "false", there is no need to check the RHS in order to conclude that the end result of the operator && is false. Hence, RHS is not evaluated and y stays at its previous value.
This question already has answers here:
Who defines C operator precedence and associativity?
(5 answers)
Closed 5 years ago.
int a=98, b=10;
float c;
c=a/b;
Output: c=9
I know because of implicit type conversion, c value is 9 not 9.8 but then I encountered this question:
int a,b,c,d;
a=40;
b=35;
c=20;
d=10;
printf("%d",a*b/c-d);
output: 60
Now if we see the precedence for equation is right to left and according to BODMAS rule b/c(35/20) will be performed first so 35/20 = 1.75 and then implicit conversion to integer make it to 1 and then rest will follow up the answer must be 30 but the output is 60 which is correct answer. Can you explain me why?
Operator precedence in C for * and / in C is left-to-right: http://en.cppreference.com/w/c/language/operator_precedence
So a*b will be done first. It's equivalent to writing ((a*b)/c)-d.
So your execution will follow left to right execution for / and *:
((40 * 35) / 20) - 10 = ((1400) / 20) - 10 = (70) - 10 = 60
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:
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