为什么printk()函数不使用逗号分隔参数?

时间:2022-12-13 11:03:02

An example printk call:

printk调用示例:

printk(KERN_INFO "Log message.\n");

Perhaps this question is more about C in general, because I've never seen a function in C before that separated parameters without a comma.

也许这个问题更多地是关于C的,因为在没有逗号的情况下,在分离参数之前我从未见过C中的函数。

How does this work? What does the compiler do with this information? Since the log level is an integer and the message is a pointer to a char array, it must pass them separately.

这个怎么用?编译器对这些信息做了什么?由于日志级别是一个整数,并且消息是指向char数组的指针,因此它必须单独传递它们。

3 个解决方案

#1


21  

The printk() function only takes one const char* argument. The KERN_INFO macro expands to "\001" "6", yielding:

printk()函数只接受一个const char *参数。 KERN_INFO宏扩展为“\ 001”“6”,产生:

printk("\001" "6" "Log message.\n");

The C lexer concatenates adjacent string literal tokens which means that the above is converted into:

C词法分隔符连接相邻的字符串文字标记,这意味着上面的内容转换为:

printk("\0016Log message.\n");

#2


7  

The log level isn't an integer but a string literal. String literals next to each other are concatenated into a single string literal at compile time.

日志级别不是整数,而是字符串文字。彼此相邻的字符串文字在编译时连接成一个字符串文字。

#3


5  

Because if you search the header files you will see that e.g. KERN_INFO is a macro expanded as a string literal (actually multiple string literals, see e.g. the linked cross-reference), and two string literals next to each-other like that will be concatenated into a single string literal by the compiler.

因为如果您搜索标题文件,您会看到例如KERN_INFO是一个扩展为字符串文字的宏(实际上是多个字符串文字,参见例如链接的交叉引用),并且两个字符串文字旁边的每个字符串文字将由编译器连接成单个字符串文字。

So the call

所以电话

printk(KERN_INFO "Log message.\n");

isn't a function call with multiple arguments, it's a function call with a single string literal argument.

不是带有多个参数的函数调用,它是一个带有单个字符串文字参数的函数调用。

#1


21  

The printk() function only takes one const char* argument. The KERN_INFO macro expands to "\001" "6", yielding:

printk()函数只接受一个const char *参数。 KERN_INFO宏扩展为“\ 001”“6”,产生:

printk("\001" "6" "Log message.\n");

The C lexer concatenates adjacent string literal tokens which means that the above is converted into:

C词法分隔符连接相邻的字符串文字标记,这意味着上面的内容转换为:

printk("\0016Log message.\n");

#2


7  

The log level isn't an integer but a string literal. String literals next to each other are concatenated into a single string literal at compile time.

日志级别不是整数,而是字符串文字。彼此相邻的字符串文字在编译时连接成一个字符串文字。

#3


5  

Because if you search the header files you will see that e.g. KERN_INFO is a macro expanded as a string literal (actually multiple string literals, see e.g. the linked cross-reference), and two string literals next to each-other like that will be concatenated into a single string literal by the compiler.

因为如果您搜索标题文件,您会看到例如KERN_INFO是一个扩展为字符串文字的宏(实际上是多个字符串文字,参见例如链接的交叉引用),并且两个字符串文字旁边的每个字符串文字将由编译器连接成单个字符串文字。

So the call

所以电话

printk(KERN_INFO "Log message.\n");

isn't a function call with multiple arguments, it's a function call with a single string literal argument.

不是带有多个参数的函数调用,它是一个带有单个字符串文字参数的函数调用。