Hi,
'C' says \ is the escape character to be used when characters are
to be interpreted in an uncommon sense, e.g. \t usage in printf(),
but for printing % through printf(), i have read [pg.154 of 'The C
programming language' -kernighan, Ritchie] that %% should be used.
Wouldn't it have been better (from design perspective) if the same
escape character had been used in this case too.
Forgive me for posting without verfying things with any standard
compiler, i don't have the means for now.
The two conventions "escape" from two different
processing mechanisms operating at two different levels.
The \ escape tells the C compiler to do something
unusual, like generating a single tab character when it
sees a \ followed by a t. The compiler does what it does,
and when all's done your program has a string containing
a tab character; both the \ and the t are gone. They have
gone, in a sense, to the same place the " quotes went: they
were source-code constructs that produced some effect in
the finished program and then vanished, leaving only the
program itself behind.
So now you've got a program with some strings in it,
and the strings don't contain any backslash characters
(unless you created them with the source sequence \\, of
course). Now you hand one of these strings to printf() as
a format, and printf() starts copying the format string's
characters to the output. (Note that there's nothing special
about the tab character; printf() just copies it the same
way it copies H e l l o.) However, you need a way to tell
printf() to stop copying and do something else, like output
the characters that represent an integer value. So printf()
is sensitive to the % character, which means "stop copying
and do something different, as specified by the next few
characters (which also aren't copied)."
Of course, this means you can't output a % character by
just copying it from the format; as soon as printf() sees %
it switches into "do something special" mode. So to make it
possible to output a percent sign, printf() recognizes the
sequence %% as "do something special, to wit, output a %
character."
printf() could have been designed to use something other
than the % character as its "special stuff" marker; in fact,
it could perfectly well have used the backslash character.
But remember that it takes extra effort to get the compiler
to generate a backslash character in a string: backslash means
"special stuff" to the compiler, so you need to write two of
them to generate one as "payload." Your printf() formats
would wind up looking like
printf ("Answer = \\d\n", 42);
That might not look too awful, but consider: this different
printf() would also need a way to output an actual backslash
character, just as the real printf() needs a special mechanism
to output a percent sign. Well, how about doubling them up?
printf ("Answer between backslashes = \\\\\\d\\\\\n", 42);
Ugh, and double ugh. (Sadly, many "regular expression" packages
run into exactly this problem: they use \ as their own "special"
indicator, and they double it to mean "just an ordinary backslash
after all," and then it gets doubled again to push it through the
C compiler ... Trying to read regular expression strings embedded
in C source code can make your eyes go twisty.)
Summary: Different escape mechanisms to talk to different
entities that operate at different times.