0

printing % in the output

why do I need to write two % in this statement to have one in the output? printf("Top 10%%.\n"); and if I write one % I don't have it in the output!

22nd Jun 2020, 10:23 AM
ŰȘوفيق Űčۚۯ Ű§Ù„Ű±ŰČŰ§Ù‚
ŰȘوفيق Űčۚۯ Ű§Ù„Ű±ŰČŰ§Ù‚ - avatar
1 Answer
0
Preamble: In C, % character has a special meaning when being used in I/O functions (e.g. printf()) as part of a string literal. Being a prefix indicating the beginning of a conversion specifier, after that, a sequence of other characters* is followed which is needed to set the conversion behavior to what is necessary. The fields of a conversion specifier shown below: "% [flag] [length modifier] [format specifier]" â–ș flag: https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p6 â–ș length modifier: https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p7 â–ș format specifier: https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p8 Note that, in the case of an input function such as `scanf()` there's no flag field. ~~~~~~~~~~~~~~ Answer to the question: Given how the language has defined this feature, it also provided the way by which the actual % character can be interpreted as to its literal form. Pretty much the same rationale when dealing with \ (backslash) character. "\" Error: \" escapes " (double quote) character "\"" prints " "\\" prints \ "%" Warning: spurious trailing ‘%’ in format "%%" prints % ________ * 1) flag 2) length modifier 3) format specifier — format specifier is mandatory among the three.
22nd Jun 2020, 11:24 AM
Babak
Babak - avatar