Keith Thompson said:
William Ahern said:
sprintf(buffer, "%05s", "42");
[Warning] `0' flag used with `%s' printf format
What's wrong with this, like why the warning?
[snip]
The semantics of sprintf(buffer, "%05s", "42") seem fairly clear -- or
rather, it seems clear what the semantics *would* be if they were
defined. Padding a string (rather than a number) with leading '0's
just isn't a common enough operation for it to be worth standardizing.
You might regret the lack if you happen to need to pad a string with
'0's, but next time you might need to pad it with some other arbitrary
character, and sprintf() isn't intended to solve all possible
problems. Sometimes you just have to roll your own solution.
[...]
Thanks Keith, esp. for answering in a non-cryptic, and helpful fashion.
I found the section in the c99 std which covers this myself
yesterday - so, I discovered that the semantics are undefined.
Still, I think I'll at least ask the gcc ppl if they could add some
of the 'why' to such warnings: after all, they've done the hard work
testing for such things, so it seems only fair that they should
consider telling the user why they've included such a check. But,
perhaps this is implied, i.e., once one sees something like this,
one should consider all such warnings in the same light. There is
one other possibility - that some of the gcc folk are like some of
the folks here (although I very much doubt that)