Why doesn't the function printk() use a comma to separate parameters?

让人想犯罪 __ 提交于 2021-01-26 19:23:11

问题


An example printk call:

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.

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.


回答1:


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

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

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

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



回答2:


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:


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.

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.



来源:https://stackoverflow.com/questions/38492059/why-doesnt-the-function-printk-use-a-comma-to-separate-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!