5 ms·
switch (n % 2) { case 0: do { ++i; case 1: ++i; } while (--n > 0); } Someone is re
by golergka 2y ago
switch (n % 2) {
case 0:
do {
++i;
case 1:
++i;
} while (--n > 0);
}
Someone is really ought to record a "WAT" video about C.
- lifthrasiir 2y agoJust turn every single IOCCC winning entry into a video, you now have a year's supply of contents.
- mananaysiempre 2y agoThe switch statement in C is not a very limited pattern match. The switch statement in C is a very ergonomic jump table. Do not think ML’s case-of with only integer literals for patterns; think FORTRAN’s computed GO TO with better syntax. And it will cease to be a WAT. (For a glimpse of the culture before pattern matching was in programmers’ collective consciousness, try the series on designing a CASE statement for Forth that ran for several issues of Forth Dimensions.)
- russellbeattie 2y agoI don't think there's any confusion of how it works, it's the deep horror in discovering that it's possible in the first place, and a morbid curiosity of the chaos it could cause if abused.
- mananaysiempre 2y agoAt least for me, the feelings you describe are characteristic of a footgun, not a WAT. A WAT is rather a desperate bewilderment as to who could ever design the thing that way and why, and for switch statements computed gotos are the answer to that question. As for the footgun issue, I mean, it could be one in theory, sure. But I don’t think I’ve ever seen it actually fired. And I can’t really appreciate the Javaesque “abuse” thinking—it is to some extent the job of the language designer to prevent the programmer from accidentally doing something bad, but I don’t see how it is their job to prevent a programmer from deliberately doing strange things, as long as the result looks appropriately strange as well. (There are reasons to dislike C’s switch statement, I just don’t think the potential for “abuse” is one.)
- deleted 2y ago[deleted]
- PhilipRoman 2y agoJust think of the "case" statements like any other label, despite the misleading indentation. Then it becomes perfectly natural to jump in the middle of a loop.
- rramadass 2y agoRight; This is how i worked out "Duff's Device". Wikipedia has a very helpful "Simplified Explanation" - https://en.wikipedia.org/wiki/Duff%27s_device#Simplified_explanation https://en.wikipedia.org/wiki/Duff%27s_device#Simplified_exp...
- rramadass 2y agoHere is an example : https://news.ycombinator.com/item?id=41670144 https://news.ycombinator.com/item?id=41670144
- agumonkey 2y agoI wonder if there's any other instance (in programming or else) of intersecting grammar constructs being accepted.
- 082349872349872 2y agocontrol "structures" in forth, although in this case the notion of "grammar construct" is more in the head of the user than in the implementation...
- agumonkey 2y agoyeah that's too loose :D
- tom_ 2y agoThis sort of thing is pretty handy sometimes. Don't forget you can have code (e.g., start of the loop) before any of the cases too!
- pjmlp 2y agoC definitly belongs into the set of WAT languages.
- rramadass 2y agoThis is actually pretty useful in some usecases. One very good example is Simon Tatham's "Coroutines in C" (https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html) to resume execution in a function after the point it returned from in the earlier call. The relevant example code is; int function(void) { static int i, state = 0; switch (state) { case 0: goto LABEL0; case 1: goto LABEL1; } LABEL0: /* start of function */ for (i = 0; i < 10; i++) { state = 1; /* so we will come back to LABEL1 */ return i; LABEL1:; /* resume control straight after the return */ } } becomes; int function(void) { static int i, state = 0; switch (state) { case 0: /* start of function */ for (i = 0; i < 10; i++) { state = 1; /* so we will come back to "case 1"*/ return i; case 1:; /* resume control straight after the return */ } } }