[OPEN-ILS-DEV] C nits: invalid conversion specification

Joe Atzberger atzberger at MECDC.ORG
Mon May 7 12:07:27 EDT 2007


Perl accepts "%lf" as an accepted format for printf and sprintf.  It would only be particularly meaningful on a system 
where the default size of a floating point number was not long double (i.e., double or quad).

 From perldoc:
	http://perldoc.perl.org/functions/sprintf.html
   "For floating point conversions (e f g E F G), numbers are usually assumed to be the default floating point size on 
your platform (double or long double), but you can force 'long double' with q, L , or ll if your platform supports them."

Interestingly, this may conflict with the fact that perl's sprintf actually invokes the C version for floating point 
numbers:
   "Perl does its own sprintf formatting--it emulates the C function sprintf, but it doesn't use it (*except for 
floating-point numbers*, and even then only the standard modifiers are allowed)."

So based on that, I'm more confused than anything.  It seems like it should only be a question of what the C version 
supports for floats, even if perl doesn't report a warning/error.

--Joe Atzberger
INFOhio Technical Specialist
Metropolitan Educational Council
(614) 473-8300 x22
atzberger at mecdc.org


Scott McKellar wrote:
> In my copy of the code base (slightly out of date by now) I have
> found 17 occurrences of the conversion specification "%lf" for
> printf-style formatting.
> 
> So far as I can tell, this conversion specification is unrecognized
> by the Standard, and therefore invokes undefined behavior.  In 
> practice, gcc appears silently to ignore the extraneous length 
> modifier 'l', but it is not required to.  There is no assurance that 
> any other compiler, or any other version of gcc, will behave the 
> same way.
> 
> The obvious intent is to format a double.  There is presumably a
> misconception that one should use "%f" for a float and "%lf" for a
> double, just as one uses "%d" for an int and "%ld" for a long.
> 
> Such an analogy is faulty.  In fact there is no conversion 
> specifier for floats.  There doesn't need to be, because printf()
> and it's kindred will never see a float.  If you try to pass a float,
> it will be promoted to a double before the called function ever sees
> it.  
> 
> The usual conversion specifier for a double is simply '%f', possibly
> modified for width, precision, and so forth.  For a long double, of
> course, you can use "%Lf", and I saw a couple of those.
> 
> If you like, I can prepare a collection of patches, but that seems
> like overkill for such a trivial fix.  The best way to identify the
> offending lines is to execute the following command from the ILS
> directory:
> 
>     grep -r '%lf' * | grep '\.[ch]:'
> 
> This command reports .c and .h files.  I also saw some Perl modules
> containing "%lf", but I don't know enough about Perl to know whether 
> it's valid.
> 
> Scott McKellar
> http://home.swbell.net/mck9/aargh/
> 



More information about the Open-ils-dev mailing list