PRB: Bad Results When %lf Is Format Specifier for Long Double

Last reviewed: July 22, 1997
Article ID: Q128791
7.00 | 1.00 1.50 1.51 1.52 MS-DOS | WINDOWS kbtool kbprb

The information in this article applies to:

  • The Microsoft C Run-time (CRT), included with:

        - Microsoft C/C++ version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
    
    --------------------------------------------------------------------

SYMPTOMS

Unpredictable results occur when %lf is used as a format specifier for a long double in functions such as sprintf() or printf() as in this example:

   sprintf(chr_buffer, "The value of long double variable is %.3lf ",
                       lng_dbl);

CAUSE

The format specifier for a long double is Lf, and the format specifier for a double is lf. The format argument tells the function the size and type of the arguments. A long double is 10 bytes and a double is 8 bytes so they need different format specifiers.

RESOLUTION

Use %Lf as the format specifier for a long double.

STATUS

This behavior is by design.

MORE INFORMATION

Sample Code

/* Compile options needed: none
*/

#include <stdio.h>
void main(void)
{
     long double lng_dbl = 2.345;
     char ch_arr[100];

 //  Line with the problem resulting from the incorrect specifier
     sprintf(ch_arr, "%.3lf", lng_dbl);
     printf("The value of 2.345 is not %s \n", ch_arr);

 //  Line with the correct format specifier
     sprintf(ch_arr, "%.3Lf ", lng_dbl);
     printf("The value of 2.345 is %s", ch_arr);
 }


Additional reference words: 7.00 1.00 1.50 1.51 1.52
KBCategory: kbtool kbprb
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.