https://lcamtuf.substack.com/p/weekend-projects-getting-silly-with [https] lcamtuf's thing SubscribeSign in Share this post [https] Weekend projects: getting silly with C lcamtuf.substack.com Copy link Facebook Email Note Other Weekend projects: getting silly with C C is beautiful yet full of horrors. Here's to uncovering its hidden depths. Jun 30, 2024 24 Share this post [https] Weekend projects: getting silly with C lcamtuf.substack.com Copy link Facebook Email Note Other 5 Share For all its warts, the C language is a marvelous thing. It is remarkably simple, yet somehow expressive enough to allow entire operating systems to be written with ease. Just as curiously, its terse, minimalistic syntax became the way to structure code -- copied by nearly all of its mainstream successors, from Java to Go. Among geeks, the syntax can also be credited for the emergence of code obfuscation as an art form. The IOCCC contest is perhaps the best-known outlet for this craft; a typical IOCCC submission looks like this: #define q [v+a] #define c b[1] #define O 1 q #define o 0 q #define r(v,a\ )v<0&&( v*=-1, a*=-1); #define p(v,m, s,w)*c==*#v?2 q\ int main() { int i = 1; switch (i) if (0) case 0: puts("i = 0"); else if (0) case 1 ... 10: puts("i = 1 ... 10"); else if (0) case 11: puts("i = 11"); else if (0) default: puts("i = something else"); return 0; } And who needs switch (...), anyway? The && operator is a longstanding GNU extension that lets you get an address of a label; you can then goto to that address. Equipped with this knowledge, you can make your own switch (...) --with blackjack, et cetera (link): #include int main() { int i = 1; goto *(void*[]){ &&case_0, &&case_1, &&case_2 }[i]; if (0) case_0: puts("i = 0"); if (0) case_1: puts("i = 1"); if (0) case_2: puts("i = 2"); return 0; } Heck, here's another fantastic use for &&: why bother with for (...) if you can use labels to implement loops directly within variable declarations? Check this out (link): #include int main() { /* Iterate from i = 0 to i = 5: */ int i = (i = 0) & ({_:0;}) | printf("i = %d\n", i) * (++i > 5) ?: ({goto *&&_;0;}); return 0; } This last snippet is probably not UB-safe and is GCC-specific. But the point stands: you can write completely alien and befuddling code in C without making it unreadable. If you liked this article, please subscribe! Unlike most other social media, Substack is not a walled garden and not an addictive doomscrolling experience. It's just a way to stay in touch with the writers you like. [ ] Subscribe For a thematic catalog of posts on this site, click here. 24 Share this post [https] Weekend projects: getting silly with C lcamtuf.substack.com Copy link Facebook Email Note Other 5 Share 5 Comments [https] [ ] Iustin Pop 3 hrs ago "... without making it unreadable." [https] Hah, that's what you think! Expand full comment Reply Share Chris Quackenbush 7 hrs ago This is a hilarious technique that implements coroutines [https] using similar switch tricks. https:// www.chiark.greenend.org.uk/~sgtatham/coroutines.html Expand full comment Reply Share 3 more comments... Top Latest Discussions No posts Ready for more? [ ] Subscribe (c) 2024 lcamtuf Privacy [?] Terms [?] Collection notice Start WritingGet the app Substack is the home for great culture Share Copy link Facebook Email Note Other This site requires JavaScript to run correctly. Please turn on JavaScript or unblock scripts