(Created page with " <quiz display="simple"> {'''What will be the output??''' <syntaxhighlight lang="c"> #include <stdio.h> #include <string.h> int main() { int x = 8, y = 1; switch(x--, y...")
 
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.
 
   
 
   
 
</quiz>
 
</quiz>

Revision as of 12:47, 10 June 2014

<quiz display="simple"> {What will be the output?? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

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>

<quiz display="simple"> {What will be the output?? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

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>