views:

745

answers:

10

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

printf("hello\%"); /* not like this */
+35  A: 

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

Pablo Santa Cruz
"printf("hello%%");" is right. But it's not a escape I think. use printf("hello\045");
NjuBee
@Pablo Santa Cruz: this method to "escape" `%` is specific to `printf`, correct?
Lazer
@eSKay: correct. Only for printf.
Pablo Santa Cruz
for formatted functions (afaik they ends all with an `f`) in general, indeed!
ShinTakezou
This is a special case of the very common rule in escaping systems that to get a literal escape symbol you use <escape symbol><escape symbol>.
dmckee
+5  A: 

With itself...

printf("hello%%"); /* like this */
martin clayton
+3  A: 

use a double %%

jldupont
+10  A: 

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.

Sinan Ünür
+1 , so many people forget about (or have never heard of) puts(). I see so many people using printf() for strings that have nothing to expand. This is the best answer.
Tim Post
Worth mentioning fputs() also, as it directly reciprocates to fprintf().
Tim Post
puts also appends a newline [even if you already have one]. If you want that, great. Otherwise...
Mikeage
@Sinan Ünür: thanks for reminding me about `puts`. I never thought of `puts` for printing strings and jumped straight to `printf`. Not anymore.
Lazer
+13  A: 

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]

Mikeage
The warning is generally appropriate however there may be situations in which you want to do "this" - as long as you know that the string you provide will be interpreted as a format string.
PP
I came up with an alternate solution once - copy the buffer to another buffer and then go through it doubling up the % signs. I eventually came across this idea and replaced a 20-30 line function with one line. Don't worry, I did beat myself severely about the head, as I deserved.
Graeme Perrow
True, but don't forget that 99% of the time when you get a format string, you get the arguments as a va_list, so you need to use vprintf. Thus, I'm technically correct ;)
Mikeage
It's so much easier to do puts( c ). If
William Pursell
puts appends a newline. That's often unwanted behavior.
Mikeage
+1  A: 

The backslash in C is used to escape characters in strings. Strings would not recognize % as a special character, and therefore no escape would be necessary. Printf is another matter: use %% to print one %.

Ralph Rickenbach
+1  A: 

Like this:

printf("hello%%");
//-----------^^ inside printf, use two percent signs together
Salman A
+6  A: 

Nitpick:
You don't really escape the % in the string that specifies the format for the printf() (and scanf()) family of functions.

The %, in the printf() (and scanf()) family of functions, starts a conversion specification. One of the rules for conversion specification states that a % as a conversion specifier (immediately following the % that started the conversion specification) causes a '%' character to be written with no argument converted.

The string really has 2 '%' characters inside (as opposed to escaping characters: "a\bc" is a string with 3 non null characters; "a%%b" is a string with 4 non null characters).

pmg
techinically, it is still "escaping"; characters that are special need a way to "escape" their special meaning and be back to their "character nature"
ShinTakezou
A: 

Yup, use printf("hello%%"); and it's done.

Kevin
+1  A: 

Use this:

#include <stdio.h>
printf("hello%s%s");

A Complete list of format specifiers used with printf can be found here:

http://www.mekong.net/tech/printf.htm