1

Anywhere in the C language, without any libraries, there is no way to print text to the console. printf() could just use language functionality to format a string, and then puts() it, which could work like putchar() in a loop.

putchar() is where the magic happens. How were the library functions written? How do they print to the console, even though C language only can't?

How does putchar() work internally? How does it create whole new functionality with only native C or Assembly? How do these library functions interact with the operating system like that?

  • Read the source code to find out. It is not a secret. In the meantime, this question is wildly off-topic. – TomServo Jun 29 '20 at 00:53
  • @TomServo The source code is meaningless to me. It seems to call other functions. Where does the actual "magic" happen? –  Jun 29 '20 at 01:04
  • Again, off-topic. – TomServo Jun 29 '20 at 01:15
  • @TomServo How? Please elaborate. –  Jun 29 '20 at 01:22
  • 1
    Similar: https://stackoverflow.com/questions/468262/how-will-you-print-any-charcater-string-or-value-of-a-variable-without-library?rq=1 – Nate Eldredge Jun 29 '20 at 01:23
  • @NateEldredge Doesn't seem like the same question at all. And that question's accepted answer is really messed up. – Richard Chambers Jun 29 '20 at 01:35
  • @RichardChambers It's at least related? –  Jun 29 '20 at 01:54
  • The answer is different from different operating systems. There are some general principles (and exceptions), but you'll get a more specific answer if you can pick a specific OS to ask about. – Nate Eldredge Jun 29 '20 at 02:39
  • 1
    The key thing "unimplementable" in C is usually "[system calls](https://en.wikipedia.org/wiki/System_call)", which bridge user program code and the operating system code. – aschepler Jun 29 '20 at 02:40
  • @aschepler System calls. I see. Does C have a built in way to perform a system call? –  Jun 29 '20 at 02:41
  • @NateEldredge Say, linux –  Jun 29 '20 at 02:42
  • 2
    Not Standard C, but compilers often have some sort of `asm` or `__asm__` etc. keyword for writing in assembly, which would make it possible if you know enough about the accepted assembly syntax, the target processor, and the calling convention for a specific system call. Which is why all of this is wrapped into libraries provided by the OS. – aschepler Jun 29 '20 at 02:49
  • @aschepler I see. –  Jun 29 '20 at 02:50
  • 2
    @PounddefineMACRO well maybe. I just posted a turgid answer with more details than anyone really wants to know at https://stackoverflow.com/a/62630549/1466970 – Richard Chambers Jun 29 '20 at 03:01

1 Answers1

1

How text is outputted internally depends on the operating system. Most operating systems provide a native API which allows access to all features provided by the operating system. The C runtime library will likely call functions from this native API in order to perform I/O operations.

For example, on Microsoft Windows, you can obtain the file handle for "standard output" by calling the function GetStdHandle, which is exported by the DLL kernel32.dll (which is part of the operating system). Afterwards, you can write to the returned file handle using WriteFile. This function is also exported by kernel32.dll. Both of these functions are intended to be called by C or C++ code, but can also be called by assembly code.

On other operating systems, such as Linux or MacOS, the interface provided by the operating system is different.

How the operating system implements these functions is an implementation detail and may change when a new version of the operating system is released. The operating system's implementation is probably mostly programmed in C or C++, with a bit of assembly language in a few places. If you want to see exactly how such things are implemented, you might want to consider taking a look at the source code of the Linux kernel and of the corresponding C runtime library.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39