This question pertains to computer science and standardization of ambiguous symbol and signal representation in programming languages in general, but I only know the very little C that I know, so I am posting here.
My goal with studying C is for it to be an entry point to the world of programming, so that I can move on to some assembly language down the road. Eventually, I want to understand where human abstraction begins/ends and hardware circuitry takes over and what that border looks like.
A few months ago, I was playing around with assigning hex values corresponding to the alphabetical characters that I wanted to have printed on screen and passing that to, say, printf. This was fun, because 1. it made my terminal look like a hacker’s screen (gibberish) in one of those generic “hacker” images and 2. it simply was interesting (and provocating) to realize how far away from the machine a function such as printf is. (Which lead me down the rabbit hole of looking through related libraries to see if I can find what printf does under the hood…)
A program I’m writing, which trims text from stdin by removing user specified characters, needed a little touch-up, so I looked at the code yesterday for the first time in a few months. I wanted to clear the screen when the program runs and when the program ends, giving it a “feel” of opening and closing a window, without diving into ncurses. (A colleague who is not at all technologically inclined complained about them not understanding what to do with the program, even though all instructions are displayed on screen. Maybe clearing whatever “garbage” is on screen before the program runs will help, I thought.) So last night I learned about yet another standardization: ANSI escape codes. It didn’t take long before I learned how to clear the screen with
printf("\x1b[1;1H\x1b[2J");
and I felt like this is for real now. I’m so cool. :3 :3 :3
But the following thoughts/questions still bother me:
-
Before I knew anything about programming or electronics, I only cared about one thing: something works or it doesn’t. Now that I know a teeny tiny bit of programming and what not, I can’t stop thinking about how brittle and arduous the journey must have been for the programmers and computer scientist during the 20th century before we had internationally acknowledged standardizations. Portability (or the lack thereof) much?!
-
I know that I am getting way ahead of my beginner self, but regardless of whether I’m using ANSI escape codes or hex entries in the ASCII table, I still feel like a “fraud” in relation to my ultimate goals. It still feels like a HUGE abstraction from what’s going on under the hood. Especially when I/we use standardized representations of signals like ANSI or ASCII. Or even the language C itself. How many layers does
printf("\x1b[1;1H\x1b[2J");pass before finally hitting the first conductor that leads to whatever transistor is first in line?
If you’re feeling too abstracted from the hardware, get an esp32 for $10. When you write C for the esp32, anything you don’t write yourself will be from an imported library where you can immediately see the source code. I’d recommend one of the learning kits that come with a breadboard, leds, etc so you don’t have to do any soldering or scrounge wires to get something more than the debug output. Or even better get an esp32 yellow display for $30 that comes with an lcd pre wired to the esp32 in an all in one package.
Modern build environments have so much automated that it’s hard to get to the source of what something like printf() is doing.
As to how things were before standards, in a way it was easier because you did whatever you wanted without worrying about how anyone else might do it.
I’m not a pro with that kind of low-level code but you can try https://godbolt.org/ to see the assembly code with various compilers. You can also compile your files with the
-Sflag likeclang -S file.corgcc -S file.c.Godbolt says that your call to printf is:
.LC0: .string "\033[1;1H\033[2J" "main": push rbp mov rbp, rsp mov edi, OFFSET FLAT:.LC0 mov eax, 0 call "printf" ...Wow, you call printf and the binary calls printf, pretty disappointing actually.
Anyway, with a Google (or DDG) search, you find this https://codebrowser.dev/glibc/glibc/stdio-common/printf.c.html which should give you a trail of the calls in the glibc (the GNU C library):
- printf (forget va_start and va_end, it’s to handle the parameters like %d)
- __vfprintf_internal => vfprintf (here https://codebrowser.dev/glibc/glibc/stdio-common/vfprintf-internal.c.html#1521)
- then a lot of magical stuff with a call to Xprintf_buffer and Xprintf which are suspiciously what you would want
- rinse, repeat
OR (I forgot that one), you can install the sources of the glibc, setup your IDE properly, and debug your application with a breakpoint on printf, and then enter the call all the way down. It will be harder though if you have never done that before but it’s the most precise way to know what happens on your own machine.(forget about it, it seems that you must debug the terminal which is more complicated)BTW, you are not a fraud, you are learning. And most developers have never done what you are doing, or debugged anything past a few function calls but never that far.
Last but not least, the internet says that the glibc only outputs the bytes to the terminal. It is the terminal itself that will interpret the values, and clear the screen in your case.
Everything I told you is kinda useless for the ANSI codes, sorry. But you can study the source code of a billion “terminal emulators” if you want to have fun. Here is a list: https://en.wikipedia.org/wiki/List_of_terminal_emulators. Be careful though, some are written in Rust or Zig, and not C as you want.
Because printf is linked, either statically or dynamically, the assembly has a lookup table that points to where the function actually is. If you fully assemble a statically linked program, you could find the printf code assembled too. If its a dynamic executable, printf is going to be loaded lazily at run time. Assembly is basically annoying to read C.
I really appreciate you taking your time! Do not apologize! I value your response regardless! :D
So, as if it wasn’t enough for me to know that these commonly used functions in C (printf in this example) are already quite high up the abstraction layers, the TERMINAL ITSELF sits in between and does some interpreting before the code is handed over to the OS? Great. (But seriously, this is so interesting, I can’t stop smiling by myself, sitting in a coffe shop LOL.)
Thanks again! PS: I’m only doing this as a hobby, so I of course have the utmost respect for those programmers that don’t have the time or capacity to be all academic about it.
the TERMINAL ITSELF sits in between and does some interpreting before the code is handed over to the OS?
Not exactly. Your code hands over the data to the os, which feeds it into a pipe or some other data structure, from which the terminal reads it. Then the terminal interprets the data and tells the os what pixels/images/data to draw where. At least to my knowledge. I wouldn’t be surprised if there are some optimizations or indirections in the path of the data to the screen.
You might also be interested in this video of computerphile about the abstractions below hello world: https://m.youtube.com/watch?v=ycl1VL0q1rs
Sweet! Didn’t know they have a video on that! :D Thanks!
The question about where abstraction begins/ends is a great one. I think its east to fall into the trap of thinking if you’re using an abstraction, you don’t really know what’s going on, which is true, but a lot of the time the point of the program is to do one thing, not to rewrite from the ground up. Abstractions are everywhere. That’s what contractors do, that’s what a baker does, its what an airplane pilot does. They all abstract stuff away for a final product. To answer your question about where the line of abstraction ends, it doesn’t. Because even when you get down to writing assembly, you’re still not wiring the functions in the arithmetic logic unit on the processor. And even if you WERE wiring the functions on the arithmetic logic unit, you still probably have to make sure it interfaces with the cache, which is probably abstracted to you because a separate team is working on it entirely.
I know this kind of beats around the bush with how many layers of abstraction it goes down. If you want to know what the first instruction that gets executed on in printf, you could always use
$CC -Sto compile the printf source to assembly. The only issue you may run into here is that a) printf may call another function in the process that you’d need to compile to assembly and b) printf definitely uses a syscall which means it interacts with the operating system, which means that a certain amount of printf is being executed as the operating system. If you want to peel back that layer, you would have to look at kernel code for how the kernel on your computer reacts to a write syscall and then passes execution back to printf. If you’re curious about a program, you can always usestraceto see what syscalls it makes.I know this kind of beats around the bush
Not at all! Holy crap, this gave me such a great perspective! Thanks! I’ll look into that
-Sflag andstraceprogram! 😊
I would say that curiosity to understand how things work is underrated. Most of the people I work with have no idea how something works, only that it does. They have no desire to dig deeper.
I had to explain to a dev much senior to me why having 12 MB sessions loaded from SQL on a web page was making the pages load slow. Every page load had to pull that blob down. He just couldn’t wrap his head around the Session framework slowing his app down when it had worked perfectly fine before. It took me attaching a profiler to his production app to show that 75% of compute time was the Session middleware.
Sic way of “sticking it to the man”! :D Yeah, my curiosity in genuine AF. I only have the ability to take SMALL steps at a time, but it’s fun. :)
I felt (and still feel, somewhat) a similar way as a young programmer. But I had the advantage of learning these things 30-40 years ago when the specifics of the hardware was much more relevant to programming, and software libraries and abstraction layers were smaller/less prevalent. Even so, I still had to track down out-of-date manuals and textbooks to find out why some things were so, and at least that should be a little easier for you now in 2026 with communities of old fogeys online getting together and documenting these things for posterity.
A lot of today’s low-level standards are actually based around the physical realities and quirks of popular hardware choices back in the day. People got used to interacting with the computer in a certain way, so that even once it became practical to move abstraction from the programming and compilation stages to runtime, the protocols were more or less already defined, with no great need to change anything.
You seem to be working a lot on drilling “from the top down”, but are you also looking at working “from the bottom up”? I found that this “two-front” approach, trying to figure out how the two sides “meet in the middle” often yielded the best (and fastest) results. As a child I read the Usborne Programming and Computing books, which gave me a great starting base. They’re all free online now, and while a lot of the details regarding specific home computers of the time are no longer useful, the fundamentals are still sound, and as I mentioned earlier, seeing how some of the old hardware was organized may help you see why certain modern standards are laid out as they are.
A more modern take on this, which I still found very interesting despite my familiarity with the subject, is Ben Eater’s series on building an 8-bit CPU from discrete components on YouTube.
It may help to look at programming for old home computers and game consoles, or microcontrollers as suggested by others, where your program is literally the only thing running, and there’s no operating system between you and the hardware. Even the old MS-DOS PCs were mostly like this (except for TSR programs, which were often like proto-hardware-drivers). It could help you appreciate how much arbitration between programs a modern OS does, beyond just abstracting hardware interfaces.
I wish you a happy and enlightening journey!



