Difference between Printk () and Printf () on Linux

I am a new user for Linux and am currently learning this. I studied the topic "Debugging the OS" and related topics, and as the main tool for debugging, it is recommended to print certain information about the state of the system.

This task can be performed by the mentioned functions printf() , as well as printk() , and found that "k" denotes the kernel and is a kind of "primitive" form of the print function. In addition, printf() is defined by the C library, not by the kernel (not completely sure about the meaning of this).

I was wondering if there is any other advantage to using any of them, except for the obvious one (which should use printk() in the early stages of loading, given that printf() is not yet available). Is it possible to get the same information when using two of them?

+4
source share
2 answers

printk() is a kernel-level function that has the ability to print various log levels , as defined in <linux/kernel.h> .

printf() will always print in the file descriptor - STD_OUT

The main difference between printk() and printf() is the ability to set the log level first. The kernel uses loglevel to decide whether to print a message to the console. The kernel displays all messages with a log level below the specified value on the console.

More info here

+2
source

printf() is a function in the C standard library

You are correct that you want to use printk() if you do not already have access to the libraries C. printk() provides you with printf() functionality in which you would not have this otherwise.

A good comparison of the two can be found here.

+1
source

All Articles