Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
#include <stdio.h> | #include <stdio.h> | ||
− | |||
int main() | int main() | ||
Line 19: | Line 18: | ||
printf("%d %d", x, y); | printf("%d %d", x, y); | ||
} | } | ||
+ | |||
</syntaxhighlight> | </syntaxhighlight> | ||
|type="()" | |type="()" | ||
Line 26: | Line 26: | ||
+33 55 | +33 55 | ||
-33 56 | -33 56 | ||
− | || x--, y++ will return the value of y which is 1, as comma operator always returns the right value. Hence, switch case starts with 1. Before starting the switch case, x is decremented and y incremented also. So, in case 1, x is 7 and y is 2. x is changed to 56 in case 1. Because of no break, all cases are evaluated here. So, in case 2, x becomes 28 and y becomes 56. In case 3 nothing happens. In case 4, y becomes 55 and finally in default case x becomes 33. | + | || (x--, y++) will return the value of y which is 1, as comma operator always returns the right value. Hence, switch case starts with 1. Before starting the switch case, x is decremented and y incremented also. So, in case 1, x is 7 and y is 2. x is changed to 56 in case 1. Because of no break, all cases are evaluated here. So, in case 2, x becomes 28 and y becomes 56. In case 3 nothing happens. In case 4, y becomes 55 and finally in default case x becomes 33. |
+ | {'''What will be the output??''' | ||
+ | <syntaxhighlight lang="c"> | ||
+ | #include <stdio.h> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int i = 3, j = 6, k; | ||
+ | k = (i++ * j, ++i * j); | ||
+ | printf("%d",k); | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |type="()" | ||
+ | /} | ||
+ | -Undefined behavior | ||
+ | -Compiler dependent | ||
+ | +30 | ||
+ | -24 | ||
+ | ||comma operator always returns the right operand and it also forms a sequence point (comma in function parameter list is actually just a separator and not comma operator thus does not form a sequence point). Hence post increment of i is done before comma operator starts and then pre increment is done and finally i (5) is multiplied by j (6) giving 30. | ||
+ | |||
+ | {'''What will be the output??''' | ||
+ | <syntaxhighlight lang="c"> | ||
+ | #include <stdio.h> | ||
+ | |||
+ | |||
+ | int main() { | ||
+ | |||
+ | int i = 5; | ||
+ | i = (++i,i++,i) ; | ||
+ | printf(" i = %d",i); | ||
+ | |||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |type="()" | ||
+ | /} | ||
+ | -6 | ||
+ | -Undefined behaviour | ||
+ | +7 | ||
+ | -5 | ||
+ | ||comma operator always returns the right operand and it also forms a sequence point (comma in function parameter list is actually just a separator and not comma operator thus does not form a sequence point). Hence pre-increment of i is done, then post increment and finally i is evaluated to 7. (Between each increment a sequence point is formed by , and hence no undefined behavior) | ||
+ | |||
+ | {'''What will be the output??''' | ||
+ | <syntaxhighlight lang="c"> | ||
+ | #include <stdio.h> | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | int i = 5; | ||
+ | i = (i,++i,++i); | ||
+ | printf(" i = %d",i); | ||
+ | |||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |type="()" | ||
+ | /} | ||
+ | -6 | ||
+ | +Undefined behaviour | ||
+ | -7 | ||
+ | -5 | ||
+ | ||comma operator always returns the right operand and it also forms a sequence point (comma in function parameter list is actually just a separator and not comma operator thus does not form a sequence point). Hence first pre increment of i is done and then again pre increment is done. But between the second pre increment and the assignment operation there is no sequence point (like i = ++i). So, this produces undefined behaviour. | ||
+ | |||
+ | {'''What will be the output??''' | ||
+ | <syntaxhighlight lang="c"> | ||
+ | #include <stdio.h> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int x=1,y=2; | ||
+ | x=((x>0) && (y=6 ? printf("TRUE"): printf("FALSE"))); | ||
+ | printf("\nx = %d y = %d",x, y); | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |type="()" | ||
+ | /} | ||
+ | -True<br>x = 1 y = 4 | ||
+ | -False<br>x = 0 y = 6 | ||
+ | +True<br>x = 1 y = 4 | ||
+ | -False<br>x = 1 y = 6 | ||
+ | ||x=((x>0) && (y=6 ? printf("TRUE"): printf("FALSE")));<br>After precedence rule<br>x=((x>0) && (y=(6 ? printf("TRUE"): printf("FALSE"))));<br>printf returns the number of characters printed. So, here it will print TRUE and returns 4 which is assigned to y and is also returned as the second operand for &&. Since, both the operands of && are non-zero, x is assigned 1. | ||
+ | |||
+ | |||
</quiz> | </quiz> |
<quiz display="simple"> {What will be the output?? <syntaxhighlight lang="c">
int main() {
int x = 8, y = 1; switch(x--, y++) { case 1: x*=8; case 2: y*= x/=2; case 3: case 4: y--; default: x+=5; } printf("%d %d", x, y);
}
</syntaxhighlight> |type="()" /} -64 2 -64 1 +33 55 -33 56 || (x--, y++) will return the value of y which is 1, as comma operator always returns the right value. Hence, switch case starts with 1. Before starting the switch case, x is decremented and y incremented also. So, in case 1, x is 7 and y is 2. x is changed to 56 in case 1. Because of no break, all cases are evaluated here. So, in case 2, x becomes 28 and y becomes 56. In case 3 nothing happens. In case 4, y becomes 55 and finally in default case x becomes 33.
{What will be the output?? <syntaxhighlight lang="c">
int main() {
int i = 3, j = 6, k; k = (i++ * j, ++i * j); printf("%d",k);
}
</syntaxhighlight> |type="()" /} -Undefined behavior -Compiler dependent +30 -24 ||comma operator always returns the right operand and it also forms a sequence point (comma in function parameter list is actually just a separator and not comma operator thus does not form a sequence point). Hence post increment of i is done before comma operator starts and then pre increment is done and finally i (5) is multiplied by j (6) giving 30.
{What will be the output?? <syntaxhighlight lang="c">
int main() {
int i = 5; i = (++i,i++,i) ; printf(" i = %d",i);
}
</syntaxhighlight> |type="()" /} -6 -Undefined behaviour +7 -5 ||comma operator always returns the right operand and it also forms a sequence point (comma in function parameter list is actually just a separator and not comma operator thus does not form a sequence point). Hence pre-increment of i is done, then post increment and finally i is evaluated to 7. (Between each increment a sequence point is formed by , and hence no undefined behavior)
{What will be the output?? <syntaxhighlight lang="c">
int main() {
int i = 5; i = (i,++i,++i); printf(" i = %d",i);
}
</syntaxhighlight> |type="()" /} -6 +Undefined behaviour -7 -5 ||comma operator always returns the right operand and it also forms a sequence point (comma in function parameter list is actually just a separator and not comma operator thus does not form a sequence point). Hence first pre increment of i is done and then again pre increment is done. But between the second pre increment and the assignment operation there is no sequence point (like i = ++i). So, this produces undefined behaviour.
{What will be the output?? <syntaxhighlight lang="c">
int main() {
int x=1,y=2; x=((x>0) && (y=6 ? printf("TRUE"): printf("FALSE"))); printf("\nx = %d y = %d",x, y);
}
</syntaxhighlight>
|type="()"
/}
-True
x = 1 y = 4
-False
x = 0 y = 6
+True
x = 1 y = 4
-False
x = 1 y = 6
||x=((x>0) && (y=6 ? printf("TRUE"): printf("FALSE")));
After precedence rule
x=((x>0) && (y=(6 ? printf("TRUE"): printf("FALSE"))));
printf returns the number of characters printed. So, here it will print TRUE and returns 4 which is assigned to y and is also returned as the second operand for &&. Since, both the operands of && are non-zero, x is assigned 1.
</quiz>
<quiz display="simple"> {What will be the output?? <syntaxhighlight lang="c">
int main() {
int x = 8, y = 1; switch(x--, y++) { case 1: x*=8; case 2: y*= x/=2; case 3: case 4: y--; default: x+=5; } printf("%d %d", x, y);
} </syntaxhighlight> |type="()" /} -64 2 -64 1 +33 55 -33 56 || x--, y++ will return the value of y which is 1, as comma operator always returns the right value. Hence, switch case starts with 1. Before starting the switch case, x is decremented and y incremented also. So, in case 1, x is 7 and y is 2. x is changed to 56 in case 1. Because of no break, all cases are evaluated here. So, in case 2, x becomes 28 and y becomes 56. In case 3 nothing happens. In case 4, y becomes 55 and finally in default case x becomes 33.
</quiz>