-5

I have started to write my first kernel-module and used the KERN_INFO macro.
The line itself looks like that

printk(KERN_INFO "Hello world");

So I was confused, why there is no comma after KERN_INFO and used the preprocessor to print the expanded version.

printk("\001" "6" "Hello world 2\n");

Now I was confused even more. I wrote a little program test this with printf.

#include <stdio.h>

int main (void)
{
        printf("Hello" "World");
        return 0;
}

Which works just fine, but why?
I do not think those are 2 arguments because arguments are comma-separated.
I know C ignores all whitspaces but I have never
heard of it concatenating 2 strings without a function.

Is there an official documentation showing that this is possible or how it works?

JDurstberger
  • 4,127
  • 8
  • 31
  • 68

2 Answers2

3

C concatenates string literals which are adjacent. So for C language:

"abc" "def"

is the same as:

"abcdef"

It's important to remember it only works on compile time literals, not on char * variables, so:

char s[10] = "abc";
printf(s "def");

wouldn't work.

Alistra
  • 5,177
  • 2
  • 30
  • 42
2

Adjacent string literals are treated as a single literal, made up by concatenating the parts.

So "Hello" " there " "world" is equivalent to "Hello there world".

Incidentally, the adjacent parts can be broken across lines (which is a bit hard with a single literal).

So

"Hello"
" there "
"world";

and

"Hello there world";

are equivalent.

Peter
  • 35,646
  • 4
  • 32
  • 74