<story><title>Should you learn C to “learn how the computer works”?</title><url>https://words.steveklabnik.com/should-you-learn-c-to-learn-how-the-computer-works</url></story><parent_chain><item><author>XMPPwocky</author><text>Here&amp;#x27;s the C99 standard:&lt;p&gt;&lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;www.open-std.org&amp;#x2F;jtc1&amp;#x2F;sc22&amp;#x2F;wg14&amp;#x2F;www&amp;#x2F;docs&amp;#x2F;n1256.pdf&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;www.open-std.org&amp;#x2F;jtc1&amp;#x2F;sc22&amp;#x2F;wg14&amp;#x2F;www&amp;#x2F;docs&amp;#x2F;n1256.pdf&lt;/a&gt;&lt;p&gt;There are no occurrences of the words &amp;quot;stack&amp;quot; or &amp;quot;heap&amp;quot; in this document.&lt;p&gt;What the spec actually discusses is &amp;quot;storage durations&amp;quot;. Now, in many cases you can say, well, &amp;quot;automatic storage duration&amp;quot; means it&amp;#x27;s on the stack, but that&amp;#x27;s not something C has any opinions about.&lt;p&gt;If you want to know about the stack and the heap, saying &amp;quot;learn C, and then learn how these abstract C concepts map onto the stack and the heap&amp;quot; might not actually be the best way to figure this stuff out.&lt;p&gt;What actually ends up happening, in my experience, is that to figure C out you have to get a decent mental model of how the stack and heap work, and then, from that, say &amp;quot;automatic storage duration and malloc&amp;#x2F;free are basically just the stack and the heap&amp;quot;.&lt;p&gt;I learned calculus as a kid because I needed it for an online electronics class I was taking. But I&amp;#x27;m not going to recommend that folks take electronics classes so they learn calculus! (that said- having a motivation to learn something, having an application in mind, a problem you want to solve... certainly seems to make learning easier.)</text></item><item><author>ashleyn</author><text>C at the very least teaches the difference between stack and heap memory, a crucial concept obscured by most higher-level languages.</text></item><item><author>ddevault</author><text>C doesn&amp;#x27;t necessarily teach you how computers work. But it does teach you how software works. Our modern software empire is built on mountains of C, and deference to C is pervasive throughout higher-level software design.&lt;p&gt;&amp;gt;You may have heard another slogan when talking about C: “C is portable assembler.” If you think about this slogan for a minute, you’ll also find that if it’s true, C cannot be how the computer works: there are many kinds of different computers, with different architectures&lt;p&gt;As with many things, this phrase is an approximation. C is a portable implementation of many useful behaviors which map reasonably closely to tasks commonly done on most instruction sets. C programmers rarely write assembly to optimize (usually to capture those arch-specific features which are unrepresentable in C), but programmers in other languages often reach for C to write the high-performance parts of their code. The same reason C programmers in the early days would reach for assembly is now the reason higher-level programmers reach for C.</text></item></parent_chain><comment><author>wahern</author><text>Debates about the &amp;quot;stack&amp;quot; rage every couple of years on comp.lang.c. Invariably people conflate two different meanings: (1) an abstract data structure with LIFO ordering semantics, and (2) #1 implemented using a contiguous block of [virtual] memory.&lt;p&gt;The semantics of automatic storage necessarily imply #1, but they do not require #2. Indeed, there are widely used implementations which implement #1 using linked lists (e.g. IBM mainframes, and GCC&amp;#x27;s split stacks or clang&amp;#x27;s segmented stacks).&lt;p&gt;Similarly, function recursion semantics necessarily imply #1 for restoring execution flow, but not #2. In addition to the examples above, so-called shadow stacks are on the horizon which maintain two separate stacks, one for function return addresses and another for data.[1]  In the near future a single contiguous stack may be atypical.&lt;p&gt;[1] Some variations might mix data and return addresses if the compiler can prove safe object access. Or the shadow stack may simply contain checksums or other auxiliary information that can be optionally used, preserving ABI compatibility.</text></comment>
<story><title>Should you learn C to “learn how the computer works”?</title><url>https://words.steveklabnik.com/should-you-learn-c-to-learn-how-the-computer-works</url></story><parent_chain><item><author>XMPPwocky</author><text>Here&amp;#x27;s the C99 standard:&lt;p&gt;&lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;www.open-std.org&amp;#x2F;jtc1&amp;#x2F;sc22&amp;#x2F;wg14&amp;#x2F;www&amp;#x2F;docs&amp;#x2F;n1256.pdf&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;www.open-std.org&amp;#x2F;jtc1&amp;#x2F;sc22&amp;#x2F;wg14&amp;#x2F;www&amp;#x2F;docs&amp;#x2F;n1256.pdf&lt;/a&gt;&lt;p&gt;There are no occurrences of the words &amp;quot;stack&amp;quot; or &amp;quot;heap&amp;quot; in this document.&lt;p&gt;What the spec actually discusses is &amp;quot;storage durations&amp;quot;. Now, in many cases you can say, well, &amp;quot;automatic storage duration&amp;quot; means it&amp;#x27;s on the stack, but that&amp;#x27;s not something C has any opinions about.&lt;p&gt;If you want to know about the stack and the heap, saying &amp;quot;learn C, and then learn how these abstract C concepts map onto the stack and the heap&amp;quot; might not actually be the best way to figure this stuff out.&lt;p&gt;What actually ends up happening, in my experience, is that to figure C out you have to get a decent mental model of how the stack and heap work, and then, from that, say &amp;quot;automatic storage duration and malloc&amp;#x2F;free are basically just the stack and the heap&amp;quot;.&lt;p&gt;I learned calculus as a kid because I needed it for an online electronics class I was taking. But I&amp;#x27;m not going to recommend that folks take electronics classes so they learn calculus! (that said- having a motivation to learn something, having an application in mind, a problem you want to solve... certainly seems to make learning easier.)</text></item><item><author>ashleyn</author><text>C at the very least teaches the difference between stack and heap memory, a crucial concept obscured by most higher-level languages.</text></item><item><author>ddevault</author><text>C doesn&amp;#x27;t necessarily teach you how computers work. But it does teach you how software works. Our modern software empire is built on mountains of C, and deference to C is pervasive throughout higher-level software design.&lt;p&gt;&amp;gt;You may have heard another slogan when talking about C: “C is portable assembler.” If you think about this slogan for a minute, you’ll also find that if it’s true, C cannot be how the computer works: there are many kinds of different computers, with different architectures&lt;p&gt;As with many things, this phrase is an approximation. C is a portable implementation of many useful behaviors which map reasonably closely to tasks commonly done on most instruction sets. C programmers rarely write assembly to optimize (usually to capture those arch-specific features which are unrepresentable in C), but programmers in other languages often reach for C to write the high-performance parts of their code. The same reason C programmers in the early days would reach for assembly is now the reason higher-level programmers reach for C.</text></item></parent_chain><comment><author>drb91</author><text>People don’t learn C by learning the spec, and virtually every c implementation and runtime I know, it uses a stack and heap.&lt;p&gt;Oddly you seem to recognize this, so I’m not sure what your point is.</text></comment>