Why is that the output of
#include<stdio.h>
void main()
{
printf("%d"+1);
}
is d but the output of
#include<stdio.h>
void main()
{
printf("%%%d"+1);
}
is %d and not %%d ??
"%d"+1
by pointer arithmetic takes you to the second char in the char array which is d
.
In the string literal "%%%d"+1
leaves you with "%%d"
which is interpreted as %d
by printf
. Since %%
is escaped to %
.
You are doing pointer arithmetic. "%d"+1
is "d"
and "%%%d"+1
is "%%d"
(it's like you are skipping the first character of the string).
But, as the documentation of printf() explains, the percent sign ( %
) is a special character in the string format argument of printf()
. It introduces a "conversion specifications".
Because it is a special character it needs a special sequence (a conversion specification, in fact) in order to be able to print a literal %
. And the conversion specification designated to print a literal %
is exactly %%
, as you can see from the first row of the conversion specifications table in the docs.
You are using +1
in printf
which will in turn skip the one character ie %
in your case. after skipping %
character you will be left with %%d
, since %%
is used to print %
character. Output will be %d
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.