📜  Linux 中 Printk() 和 Printf() 的区别

📅  最后修改于: 2021-09-11 03:37:02             🧑  作者: Mango

在 Linux 机器上工作并分析一些代码时,您可能遇到过在某处看到 printk() 和 printf() 的约定,现在您想知道。
Linux 中的 Printk() 和 Printf() 有什么区别?
好吧,答案就在本文中,让我们深入了解并一次了解一个。对于初始启动器,printk 是一个 Linux 内核接口 C函数,它将消息输出到内核日志,而 printf 命令用于在终端窗口中显示字符串、数字或任何其他格式说明符

最著名的Linux 内核函数之一是 printk() 。它是打印消息的默认工具,也是最基本的跟踪和调试方法。 printf() 方法将参数打印到标准输出流中,这些参数用双引号引起来。

Linux 中 Printk() 和 Printf() 的区别:

Printk()

Printf()

printk() is a kernel-level function that may print to a variety of log levels, as described in Linux. 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.

带走 :

printk() 定义日志级别的能力是它与 printf() 的主要区别。

在终端上,内核显示日志级别低于定义值的所有消息。

结论 :

尽管 ptink() 和 printf() 之间的区别非常不稳定,但它们是独特且不言自明的,您可以使用 prink()函数从 Linux 系统打印某些内容,或者使用 printf()函数来简单地打印输出。

决定权在您手中,您现在可以做出决定!