Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Printk() and Printf() in Linux

I am a new user to Linux OS and am currently learning my way in it. I was studying about OS Debugging and related topics and as a basic debugging tool it is recomended to print out certain information regarding the state of the System.

This task can be accomplished by the mentioned functions printf() and also printk(), and have found that the "k" stands for kernel, and is some sort of "primitive" form of print function. Also that printf() is given by the C library rather than the kernel one (not completely sure of the meaning of this).

I was wondering if there is other advantage for using either of them other than the obvious one (which is to use printk() in earlier stages of the booting given that printf() is not yet avialable). Can one obtain the same information when using the two of them?

like image 937
DarkCygnus Avatar asked Aug 04 '15 19:08

DarkCygnus


People also ask

How printk works in Linux?

printk is a C function from the Linux kernel interface that prints messages to the kernel log. It accepts a string parameter called the format string, which specifies a method for rendering an arbitrary number of varied data type parameter(s) into a string. The string is then printed to the kernel log.

What are some differences between write () and printf () in C?

printf() is one of the APIs or interfaces exposed to user space to call functions from C library. printf() actually uses write() system call. The write() system call is actually responsible for sending data to the output.

Where do printk messages go?

All printk() messages are printed to the kernel log buffer, which is a ring buffer exported to userspace through /dev/kmsg.

What is the difference between printf () and printk ()?

printk () is a kernel level function, which has the ability to print out to different loglevels as defined in <linux/kernel.h>. The major difference between printk () and printf () is the capability of the former to specify a loglevel. The kernel uses the loglevel to decide whether to print the message to the console.

What is the use of printf in Linux?

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.

What is printk () in Linux?

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.

What is the difference between printf and echo command in Linux?

Echo command adds " " characters to the end of the string. That is, when we print something with the echo command, it automatically pass to the new line. But printf does not do this. echo "string" equal to "string "and print "string" equal to "string". so the output of "wc -m" command is different.


2 Answers

printk() is a kernel level function, which has the ability to print out to different loglevels as defined in <linux/kernel.h>.

printf() will always print to a file descriptor - STD_OUT

The major difference between printk() and printf() is the capability of the former to specify a loglevel. The kernel uses the loglevel to decide whether to print the message to the console. The kernel displays all messages with a loglevel below a specified value on the console.

More Information Here

like image 152
The Brofessor Avatar answered Oct 01 '22 11:10

The Brofessor


printf() is a function in the C Standard Library

You are correct that you would want to use printk() if you did not yet have access to the C libraries yet. printk() gives you printf() functionality where you would not have it otherwise.

A good comparison of the two can be found here

Edit note - replaced dead link with static archive version

like image 39
Michael Vessia Avatar answered Oct 01 '22 10:10

Michael Vessia