The Old Switcheroo
I recently learned (the hard way, as usual) something about the Switch statement in C. I had a part of code which I want executed for all the cases in the switch statement. So I naively put that statement just above the first case statement inside the switch structure.
Take a look at this simplified version of the code:
b = 10
is the common code that I want executed for all the values of i
. To my surprise, the above program returned an output of 100 and the worst part is gcc does not throw any warnings even with the -Wall
option.
Solution is quite simple. Move the b = 10
statement above the switch statement. Don’t know if it is a quirk of the language or if it is expected. I will provide an update if I find any more details.