Skip to content
Related Articles

Related Articles

Improve Article

Difference between Printk() and Printf() in Linux

  • Last Updated : 02 Jul, 2021

While working on a Linux machine, and analyzing some code you might have come across a convention of seeing printk() and printf() written somewhere, and now you wonder.
What is the Difference Between Printk() and Printf() in Linux?
Well, the answer lies here in this article, lets dive deep and understand them one at a time. For the initial starter printk is a Linux kernel interface C function that outputs messages to the kernel log whereas the printf command is used to show a string, a number, or any other format specifier in a terminal window.

One of the most well-known Linux kernel functions is printk(). It’s the default tool for printing messages and the most basic method of tracing and debugging. The printf() method prints the parameters to the stdout stream, which are written in double inverted quotations.

Difference between Printk() and Printf() in Linux :



printk() is a kernel-level function that may print to a variety of log levels, as described in Linux<kernel.h>.printf() will always print to an STD OUT file descriptor.
printk() is not a Standard Library Function.printf() is a C Standard Library function.
printk() is used by the kernel to print.To print something from the application layer it uses printf().
The printk() method can be called at any time from almost anywhere in the kernel.The printf() method is not so robust.
It’s useless until a specific point during the kernel startup process, before the console is initialized.Unlike printk() it is always ready as the system is in the ready state to execute it terminally.

Example of printk() (Using Log Level):

printk("<2>This is GeeksForGeeks\n");

Example of printf()

printf("This is GeeksForGeeks\n");
printk() is line-driven, which means that only data received by a newline character is written to the terminal; otherwise, no data is produced.printf() on the contrary is not line-driven and the content can be written without even the newline characters.

Takeaway :

The ability of printk() to define a loglevel is the main distinction between it and printf(). 

Note: The loglevel is used by the kernel to determine whether or not to print the message to the console. 

On the terminal, the kernel shows all messages with a loglevel lower than a defined value.

Conclusion :

Although the difference between both ptink() and printf() are highly volatile, they are unique and self-explaining, you may use the prink() function to print something from your Linux system or else use the printf() function to simply print outputs.

The decision is yours, and you can now take that!


My Personal Notes arrow_drop_up
Recommended Articles
Page :