INF: Operation of scanf() Using the Width Option in the Format

ID Number: Q40357

5.x 6.00 6.00a 6.00ax 7.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 scanf() run-time library function allows the specification of

the width that you would like to read in from the input device. Some

examples of this width formatting are as follows:

scanf ("%10s", buffer); /* read ten chars into buffer[] */

scanf ("%5d", &i); /* read five digits of a number into i */

When using width parameters in the format, the first n digits are read

into the address specified.

More Information:

When used with a character string, the width parameter performs as

follows:

char buffer[15];

scanf ("%10s", buffer);

The first 10 characters are read, and a null terminator ('\0') is

added to the end of the string.

When used with an integer or float, scanf() always reads the first n

characters (including decimal if in a float) specified with the width

format. Entering invalid characters (for example, a decimal in an

integer, or any alphabetic character) causes the termination and

failure of the read.

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00