INF: Clarification of the "g" Format Specifier for printf()

ID Number: Q43392

5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.00 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, the output format resulting from the printf() format specifier

"g" does not exactly match the output format resulting from either

format specifier "e" or "f." Page 459 of the "Microsoft C Optimizing

Compiler Run-Time Library Reference" states that "g" will use either

the "f" or "e" format, whichever is more compact.

The precision value is interpreted differently in "g" format than in

"f" format. Table R.3 on pages 461-2 states the difference. The

precision for "f" specifies the number of digits after the decimal

point. The precision for "g" specifies the maximum number of

significant digits printed.

More Information:

The following example demonstrates the difference:

#include <stdio.h>

void main (void)

{

printf ("%.4g\n", 4.0/3.0); /* Outputs: 1.333 */

printf ("%.4f\n", 4.0/3.0); /* Outputs: 1.3333 */

}

The results of the above program are correct.

Additional reference words: 6.0 6.00 6.0a 6.00a 6.0ax 6.00ax 2.0 2.00

2.01 2.5 2.50 2.51 7.00 QuickC