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

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


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

Using your example:

printf("hello%%");

Escaping the '%' 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: %%


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, segmentation faults, etc.).


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.