What is the output of the following and what's the reason behind this?
main()
{
printf("%%%%");
}
The answer is "%%", but I don't know why.
What is the output of the following and what's the reason behind this?
main()
{
printf("%%%%");
}
The answer is "%%", but I don't know why.
%
is the beginning of a format-specifier. (For example, "%d"
means "print an int
".) A %
after the format-specifier escapes it, printing a single "%".
That is, in the same way "\\"
results in a single backslash, "%%"
results in a single percent-sign.
in printf, %
is usually used to indicate a token such as %s
or %d
or %5.2f
. If you want to output a literal %
, you use %%
.
Technically, the output could be anything since you didn't print a newline at the end, and also because you didn't include stdio.h
:-).
However, correcting the two mistakes:
#include <stdio.h>
int main(void)
{
printf("%%%%\n");
return 0;
}
The above should print %%
followed by a newline.
Each conversion specification is introduced by the character %.
...
The conversion specifiers and their meanings are:
...
%
: A%
character is written. No argument is converted. The complete conversion specification shall be%%
.
(C99 7.19.6.1).