We all know that printf() is a function of <stdio.h>. I want to know that what's happening under the hood when we type printf("something"); and it gets displayed on the screen... How come it's displayed on the screen and what is the code inside printf() function by which it displays something on the screen?thanks in advance
Asked
Active
Viewed 1,258 times
1
-
4Possible duplicate of [Code for printf function in C](https://stackoverflow.com/questions/4867229/code-for-printf-function-in-c) – Sander De Dycker Jan 30 '19 at 15:13
-
3There are many open-source implementations of `printf` available, with source available for you to study. However, if you want to know the full path from the call of `printf` to the output being displayed on the screen, that's a Herculean task that will teach you about compilers (generating the code for the call) to operating systems to device drivers to hardware and back up through the stack until the output ends up in the terminal window. – Some programmer dude Jan 30 '19 at 15:13
-
1It appears you are looking for general information on the whole chain of events that makes `printf` work. [Here](https://stackoverflow.com/a/13659290/298225) is my answer from six years ago about that. – Eric Postpischil Jan 30 '19 at 15:42
-
1This should be marked as a duplicate of [What goes behind printf in C](https://stackoverflow.com/questions/13657890/what-goes-behind-printf-in-c), rather than [Code for printf function in C](https://stackoverflow.com/questions/4867229/code-for-printf-function-in-c), and that would be a better resolution than too broad. So I vote to reopen, and will then vote to close as a duplicate. – Eric Postpischil Jan 30 '19 at 15:43
-
*Nothing* in `printf` itself displays anything on the screen, and indeed, `printf` calls don't always have that result. They just cause formatted output to be sent to the standard output stream. If that happens to be connected to a terminal driver, then that driver displays the output (on the screen). If it's connected to some other kind of device, then it is handled appropriately for that device -- written in a file, maybe, or sent across the network, or stored in memory, or dispatched to another program, or .... – John Bollinger Jan 30 '19 at 15:51
1 Answers
1
printf("something");
is the equivalent of fprintf(stdout, "something");
so it prints on the standard output, whatever where finally the output will be done (screen, file, pipe, ...)
because "something" doesn't content special formating (using %) if is printed unchanged, and probably your question moves to be how putchar() / fputc() works
printf doesn't know what a screen / file / pipe / ... is, in the same way scanf doesn't know what a keyboard is, this is not their responsibility

bruno
- 32,421
- 7
- 25
- 37
-
Sir actually how the computer displays it on the screen ..is there any code inside it that display it on the screen?we are just typing printf("hello world") and it's on the screen..but how it's on the screen Can we create our own printf() function? – Debdut Bhaduri Jan 30 '19 at 15:17
-
1@DebdutBhaduri it you redirect the output you will not have the result on the screen ;-) What is your OS ? How you started your program, in a shell ? – bruno Jan 30 '19 at 15:18
-
Sir am really not getting it suppose we write a programming to print hello world we write. Printf("hello world") we get hello world on the screen...so what is exactly happening under the hood..how this function displays hello world on the screen – Debdut Bhaduri Jan 30 '19 at 15:19
-
-
@DebdutBhaduri ok, how you started your program, in a terminal or double clic on its icon ? – bruno Jan 30 '19 at 15:22
-
Sir I write the code in sublime text...and then on the terminal ...>gcc prog.c ..then> a.exe... – Debdut Bhaduri Jan 30 '19 at 15:24
-
@DebdutBhaduri ok, so in that case the _standard output_ is the shell output, but if you do `a.exe > foo` the result will be in the file _foo_ – bruno Jan 30 '19 at 15:25
-
1@DebdutBhaduri _printf_ doesn't care about where the output will be produced, this is not its job, like _getchar_ doesn't manage the keyboard. All is done on the I/O management – bruno Jan 30 '19 at 15:27