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

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

    As others have said, %% will escape the %.

    Note, however, that you should never do this:

    char c[100];
    char *c2;
    ...
    printf(c); /* OR */
    printf(c2);
    

    Whenever you have to print a string, always, always, always print it using

    printf("%s", c)
    

    to prevent an embedded % from causing problems [memory violations, segfault, etc]

提交回复
热议问题