KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q47663: Variable Width, Precision Available with printf

Article: Q47663
Product(s): See article
Version(s): 5.00 5.10 | 5.00 5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | S_QuickC 1.00 1.10 2.00 2.01 S_QuickASM | mspl13_c
Last Modified: 9-AUG-1989

The Microsoft run-time library function printf() allows the width of
the format specifier to be supplied at run time. This is done with the
use of the * with the format specifier. This is mentioned on Pages 460
and 461 of the "Microsoft C 5.1 Optimizing Compiler Run-Time Library
Reference." The following code sample demonstrates this capability.

Code Sample
-----------

/* This program defines a procedure to print out a floating point
   number with variable width and precision.
*/
#include <stdio.h>

void print(int, int, float);

void main(void) {
   print (10, 2, 1234.5678f);
   print (9, 4, 1234.5678f);
   print (5, 2, 1234.5678f);
}

void print(int w, int p, float value)  {
   printf ("Printf format :%*.*f\n", w, p, value);
   printf ("Width =        0123456789012\n");
}

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.

Copyright Microsoft Corporation 1986-2002.