TITLE: Duff's device [ Here's a bit of twisted code for your amusement only. By the way, I tried this with the ObjectCenter compiler, which rejected it. -adc] AUTHOR: kuehl@uzwil (Dietmar Kuehl), 7 Sep 95 Here is the Duff's-Device as found in "The New Hackers Dictionary", E.Raymond, Addison-Wesley (it is not found in the jargon...): register int n = (count + 7) / 8; /* count > 0 assumed */ switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } However, here is a working C++ program which demonstrates the case James mentioned: #include #include int main(int ac, char **av) { int count = strlen(av[1]); int n = (count + 7) / 8; switch (count % 8) while (--n) { case 0: cerr << *av[1]++; case 7: cerr << *av[1]++; case 6: cerr << *av[1]++; case 5: cerr << *av[1]++; case 4: cerr << *av[1]++; case 3: cerr << *av[1]++; case 2: cerr << *av[1]++; case 1: cerr << *av[1]++; } cerr << endl; return 0; } [...] Here is the part of the entry in the New Hackers Dictionary which is of interest: Shocking though it appears to all who encounter it for the first time, the device is actually perfectly valid, legal C. C's default fall through in case statements has long been its most controversial single feature; Duff observed that "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against." RESPONSE: kanze@lts.sel.alcatel.de (James Kanze), 8 Sep 95 [...] Actually, I was thinking of a much simpler variant: void copy( char* dst , char const* src , int len ) { switch ( n % 4 ) while ( n > 0 ) { *dst ++ = *src ++ ; case 3: *dst ++ = *src ++ ; case 2: *dst ++ = *src ++ ; case 1: *dst ++ = *src ++ ; case 0: n -= 4 ; } } A couple of comments: 1. Obviously, changning len to unsigned will have disasterous consequences. 2. So will adding a break after case 0 (or anywhere else, for that matter). 3. I wouldn't recommend calling the function with len<0, either. In sum, the whole thing is very fragile. The only legitimate use of this construct I can think of is for showing just how bad the semantics of the C switch statement really are. [...]