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

CPrintfFormat String

C Problem Overview


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

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

C Solutions


Solution 1 - C

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

Solution 2 - C

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.).

Solution 3 - C

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.

Solution 4 - C

With itself...

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

Solution 5 - C

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).

Solution 6 - C

Use a double %%:

printf("hello%%");

Solution 7 - C

Like this:

printf("hello%%");
//-----------^^ inside printf, use two percent signs together

Solution 8 - C

You are using the incorrect format specifier. You should use %% for printing %. Your code should be:

printf("hello%%");  

Read more all format specifiers used in C.

Solution 9 - C

You can use %%:

printf("100%%");

The result is:

> 100%

Solution 10 - C

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

Solution 11 - C

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

Solution 12 - C

You can simply use % twice, that is "%%"

Example:

printf("You gave me 12.3 %% of profit");

Solution 13 - C

The double '%' works also in ".Format(…). Example (with iDrawApertureMask == 87, fCornerRadMask == 0.05): csCurrentLine.Format("\%ADD%2d%C,%6.4f*\%",iDrawApertureMask,fCornerRadMask) ; gives the desired and expected value of (string contents in) csCurrentLine; "%ADD87C, 0.0500*%"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris_45View Question on Stackoverflow
Solution 1 - CPablo Santa CruzView Answer on Stackoverflow
Solution 2 - CMikeageView Answer on Stackoverflow
Solution 3 - CSinan ÜnürView Answer on Stackoverflow
Solution 4 - Cmartin claytonView Answer on Stackoverflow
Solution 5 - CpmgView Answer on Stackoverflow
Solution 6 - CjldupontView Answer on Stackoverflow
Solution 7 - CSalman AView Answer on Stackoverflow
Solution 8 - CPankaj PrakashView Answer on Stackoverflow
Solution 9 - CDonald DuckView Answer on Stackoverflow
Solution 10 - CRalph M. RickenbachView Answer on Stackoverflow
Solution 11 - CKevinView Answer on Stackoverflow
Solution 12 - CMd ShahriarView Answer on Stackoverflow
Solution 13 - CdindeaView Answer on Stackoverflow