Sunday, August 2, 2009

What will output of this c program and how???

#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int i,k;


for(i=0;i%26lt;10;i++) {


switch(i % 5)


{ case 0: k=0;


case 1:k++;


printf("k1=%d\n",k);


case 2:k+=2;


printf("\nk2=%d\t\t",k);


case 3:k+=3;


printf("\nk3=%d\t\t");


case 4:k+=4;


default :k+=10;





}


}


printf("%d",k);


}

What will output of this c program and how???
output is


k1=1 %26lt;-- i = 0





k2=3 %26lt;-- this get output cause of no break statement


k3=6(tab)(tab)k1=21 %26lt;-- i =1





k2=23


k3=26(tab)(tab) %26lt;-- i = 2


k2=42


k3=45


k3=62(tab)(tab) %26lt;-- i = 3


k1=1 %26lt;-- i = 5





k2=3


k3=6(tab)(tab)k1=21 %26lt;-- i =6





k2=23


k3=26(tab)(tab) %26lt;-- i = 7


k2=42


k3=45


k3=62(tab)(tab)90 %26lt;-- i = 8


If you added break statements before the next case statement or default: statement, you will get different result.


and less confusing output.
Reply:Are you sure guys? O_o I think that's a modulo operation over there at switch(i % 5) statement, not a division..





My guess is this:


At the first loop it will encounter the "case 0" statement and set the "k" value to 0. But since there is no break in the statement, it will "roll over" to the each of the continuing statements. In the end, it will also satisfy the "default" statement, causing the value of "k" to be incremented to 10.





Second loop, it would satisfy the statement "case1" and print out "k1=11". But at the end it would be incremented yet again by 10 (to 21).





Third loop, "case 2", print out "k2=23", k=33 at the end.





Fourth loop, "case 3", print out "k3=36", k=46 at the end.





Fifth loop, "case 4", k=50, k=60 at the end.





Sixth loop, back to "case 1"... And so on until the tenth loop is the same with the fifth loop.





So, at the end it would print out 60. As the previous answerer had pointed out, you might want to include a "break" at every end of the "case" statement. In addition, there is some confusion at your output formatting, making the print out rather ugly.





In summary, the program would output:


k1=11





k2=23


k3=36 k1=11





k2=23


k3=36 60





==============


EDIT: Four people with four different answers, I bet you're confused right now which one is the right one. ;) So I decide to run the code through the compiler and found out that the right dude is Dasher Dude. Congrats dude, you're a dasher. ^_^





Ok, enough with wordplays. =p There is just one small mistake from the code that I just realized after reading the compiler's output:


printf("\nk3=%d\t\t"); is supposed to be printf("\nk3=%d\t\t", k); right?
Reply:The output will be


k1=1


k1=2


k1=3


k1=4


k1=5


k=5





Because first 4 values of i are less than 5 so answer of division will be 0 which will go to case 0. It is not having print statement


After this for numbers (i=5 to 9) the division will be 1, hence causes to execute case statement 1.which print 'k' value.


everytime value of k is incremented at every iteration.





But there must have "Break;" statement at end of every case.
Reply:k1=1





k2=3(tab)(tab) /*these tab characters will be invisible*/


k3=6(tab)(tab)14


/*k4 will not be printed*/





/* and you forgot to put the "break" statement, as the previous answerer said*/


No comments:

Post a Comment