printf("%%d",12) prints %dd whereas
printf("%%%d",12) prints %12d
what is the reason behind this? how does it actually work?
printf("%%d",12) prints %dd whereas
printf("%%%d",12) prints %12d
what is the reason behind this? how does it actually work?
When printf encounters % it thinks as a format specifier unless it encounters another %.
%% is used to print %
When you do printf("%%d",12) -
%, it recognizes it as a format specifier.%, the second % behaves as a escape character for the first % (remember % is escaped with another %)d, ie, ā%dā (with ā%ā not being a format specifier here).12 to printf and printf is not expecting any parameter (since there is no format specifier, it gives a warning that there are extra arguments) it ignores 12 and prints ā%dā.Now if you had %%%d, the compiler would interpret the first two % as a single ā%ā and then you have this followed by %d, which will compile to ā12ā. So the output will be ā%12ā.
Ideone - http://ideone.com/aYOIZZ