How to escape the % (percent) sign in C's printf?

前端 未结 13 2833
长发绾君心
长发绾君心 2020-11-22 09:34

How do you escape the % sign when using printf in C?

printf(\"hello\\%\"); /* not like this */
13条回答
  •  [愿得一人]
    2020-11-22 10:24

    Nitpick:
    You don't really escape the % in the string that specifies the format for the printf() (and scanf()) family of functions.

    The %, in the printf() (and scanf()) family of functions, starts a conversion specification. One of the rules for conversion specification states that a % as a conversion specifier (immediately following the % that started the conversion specification) causes a '%' character to be written with no argument converted.

    The string really has 2 '%' characters inside (as opposed to escaping characters: "a\bc" is a string with 3 non null characters; "a%%b" is a string with 4 non null characters).

提交回复
热议问题