http://www.makepovertyhistory.org

The strangest of switch statements

When compiled as C this is a perfectly valid piece of code, yet you would have to be a few sandwiches short of a pick nick basket to write it (or an evil genius).

#include "stdio.h"
int is_even(unsigned char c)
{
        return c & 0x01 ? 0: 1;
}

int main()
{
        unsigned char c =4;
        switch(c)
        {
                default:
                        if(is_even(c))
                case 2: case 4: case 6: case 8: case 10:
                                printf("is even\n");
                        else
                case 1: case 3: case 5: case 7: case 9:
                                printf("is not even\n");
        }
        return 0;
}

Leave a Reply