This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Percent symbol and sprintf() (BUG?)

How do I print a percent sign with sprintf()?. I want to do something like this:

sprintf(buf, "Value: %3d%%", value);

According to ANSI C this should give
"Value: 98%" or the like.

But it seems that sprintf doesn't know the double precent seqence (printf does!).

sprintf(buf, "Value: %3d%s", value, "%");
doesn't work either.

sprintf(buf, "Value: %3d%s", value, "%%");
works but should not!

Is this a bug or am I doing something stupid?

Regards,
Ralf

Parents
  • Silly me!
    I found the error: it was my mistake.
    So forget it!

    FYI:
    the string formatted with sprintf() was output with a printf() later and thus used as the new format string, this format string mustn't have a single percent sign (will be misinterpreted).
    Using puts() instead solved the problem.

    Ralf

Reply
  • Silly me!
    I found the error: it was my mistake.
    So forget it!

    FYI:
    the string formatted with sprintf() was output with a printf() later and thus used as the new format string, this format string mustn't have a single percent sign (will be misinterpreted).
    Using puts() instead solved the problem.

    Ralf

Children