DEV Community

hassaan-syed
hassaan-syed

Posted on

printf("hello"); How does "Hello" appear on your screen?

A journey from your program to the terminal.
WHAT IS printf()?
Generally, printf() is a function used to display output on the screen. It is a predefined library function provided by the Standard C Library (stdio.h).

#include <stdio.h>

int main() {
    printf("Hello");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

output:
hello


______________________A Flow Chart_______________________
At first glance, it looks like printf() simply prints the text on the screen.
But is that what actually happens?
Does printf() directly communicate with the monitor?
No.

There are several interesting steps that take place behind the scenes before the text finally appears on your terminal.

Let's understand the complete journey of printf() step by step.

Step 1: The printf() Statement is Executed
When the CPU executes the following statement:

printf("Hello\n");
Enter fullscreen mode Exit fullscreen mode

the first question that comes to our mind is:
Does printf() immediately display "Hello" on the screen?
The answer is No.

At this point, the CPU simply transfers control to the printf() function, which is part of the Standard C Library (stdio.h).

main()
   |
   | executes
   v
printf("Hello\n")
Enter fullscreen mode Exit fullscreen mode

Now the execution has entered the printf() function,
The job of printf() is not to directly communicate with the terminal. Instead, it begins preparing the output by processing the string and storing it in the stdout buffer.

In the next step, we'll see how this buffer works and why C uses it.
Step 2: The stdout Buffer
Now that execution has entered the printf() function, it doesn't immediately call the kernel.

Instead, printf() copies the output into the stdout buffer, which is located in user space.

Application (User Space)

printf()
     |
     v
+--------------------+
| stdout Buffer      |
| Hello\n            |
+--------------------+
Enter fullscreen mode Exit fullscreen mode

At this point, nothing has been displayed on the terminal yet.

The data is simply waiting inside the buffer.
Step 3: Why Doesn't printf() Display the Output Immediately?
After copying the data into the stdout buffer, printf() does not immediately send it to the terminal.

Instead, the data remains in the buffer until one of the following events occurs:
The buffer becomes full.
A newline character (\n) is encountered (when stdout is line-buffered).
fflush(stdout) is called.
The program terminates normally.

This behavior is called buffering.

Why use buffering?

Imagine you need to send 100 letters.

You have two choices:

Deliver each letter individually (100 trips).
Collect all the letters in a bag and deliver them together (1 trip).

Obviously, making one trip is much more efficient.

The stdout buffer works in the same way. Instead of asking the operating system to display every single character immediately, printf() collects the data in a buffer and sends it in one operation. This reduces the number of system calls, making the program much more efficient.
At this point, the stdout buffer contains:

+----------------------+
| H | e | l | l | o |\n|
+----------------------+
Enter fullscreen mode Exit fullscreen mode

The data is still in user space and has not yet entered the kernel.

The next step is to understand what causes this buffer to be flushed and how the write() system call transfers the data into kernel space.
Step 4: The stdout Buffer is Flushed
The data is currently stored inside the stdout buffer.

However, the terminal still cannot display it because the data hasn't left user space.
When one of the flushing conditions is met (for example, printf("Hello\n"); contains a newline), the C Standard Library decides to flush the buffer.

Flushing does not mean deleting the data.
It means sending the buffered data to the operating system (kernel).
Internally, the C library performs this operation by calling the write() system call.

Conceptually, it looks like this:

write(1, stdout_buffer, 6);
Enter fullscreen mode Exit fullscreen mode

where:

1 → File descriptor for Standard Output (stdout)
stdout_buffer → Address of the data stored in the buffer
6 → Number of bytes to send (Hello\n)

At this moment, the following sequence occurs:

stdout Buffer
+----------------------+
| H | e | l | l | o |\n|
+----------------------+
          │
          │ Flush
          ▼
write(1, stdout_buffer, 6)
Enter fullscreen mode Exit fullscreen mode

The write() function is a system call, which means the CPU now switches from User Mode to Kernel Mode.
This is the first time our data leaves the C library and enters the operating system.

In the next step, we'll see what happens inside the kernel after the write() system call is invoked.
At the end of every step, leave the reader with a question or teaser.

For example:

But how does write() actually transfer the data into the kernel?
Can any one give the answer

Top comments (0)