INF: Variable Width, Precision Available with printf()

ID Number: Q47663

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:

The Microsoft C 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 Optimizing Compiler Run-Time Library

Reference" version 5.1 manual. The following code sample demonstrates

this capability.

Sample Code

-----------

/* This program defines a procedure to print out a floating-point

number with variable width and precision.

*/

/* Compile options needed: none

*/

#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");

}

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 5.0 5.1 6.0

6.0a 6.0ax 7.00 7.0