TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Weekend projects: getting silly with C

238 pointsby nothacking_11 months ago

16 comments

quietbritishjim11 months ago
&gt; The above example will print the value of a, but it won’t be initialized to 123!<p>It certainly could do though. In C, using an uninitialised variable does <i>not</i> mean &quot;whatever that memory happened to have in it before&quot; (although that is a potential result). Instead, it&#x27;s undefined behaviour, so the compiler can do what it likes.<p>For example, it could well unconditionally initialise that memory to 123. Alternatively, it could notice that the whole snippet has undefined behaviour so simply replace it with no instructions, so it doesn&#x27;t print anything at all. It could even optimise away the return that presumably follows that code in a function, so it ends up crashing or doing something random. It could even optimise away the instructions <i>before</i> that snippet, if it can prove that they would only be executed if followed by undefined behaviour – essentially the undefined behaviour can travel back in time!
评论 #40836623 未加载
评论 #40842439 未加载
评论 #40836838 未加载
JonChesterfield11 months ago
This features the construct<p><pre><code> switch(k) { if (0) case 0: x = 1; if (0) case 1: x = 2; if (0) default: x = 3; } </code></pre> which is a switch where you don&#x27;t have to write break at the end of every clause.<p><pre><code> #define brkcase if (0) case </code></pre> That might be worth using. Compilers won&#x27;t love the control flow but they&#x27;ll probably delete it effectively.
评论 #40836365 未加载
评论 #40836437 未加载
评论 #40837500 未加载
geon11 months ago
This can be used to implement coroutines in C. <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;24202890&#x2F;switch-based-coroutines" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;24202890&#x2F;switch-based-co...</a>
评论 #40836675 未加载
nj5rq11 months ago
Why did I not know that this:<p><pre><code> case 1 ... 10: </code></pre> Is valid C? I have been programming in C for years, what standard is this from?
评论 #40837554 未加载
评论 #40837568 未加载
jftuga11 months ago
This reminds me of some silly C code I once wrote for fun, which counts down from 10 to 1:<p><pre><code> #include &lt;stdio.h&gt; &#x2F;&#x2F; compile &amp; run: gcc -Wall countdown.c -o countdown &amp;&amp; .&#x2F;countdown int n = 10; int main(int argc, char *argv[]) { printf(&quot;%d\n&quot;, n) &amp;&amp; --n &amp;&amp; main(n, NULL); } </code></pre> Python version:<p><pre><code> import sys # run: python3 countdown.py 10 def main(n:int): sys.stdout.write(f&quot;{n}\n&quot;) and n-1 and main(n-1) main(int(sys.argv[1])) </code></pre> Shell version:<p><pre><code> # run .&#x2F;countdown.sh 10 echo $1 &amp;&amp; (($1-1)) &amp;&amp; $0 $(($1-1))</code></pre>
评论 #40837434 未加载
评论 #40836937 未加载
teo_zero11 months ago
Another source of surprise:<p><pre><code> 4[arr] &#x2F;&#x2F; same as arr[4]</code></pre>
评论 #40836317 未加载
评论 #40839074 未加载
codext11 months ago
The final obfuscated code snippet in the article brought to light another GCC extension:<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;34559705&#x2F;ternary-conditional-operator-without-the-middle-expression" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;34559705&#x2F;ternary-conditi...</a>
smusamashah11 months ago
Found these silly tricks by the author of this blog on twitter first. Switch statement can do loops too <a href="https:&#x2F;&#x2F;twitter.com&#x2F;lcamtuf&#x2F;status&#x2F;1807129116980007037" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;lcamtuf&#x2F;status&#x2F;1807129116980007037</a>
评论 #40835828 未加载
mgaunard11 months ago
aren&#x27;t the switch shenanigans important to the duff&#x27;s device?
评论 #40836168 未加载
o11c11 months ago
Due to the way lifetimes work in C (they begin with the block, not the declaration), the following is legal:<p><pre><code> #include &lt;stdio.h&gt; #include &lt;stddef.h&gt; int main() { { int *p = NULL; if (p) { what: printf(&quot;a = %d\n&quot;, *p); return 0; } int a = 123; p = &amp;a; goto what; } }</code></pre>
junon11 months ago
&gt; switch (i) case 1: puts(&quot;i = 1&quot;);<p>I&#x27;ve seen this in the wild, particularly with macros.<p><pre><code> #define assert(c) if (!c) ... if (foo) assert(...); else bar(); &#x2F;&#x2F; oops!</code></pre>
pdimitar11 months ago
<i>Fun at parties alert:</i><p>Let&#x27;s stop getting silly with C, too many CVEs!<p>---<p><i>Serious comment:</i><p>It&#x27;s a rather cool article actually. Not something I&#x27;d do daily but it&#x27;s kind of sort of useful to know these techniques.
drzzhan11 months ago
I am so lost at the final block of code. Does every C developer have to deal with this everyday?
评论 #40854756 未加载
评论 #40852629 未加载
fanf211 months ago
see also <a href="https:&#x2F;&#x2F;www.chiark.greenend.org.uk&#x2F;~sgtatham&#x2F;mp&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.chiark.greenend.org.uk&#x2F;~sgtatham&#x2F;mp&#x2F;</a><p>Metaprogramming custom control structures in C by Simon Tatham
评论 #40837882 未加载
nxobject11 months ago
If only there was a way of using setjmp&#x2F;longjmp-style contexts instead of goto, un&#x2F;winding the stack as required. So we could travel around in time... unfortunately you can&#x27;t work with a setjmp buffer before it&#x27;s actually created, unlike gotos.
评论 #40840443 未加载
JohnMakin11 months ago
My undergrad was entirely in the C language and I’m very glad for it. Sometimes more modern languages can throw me for a loop, no pun intended, but the beauty (and horror) of C is that you are pretty close to the metal, it’s not very abstracted at all, and it allows you a lot of freedom (which is why it’s so foot gunny).<p>I will never love anything as much as I love C, but C development jobs lie in really weird fields I’m not interested in, and I’m fairly certain I am not talented enough. I have seen C wizardry up close that I know I simply cannot do. However, one of the more useful exercises I ever did was implement basic things like a file system, command line utilities like ls&#x2F;mkdir etc. Sometimes they are surprisingly complex, sometimes no.<p>After you program in C for a while certain conventions meant to be <i>extra</i> careful kind of bubble up in languages in a way that seems weird to other people. for example I knew a guy that’d auto reject C PR’s if they didn’t use the syntax if (1==x) rather than if (x==1). The former will not compile if you accidentally use variable assignment instead of equality operator (which everyone has done at some point).<p>This tendency bites me a lot in some programming cultures, people (ime) tend to find this style of programming as overly defensive.
评论 #40836494 未加载
评论 #40836505 未加载
评论 #40836297 未加载
评论 #40836842 未加载
评论 #40837563 未加载
评论 #40836173 未加载
评论 #40836202 未加载