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

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

    You can escape it by posting a double '%' like this: %%

    Using your example:

    printf("hello%%");
    

    Escaping '%' sign is only for printf. If you do:

    char a[5];
    strcpy(a, "%%");
    printf("This is a's value: %s\n", a);
    

    It will print: This is a's value: %%

提交回复
热议问题