[gdal-dev] Question about json-c mods in changeset r18545

Mateusz Loskot mateusz at loskot.net
Sat Apr 28 18:21:37 EDT 2012


On 28 April 2012 14:07, Even Rouault <even.rouault at mines-paris.org> wrote:
> Le samedi 28 avril 2012 14:09:35, Mateusz Loskot a écrit :
>> I do not use MinGW, but could you or anyone who is concerned about MinGW
>> support try the following test and report any errors occurred?
>>
>> https://gist.github.com/2518402
>
> I've tried with 2 mingw versions, and with both the %lf %lg don't work well
> (although the output is a bit different)
> [...]

Even,

Big thanks for testing it for me.

> My conclusion is that %lf or %lg is unnecessary (and unspecified by the
> standards) when passing a double.

That's not really like that.

Given the current C99 (I use N1256 draft), the rules depend on the
function kind:
fprintf (printf, sprintf, ...) or scanf.

"Foreword
(...)
This second edition cancels and replaces the first edition, ISO/IEC
9899:1990, as
amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC 9899/AMD1:1995, and
ISO/IEC 9899/COR2:1996. Major changes from the previous edition include:
(...)
%lf conversion specifier allowed in printf
(...)"

7.19.6.1 The fprintf function
   7 The length modifiers and their meanings are:
      l (ell) Specifies that (...) has no effect on a following a, A,
e, E, f, F, g, or G conversion
specifier.
      L Specifies that a following a, A, e, E, f, F, g, or G
conversion specifier
applies to a long double argument.

7.19.6.2 The fscanf function

   11 The length modifiers and their meanings are:
      l (ell) Specifies that (...) that a following a, A, e, E, f, F,
g, or G conversion specifier applies to
an argument with type pointer to double;
      L Specifies that a following a, A, e, E, f, F, g, or G
conversion specifier
applies to an argument with type pointer to long double.

   12 The conversion specifiers and their meanings are:
      a,e,f,g Matches an optionally signed floating-point number, (...)

   14 The conversion specifiers A, E, F, G, and X are also valid and
behave the same as,
respectively, a, e, f, g, and x.

Long story short, for printf:
%f -> double
%Lf -> for long double.

for scanf::
%f -> float
%lf -> double
%Lf -> for long double.

Or, I'm misunderstanding the C99 document.
If I'm correct, then all C99 compilers should printf using %Lf
but not %lf, thus json-c is incorrect and I have the basis to suggest
the json-c maintainers to fix it.

Even, would you agree?

> When you specify %f in printf, the compiler
> naturally expects a double (and if you provide a float, due to the fact that it
> is a varags, it is automatically promoted to double). There is often
> confusion, because in sscanf, %f expects a pointer to a float, whereas %lf
> expects a pointer to a double. But printf is different...

Yes, but the difference is important.

Best regards,
-- 
Mateusz Loskot, http://mateusz.loskot.net


More information about the gdal-dev mailing list