Discussion:
Leading 0's for %f format spec may not be C standard?
(too old to reply)
Steve
2014-04-15 18:13:13 UTC
Permalink
Hello,

I have:

printf("HARDCODED:that is 09.2f: <%09.2f>\n", 123.45);
I would expect this to result in "00123.45" but instead I get " 123.45".

Per information I've been able to gather, including from OW's documentation,
it appears that there MAY be a non-standard implementation here:

ISO/IEC says

For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, leading zeros
(following any indication of sign or base) are used to pad to the field
width rather
than performing space padding, except when converting an infinity or NaN. If
the
0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X
conversions, if a precision is specified, the 0 flag is ignored. For other
conversions, the behavior is undefined.

OW says:

http://www.openwatcom.org/ftp/manuals/current/clib.pdf


If no field width is specified, or if the value that is given is less than
the number of characters in the converted value (subject to any precision
value), a field of sufficient width to contain the converted value is used.

"If the converted value has fewer characters than are specified by the field
width, the value is padded on the left (or right, subject to the
left-justification flag) with spaces or zero characters ("0").

" If the field width begins with "0" and no precision is specified, the
value is padded with zeros; otherwise the value is padded with spaces.

"If the field width is "*", a value of type int from the argument list is
used (before a precision argument or a conversion argument) as the minimum
field width. A negative field width value is interpreted as a
left-justification flag, followed by a positive field width."

To me this seems inconsisent.

Btw, FWIW, I am still on Watcom 11 - I know it's not supported - but went
ahead with this post since the OW Manual seems to confirm this odd behavior.

Thanks,
Steve
Hans-Bernhard Bröker
2014-04-15 20:29:27 UTC
Permalink
Post by Steve
printf("HARDCODED:that is 09.2f: <%09.2f>\n", 123.45);
I would expect this to result in "00123.45" but instead I get " 123.45".
Get it ... from what? OW 1.9 yields

HARDCODED:that is 09.2f: <000123.45>
Post by Steve
Per information I've been able to gather, including from OW's documentation,
Where is this "here"?

[... quotes from ISO C and the OW clib documentation ...]
Post by Steve
To me this seems inconsisent.
I think you'll have to expand on why that seems so. What's inconsistent
with what, and why exactly?
Steve
2014-04-15 20:49:44 UTC
Permalink
Post by Hans-Bernhard Bröker
Get it ... from what? OW 1.9 yields
HARDCODED:that is 09.2f: <000123.45>
OK as I said this was on Watcom 11. I know this is outdated and I would not
have pursued this further but the OW documentation seemed to imply that the
padding would be per blanks...(I think that I did not paste this properly in
my original post and for that I apologize. :) )

"If the field width begins with "0" and no precision is specified, the value
is padded with zeros. Otherwise, the value is padded with spaes."

It's the "otherwise" that made me think that the OW behvaior was to pad with
blanks.
Post by Hans-Bernhard Bröker
Post by Steve
To me this seems inconsisent.
I think you'll have to expand on why that seems so. What's inconsistent
with what, and why exactly?
All I meant that it seemed that Watcom C padded with blanks and the standard
C with 0's. This was based on the OW documentation and what I observed in
Watcom 11. Again, I know that I cannot expect support for v11, so I'll have
to live with this, but it's that above sentence in the
documentation..."Otherwies, the value is padded with spaces"...that raised
my level of concern.

Thanks, and sorry for any confusion.
Post by Hans-Bernhard Bröker
Post by Steve
printf("HARDCODED:that is 09.2f: <%09.2f>\n", 123.45);
I would expect this to result in "00123.45" but instead I get " 123.45".
Get it ... from what? OW 1.9 yields
HARDCODED:that is 09.2f: <000123.45>
Post by Steve
Per information I've been able to gather, including from OW's
documentation,
Where is this "here"?
[... quotes from ISO C and the OW clib documentation ...]
Post by Steve
To me this seems inconsisent.
I think you'll have to expand on why that seems so. What's inconsistent
with what, and why exactly?
Loading...