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

前端 未结 13 2839
长发绾君心
长发绾君心 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:29

    If there are no formats in the string, you can use puts (or fputs):

    puts("hello%");
    

    if there is a format in the string:

    printf("%.2f%%", 53.2);
    

    As noted in the comments, puts appends a \n to the output and fputs does not.

提交回复
热议问题