Why printf(“%s\n”, “123456789” + 3); //output: “456789”
源自: stackoverflow

execution function:

printf("%s\n", "123456789" + 3); //output: "456789"

why output this result? In the same way, what should I do if I want to output "123"? Now I'm confused. Help me.


printf() is passed a pointer to a string constant incremented by 3

printf("%s\n", "123456789" + 3);  

printf() takes a format string argument followed by a variadic set of arguments. In this case, the '%s' is the first (and only) variable in the format string.

There is only one argument in the variadic section after the format string: a string constant: "12345678" which resolves to a pointer to the '1' character; however, that pointer is incremented by 3 due to the "+ 3". This moves the pointer 3 positions to the '4'.

Then, printf prints the string that starts at '4'. As the string constant is terminated by a '\0' character right after the '9', printf prints "456789"

Why printf(“%s\n”, “123456789” + 3); //output: “456789”