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.

Tearing apart printf()

231 pointsby necrodomealmost 7 years ago

10 comments

vinaymsalmost 7 years ago
This is a poorly written article that is convoluted and made unnecessarily complex. This is similar to writing an article titled &#x27;tearing apart google search&#x27; and then dwelling on HTTP, TCP&#x2F;IP, routers etc instead of actually talking about how google search works.<p>I was expecting the article to start off by &#x27;tearing apart&#x27; the source code, then dig into the algorithms with which diverse formats are rendered, then compare a few popular implementations and finally provide valuable insights, something on the lines of what WalterBright mentioned and the brief discussion it started. If not all this, the minimum I expected was an abridged, annotated source code.<p>This article left me underwhelmed.
评论 #17118191 未加载
WalterBrightalmost 7 years ago
In the 1980s, Borland made up for much of the poor code generation of their C compiler by doing the runtime library all in hand-optimized assembler, including printf.<p>What I did (Datalight, Zortech) was make large disk I&#x2F;O buffers. It gave huge boosts to speed, and very few people cottoned on to that :-)<p>It was a technique I learned from Shal Farley.
评论 #17117687 未加载
评论 #17115611 未加载
userbinatoralmost 7 years ago
<i>Possible trap: Some compilers may recognize the &#x27;1&#x27; literal used in printf1.c, fold it in to the string, and avoid printf() in both cases. If that happens to you, substitute an expression that must be evaluated.</i><p>Or more &quot;robustly&quot;, an expression that comes from external input (such as getchar()). Compilers can be smart enough to evaluate expressions which are constant.<p>This reminds me, I briefly played around with JIT&#x27;ing printf() itself --- if you look at it the right way, it&#x27;s basically a &quot;bytecode&quot; interpreter for the format string. I did get some pretty good performance gains, but it turned out simpler for the project it was intended to go in to just switch to a binary protocol instead of text for output.<p>I also think that writing a (subset of) printf() is a useful exercise for those beginning programming C in general; it&#x27;s not too difficult (at least if you avoid the floating-point stuff...), but also not too easy either.
评论 #17116574 未加载
评论 #17116355 未加载
microtherionalmost 7 years ago
Back in the day, I found P.J. Plauger&#x27;s &quot;The Standard C Library&quot; immensely instructive, as it contains a full implementation of the (then) standard library.<p>Admittedly, the book is more than 25 years old now, but I&#x27;m sure it would still make an interesting read:<p><a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Standard-C-Library-P-J-Plauger&#x2F;dp&#x2F;0131315099&#x2F;ref=la_B00IP4FPWQ_1_16?s=books&amp;ie=UTF8&amp;qid=1526871663&amp;sr=1-16&amp;refinements=p_82%3AB00IP4FPWQ" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Standard-C-Library-P-J-Plauger&#x2F;dp&#x2F;013...</a>
评论 #17116485 未加载
saagarjhaalmost 7 years ago
&gt; %n - null<p>&gt; %% - complete form<p>Wait, what? %n writes the current number of characters outputted to the int * you specify, while %% just prints a percent sign.
评论 #17115296 未加载
maxk42almost 7 years ago
The most fascinating thing about this article for me was the copyright notice at the bottom of the page consisting primarily of asterisks and hash marks.<p>Can anyone explain this to me?
评论 #17116969 未加载
SilasXalmost 7 years ago
Very, very interesting deep dive. I reminds me of the nand2tetris.org course, which does something very similar (albeit for a toy hardware&#x2F;OS&#x2F;language): explain what the heck is going on between a print command and something being written to the screen.<p>In nand2tetris, they work from a hardware simulation that represents memory where a block of that memory determines what the pixels on the screen are set to. Then you write code that can turn the pixels on or off, then a character OS library that converts binary ascii values into a combination of pixel settings that display a character.<p>Then you implement your OS&#x27;s program loop (the &quot;OS&quot; is based around a one-shot &quot;here a bunch of functions, run from an entry point&quot; that you have to specify each time you turn it on) which has a specified entrypoint from which the rest is called.<p>Obviously, modern systems are a lot faster, but it was a great illustration of what has to happen in between, which this article is doing for real OSes.
abhishekjhaalmost 7 years ago
I hope the bitwise project helps me see these things a little more clearly. Implementing something from ground up always feels nice.
ktpsnsalmost 7 years ago
How can compilers optimize a printf() call without format strings involved towards a simple puts() call? Sure it would be easy in C++, roughly like<p><pre><code> inline void printf(const char* const s) { puts(s); } void printf(const char* const s, ...); &#x2F;* non-trivial implementation *&#x2F; </code></pre> but that&#x27;s not possible in C.
评论 #17117349 未加载
cedricvanrompayalmost 7 years ago
Newbie question: is there one context switch per line in the &quot;printf() execution sequence&quot; figure?
评论 #17116928 未加载