The output of
printf("%%%%%%%%");
is
%%%%
I used %
eight times. Why does the output only have four %
s?
Because %
is a format specifier escape sequence (as in %d
would print an int
).
To get the program to print the actual symbol, you write %%
. The same goes for \
(although fundamentally different, you still need to to print one).
The "%"
is a special character, and it's used to specify format specifiers. To print a literal "%"
, you need a sequence of two "%%"
.
The "%"
is used in the format string to specify a placeholder that will be replaced by a corresponding argument passed to the functions that format their output, like printf()
/fprintf()
/sprintf()
.
So how can you print a literal "%"
?
"%"
.If you for example wish to print an integral percentage value, you need to specify as the "%d"
the format specifier and a literal "%"
, so you would do it this way:
printf("%d%%\n", value):
Read this to learn more about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With