If ‘Hello World’ is the first program for C students, then printf() is probably the first function. I’ve had to answer questions about printf() many times over the years, so I’ve finally set aside time for an informal writeup. The common questions fit roughly in to two forms:
Easy: How does printf mechanically solve the format problem?
Complex: How does printf actually display text on my console?
My usual answer? “Just open up stdio.h and track it down”
This wild goose chase is not only a great learning experience, but also an interesting test for the dedicated beginner. Will they come back with an answer? If so, how detailed is it? What IS a good answer?
This is incredibly detailed and definitely over my head, but I’m sure many of you will enjoy this one greatly.
Detailed is one word for it. Why does it that everyone think they have to start their articles as if you have just discovered computers? You have to scroll down halfway through the article before he stopped talking about everything other than “tearing apart” and start digging into some low level stuff. Why does he think that he must first explaine that a C function is something you type, compile, link and execute? One thing if you say “tearing down ls”, and explain that ls is a C program, but explaining what a C function is? Who is your target audience? Does he actually think anyone program in C, even a “hello world” without actually knowing those basics? “Step one, Include the C header” Is this a joke?
If anyone is interested, search for “Step 5 – Loader prepares the run-time”. Anything above that is pure, slightly insulting, drivel.
The “usual answer” of telling to open up a C header file is actually, in general, really poor, because the header file isn’t guaranteed to contain 1) any useful code 2) any hints where the actual code is located on the disk. Therefore the students could only receive a big “fuck you” sign upon spending some time to locate the header file in the first place…
The final line “Last (and definitely least): Untangling the mess inside vfprintf” is what I assumed this whole article was about. It spent the whole time building up to something and didn’t get to its destination.
Fwiw, I wrote one of these and I’d encourage anyone who wants to understand it to do the same. varargs is a bit goofy, and has some strange quirks like arguments always get pushed onto the stack as a multiple of machine word size, so printf has to pop off a machine word even when printing a single char, etc.
One (of the many) unfortunate things about the whole printf design is because the format string is evaluated at runtime the linker ends up having to throw in all the code to format every format type even if the program doesn’t use them. I was actually impressed reading the article that gcc is smart enough to remove printf calls when no format specifiers are present – that’s pretty cute, although confusing as all get out, clearly blurring the architectural line between the compiler and libraries.