[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::decc

Title:DECC
Notice:General DEC C discussions
Moderator:TLE::D_SMITHNTE
Created:Fri Nov 13 1992
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2212
Total number of notes:11045

2127.0. "printf and %d?" by CSC32::J_HENSON (Don't get even, get ahead!) Tue Mar 25 1997 11:23

I need a sanity check/confirmation on the expected behavior of the
printf statement with the %d specifier.  I just got off of the
phone with a customer who is contending that it is not working as
advertised.  I think he is misinterpreting the documentation (which
I also think is easy to do), but would like some confirmation.

In particular, look at that following code.

#include <stdio.h>
main()
{
	int i = 1;
	printf("%3.3d\n",i);
	printf("%3.0d\n",i);
}

The output of this is 

001
  1

My customer's contention is that the %3.3d specifier should not be padding
to the left with 0s.  And, the documentation for field width (page 2-18
of the rtl ref manual) states

"If the converted output source is narrower than the minimum width, pad
it to make up field width.  Pad with SPACES (emphasis mine) by default.
Pad with zeroes if the 0 flag is specified;....."

FWIW, I think that this behavior is covered in the description of the
precision specifier (page 2-19), which reads

 - minimum number of DIGITS (emphasis mine) to appear for d,i,o,u,x and X
   conversions.


Anyway, can someone give me a definitive answer on this?  And, the
documentation is at best confusing on this issue.  Perhaps it can
be reworded for a future release.

Thanks,

Jerry
T.RTitleUserPersonal
Name
DateLines
2127.1confirmed :-)TAVENG::BORISBoris Gubenko, ISETue Mar 25 1997 13:1218
  I don't want to look protecting DEC C RTL Reference Manual at any price -
  the Manual leaves a lot to be desired - but it seems to me, that in this
  (relatively rare :-) case the Manual provides a clear answer:

        int i = 1;
	printf("%3.3d\n",i);

  According to the precision field, "the minimum number of DIGITS to appear"
  is 3, so we have 001 (it is obviously, that zero is the only digit which
  can be used to satisfy the precision).

  Now, because we already have an output field of width 3, no padding for the
  width is performed.

  How would you suggest to make the Manual more clear?

  Boris
2127.2Thanks, and...CSC32::J_HENSONDon&#039;t get even, get ahead!Wed Mar 26 1997 09:3329
>>            <<< Note 2127.1 by TAVENG::BORIS "Boris Gubenko, ISE" >>>
>>                               -< confirmed :-) >-


>>        int i = 1;
>>	printf("%3.3d\n",i);

>>  According to the precision field, "the minimum number of DIGITS to appear"
>>  is 3, so we have 001 (it is obviously, that zero is the only digit which
>>  can be used to satisfy the precision).

>>  Now, because we already have an output field of width 3, no padding for the
>>  width is performed.

>>  How would you suggest to make the Manual more clear?

Boris,

Thanks for the confirmation.  I'll pass it on to my customer.

As for making the manual more clear, I can offer this.

In this particular case, there appear to be two different and conflicting
descriptions.  One which addresses padding, and the other which addresses
the precision, and which happens to be the overriding rule.  Perhaps
some verbage which defines which rule takes precedence over the other
in cases such as this would be appropriate.

Jerry
2127.3thanks for the suggestionTAVENG::BORISBoris Gubenko, ISEWed Mar 26 1997 12:1632
  Jerry,

  Thanks for the suggestion.

  Description of the field width uses the term "converted output source" which
  implies, that the field width is considered after the conversion is done:
  
        field width     The minimum field width can be designated...
                        ...
                        If the converted output source is wider than...
                        ...
                        If the converted output source is narrower than...

  Probably, we should emphasis the fact, that the field width is the last
  component of the format directive processed by printf() (see also note 2068
  in this conference).

  How about:

        field width     The minimum field width can be designated ....

                        The minimum field width is considered after the
                        conversion is done according to the all other
                        components of the format directive. This component
                        affects padding the result of the conversion as
                        follows:

                        If the result of the conversion is wider than...
                        ...
                        If the result of the conversion is narrower than...

  Boris
2127.4looks better to meCSC32::J_HENSONDon&#039;t get even, get ahead!Mon Mar 31 1997 10:2114
>>            <<< Note 2127.3 by TAVENG::BORIS "Boris Gubenko, ISE" >>>
>>                         -< thanks for the suggestion >-

Boris,

That looks better.  Personally, though, I think that wording this so that
someone doesn't misinterpret it is very hard to do.  There are just too
many factors to mention....

I'm just glad that it's someone else's job to wordsmith this. ;-)

Thanks,

Jerry