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

Chris_45 picture Chris_45 · Dec 7, 2009 · Viewed 292.3k times · Source

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

printf("hello\%"); /* not like this */

Answer

Pablo Santa Cruz picture Pablo Santa Cruz · Dec 7, 2009

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: %%