7
Contributors
13
Replies
16
Views
4 Years
Discussion Span
Last Post by Gonbe
Featured Replies
  • 2

    I'm not sure if this is what you mean, but try `printf("How are you %%dad%%");` Read More

  • 2

    > for example by adding \" it interprates " as a string then why cannot we use \ in our case? Because escape characters are interpreted at the compiler level and format specifiers are interpreted at the library level. If `\%` gave you a literal `'%'` then it would still … Read More

  • // show "How are you %dad%" #include <stdio.h> int main() { printf( "How are you %cdad%c", '%', '%'); getchar(); // wait return 0; } See http://ideone.com/wXgb0D Read More

  • 1

    > Sorry, couldn't get my CodeBlock IDE to work, so I did it on the ideone.com C compiler. It'll obviously work, but it's kind of silly. printf() offers a better way to do it, and if you're just printing a string with no replacements then puts() or fputs() would be … Read More

2

I'm not sure if this is what you mean, but try printf("How are you %%dad%%");

0

@mior.farhan.9 Thankyou for your answer but I had a doubt as to what is the mechanism or how does the compiler interprate that by adding one more % in the string it considers the next %d as part of string and not format specifier..for example by adding \" it interprates " as a string then why cannot we use \ in our case?

Edited by saurabh.mehta.33234

2

for example by adding \" it interprates " as a string then why cannot we use \ in our case?

Because escape characters are interpreted at the compiler level and format specifiers are interpreted at the library level. If \% gave you a literal '%' then it would still be treated as the opening character for a format specifier by the library code. Further, even though the library could be specified to use \%, it would have to actually look like this in user code:

printf("How are you \\%dad\\%");

And the reason is because the compiler will try to interpret any escape, so you need to escape the escape opening character to get a literal '\'. In other words, it's awkward either way, so printf()'s designer took the wiser route, in my opinion, and simply made a format specifier for a literal '%'.

It's probably a little advanced, but you can see a working implementation of printf() here:

http://code.google.com/p/c-standard-library/source/browse/src/internal/_printf.c#35

And the format specifier parsing is done in this function:

http://code.google.com/p/c-standard-library/source/browse/src/internal/_fmtspec.c#128

1

Sorry, couldn't get my CodeBlock IDE to work, so I did it on the ideone.com C compiler.

It'll obviously work, but it's kind of silly. printf() offers a better way to do it, and if you're just printing a string with no replacements then puts() or fputs() would be a better solution anyway:

puts("How are you %dad%");
0

Sorry, I thought the question was
how to print '% ' in printf()

That was the question, but if you're going to take it to unreasonable extremes, it's important to bring a little sanity to the thread. ;)

0

I reccommend nullptr's suggestion. You're going to have to get used to the double percent sign anyway, because there are some situations where you need to have both a format specifier and a percent sign in the same call to printf().

printf("Jimmy got a %d%% on his quiz!", percentage);
0

Using the the "%%" in the format string is the only good solution as far as I'm concerned. The function itself states that this should be used to print a single '%' symbol; it makes no sense to create all sorts of more complex constructs to do something that can be done in a simple manner.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.