Let me try to help and add to @Yunnosch answer, you could do following:
char *str = "%d";
printf (str, 127);
and output should be:
127
In above example str
is stored in memory like this (this is just an example and in real life addresses are like 0xabcdef12345678):
address | memory
--------+--------------
0 | %
1 | d
3 | \0
so str
points to address 0
(or equivalent on your system) which holds %
, printf()
gets that address and starts reading from there, reads until it hits the NULL character '\0'. Now every time it sees the %
it looks for next character d
, c
, x
etc. it reads next argument from argument list. If you supplied d
it will output decimal, if you supply c
it will print character, x
is for hex and there is many more. So printf()
will read all characters and replace %<place holder>
with appropriate parameter until it hits the NULL character \0
, but it will start at supplied address.
Your case:
printf("%d"+1, 127);
is same as:
char *str = "%d";
printf (str + 1, 127); // printf receives address of character `d` as starting point
or it is similar to
char *str = "%d";
char *str1 = str+1; // increment the address of string by one
printf (str1, 127)
in both cases it receives address of d
and reads from there until it hits \0
.
If you do following:
printf ("%d" + 2, 127);
This would be the same as:
char *str = "%d";
printf (str + 2, 127);
it would output nothing because, printf()
will get the address of \0
. and as @Yunnosh said 127
would be ignored.
So +1
and +2
do not get converted to a string they get added to the address of the string.
I hope this helps, I think I answered both of your questions.